0

我已经使用 PHP 创建了一个注册页面。控件总是进入其他部分('错误')。我已经定义了数据库,使用了表单字段中使用的相同 ID,但仍然相同。我无法弄清楚这一点。我真的很感激任何帮助。

这是我的代码:

<?php
require_once 'connectdb.php';

$dbc = mysqli_connect($mysql_host, $mysql_user , $mysql_pass , $mysql_db ) or die('Error Connecting to database');
if ( isset($_POST['submit'] ) )
{

$first_name =  mysqli_real_escape_string($dbc, trim($_POST['first_name']));
$last_name =  mysqli_real_escape_string($dbc, trim($_POST['last_name']));
$password1 =  mysqli_real_escape_string($dbc, trim($_POST['password1']));
$password2 =  mysqli_real_escape_string($dbc, trim($_POST['password2']));
$email = mysqli_real_escape_string($dbc, trim($_POST['email']));

if (!empty($first_name) &&  !empty($last_name) && !empty($password1) && !empty($password2) && ($password1==$password2) )
{
    $query = "SELECT * FROM user_db WHERE email='$email'";
    $data = mysqli_query($dbc, $query);
    if(mysqli_num_rows($data) == 0) 
    {
        $password2 = md5($password1);
        $query = "INSERT INTO user_db(fname, lname, email, password) VALUES('$first_name','$last_name','$email','$password2')";
        $data = mysqli_query($dbc, $query) or die('Error Querying database-Adding info');
        echo '<strong>Registration Successful</strong>';

        mysqli_close($dbc);
        exit();
    }
    else
    {
        echo 'An account already exists for with this email id. Kindly use a different one<br /> ';
        $email = "";
    }
}

else
{
    echo 'Please enter all the fields correctly, including the desired password correctly <br/>';
}

}
 // The control goes everytime into this else part
 else
{
    echo 'Error';
}

mysqli_close($dbc);

?>
4

3 回答 3

1

$_POST['submit']未设置。这就是您的代码进入“错误”部分的原因。

您必须设置 html 元素的名称,以便 PHP 知道要查找的内容。

像这样的东西:

<form action="index.php" method="POST" > 
First Name :    <input type="text" id="first_name" name="first_name" /><br/> 
Last Name : <input type="text" id="last_name" name="last_name" /><br/> 
Email    : <input type="text" id="email" name="email" /><br/> 
Password : <input type="password" id="password1" name="password1" /><br/> 
Repeat Password:<input type="password" id="password2" name="password2" /><br/> 
<input type="submit" id="submit" value="submit" name="submit" /> </form>

还要考虑不要混合 html 元素的类型和它们的 id/name。submit 不是 html 元素的最终名称,因为它可能会带来很多混乱。而是将提交按钮命名为:

<input type="submit" id="submit-registration" value="submit" name="submit-registration" /> </form>
于 2013-04-07T11:49:30.273 回答
0

请显示 HTML。我的猜测是您的表单中没有带有and a的<input>标签:-)name="submit"value="something"

于 2013-04-07T11:48:54.433 回答
0

这告诉你一件事..submit没有在你的论坛中通过。这是一个简单的测试。在您的表单中添加这样的输入<input type="text" name="submit" />,然后重试

于 2013-04-07T11:51:54.587 回答