我已经使用 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);
?>