0

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

它应该像这样工作:首先,用户必须在搜索栏中搜索一个项目。然后显示匹配项,每个匹配项旁边都有单选按钮,底部有一个提交按钮。提交后,表单数据(这是他们用收音机检查的搜索结果)进入数据库表“用户”。'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
}
?>
4

1 回答 1

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>
于 2013-08-02T18:49:47.790 回答