多个表格注册不起作用

时间:2013-08-02 17:23:51

标签: php html mysql forms

我有一个注册过程,涉及两个提交的表单。问题是第一张表格没有提交。另外,我需要一种方法来关联两个表单,因为两者的信息都被插入到同一个表行中,我认为这可以通过获取前一个表行id来完成。

应该这样工作:首先,用户必须在搜索栏中搜索项目。然后显示匹配,每个匹配旁边的单选按钮和底部的提交按钮。提交时,表单数据(这是他们用收音机检查的搜索结果)进入数据库表'users'。 'users'表包含id,username,password和radio的行。

无线电选项被提交到电台。这也会创建一个自动递增的id。这是第一种形式。一旦选择了无线电选项并且数据在表格行中,用户必须填写第二个表格,要求提供电子邮件和密码,该表格将提交到无线电选项所在的同一行。

当我完成此过程时,表中会显示电子邮件(称为表中的用户名)和密码以及ID,但收音机始终为空白。不确定为什么没有提交无线电选项。也不确定我是否需要一种方式来关联表格。我是这方面的初学者,请尝试使答案可以理解。在此先感谢,见了代码:

<?php
       //This code runs if the form has been submitted

 if (isset($_POST['submit'])) { 



 //This makes sure they did not leave any fields blank

 if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {

        die('You did not complete all of the required fields');

    }



 // checks if the username is in use

    if (!get_magic_quotes_gpc()) {

        $_POST['username'] = addslashes($_POST['username']);

    }

 $usercheck = $_POST['username'];

 $check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'") 

or die(mysql_error());

 $check2 = mysql_num_rows($check);



 //if the name exists it gives an error

 if ($check2 != 0) {

        die('The email '.$_POST['username'].' is already in use.');

                }


 // this makes sure both passwords entered match

    if ($_POST['pass'] != $_POST['pass2']) {

        die('Your passwords did not match. ');

    }



    // here we encrypt the password and add slashes if needed

    $_POST['pass'] = md5($_POST['pass']);

    if (!get_magic_quotes_gpc()) {

        $_POST['pass'] = addslashes($_POST['pass']);

        $_POST['username'] = addslashes($_POST['username']);

            }

 // now we insert it into the database
    $insert = "INSERT INTO users (username, password, radio)
            VALUES ('".$_POST['username']."', '".$_POST['pass']."', '".$_POST['radio']."')";
    $add_member = mysql_query($insert);
?>

 <h2><font color="red">Registered</font></h2>
 <p>Thank you, you have registered - you may now login</a>.</p>
<?php 
} 
else 
{   
?>
<font color = #000000><h1>Sign Up</h1></font>
<?php
// Search Engine
// Only execute when button is pressed
if (isset($_POST['keyword'])) {
// Filter
$keyword = trim ($_POST['keyword']);

// Select statement
$search = "SELECT * FROM tbl_name WHERE cause_name LIKE '%$keyword%'";
// Display
$result = mysql_query($search) or die('That query returned no results');
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
while($result_arr = mysql_fetch_array( $result )) 
{ 
?>
// Radio button
<input type="radio" name="radio">
<?php
echo $result_arr['cause_name']; 
echo " "; 
echo "<br>";
}
?>
<input type="submit" name="radio_submit" value="Select Item">
</form>
<?php
$anymatches=mysql_num_rows($result); 
if ($anymatches == 0) 
{ 
   echo "We don't seem to have that cause. You may add a cause by filling out a short <a href='add.php'>form</a>.<br><br>"; 
}
}
?>
<!--Sign Up Form-->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="sign_up_form">
<input type="text" name="keyword" placeholder="Search" onFocus="this.select();" onMouseUp="return false;">
<input type="submit" name="search" value="Search">
<br />
<br />
<input type="email" name="username" maxlength="250" placeholder="Your email" onFocus="this.select();" onMouseUp="return false;">
<input type="password" name="pass" maxlength="50" placeholder="Password" onFocus="this.select();" onMouseUp="return false;">
<input type="password" name="pass2" maxlength="50" placeholder="Re-type Password" onFocus="this.select();" onMouseUp="return false;">
<br />
<input type="submit" name="submit" value="Sign Up">
</form>
<?php
}
?>

1 个答案:

答案 0 :(得分:0)

像这样修改你的代码

<input type="radio" name="radio" value="<?php echo $result_arr['cause_name']; ?>" >

然后提交它你应该得到值

同时更新以下查询

<?php
echo $insert = "INSERT INTO users (username, password, radio) VALUES ('$_POST[username]', '$_POST[pass]', '$_REQUEST[radio]')";
    $add_member = mysql_query($insert);
?>

更改以下代码

 //This code runs if the form has been submitted

 if (isset($_POST['radio_submit'])) { 

/////

<?php 
    if (isset($_REQUEST['submit']))
    {
        $radio = $_REQUEST['radio'];
        $insert = mysql_query("INSERT INTO users (radio) VALUE ('$radio')");

        if (!$insert)
        {
            echo mysql_error();
        }
    }


?> 

<form action="" method="get"> 
<input type="radio" name="radio" value="red">red 
<input type="radio" name="radio" value="blue">blue 
<input type="submit" name="submit" /> 
</form>