我是PHP和MySQL的新手,我已经为此工作了三天,这个问题让我很难过。我没有错误,也没有向我的数据库添加任何查询。在页面register.php队列之后,它应该加载带有Registered
文本的login.php,但它是空白的,没有记录添加到数据库中。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<?php
$page_title = 'Register';
if( $_SERVER['REQUEST_METHOD'] == 'POST')
{
include_once "connect.php";
$errors = array();
if( empty( $_POST['user_id']))
{
$errors[] = 'Enter your user ID.';
}
else
{
$ui = mysqli_real_escape_string($db_name, trim($_POST['user_id']));
}
if ( empty($POST['pass1']))
{
if($_POST['pass1'] != $_POST['pass2'])
{
$errors[] = 'Passwords do not match.';
}
else
{
$fn = mysqli_real_escape_string($db_name, trim($_POST['pass1']));
}
}
else
{
$errors[] = 'Enter your Password.';
}
if (empty($errors))
{
$q = "INSERT INTO User_id
(user,password)
VALUES ('$ui','$fn',SHA('$fn'),NOW())";
$r = mysqli_query ($db_name,$q);
if ($r)
{
echo '<h1>Registered!</h1>
<p> You are now registered.</p>
<p><a href="login.php">Login</a></p>';
}
mysqli_close( $db_name);
exit();
}
else
{
echo '<h1>Error!</hl>
<p id="err_msg">The following error(s) occurred:<br>';
foreach ($errors as $msg)
{
echo "-$msg<br>";
}
echo 'Please try again.</p>';
mysqli_close( $db_name);
}
}
?>
<h1>Register</h1>
<form action = "register.php" method="POST">
<p>
User ID: <input type="text" name= "user_id"
value="<?php if (isset( $_POST[ 'user_id']))
echo $_POST[ 'user_id'];?>">
Password: <input type="password" name="pass1"
value="<?php if (isset($_POST['pass1']))
echo $_POST ['pass1'];?>">
Confirm Password: <input type="password" name="pass2"
value="<?php if(isset($_POST['pass2']))
echo $_POST['pass2'];?>">
</p> <p>
<input type="submit" value= "Register"> </p>
</form>
</body>
</html>
答案 0 :(得分:6)
你告诉MySQL你想要将数据插入两列,然后给它插入四个值。那会导致错误。但是你没有看到它,因为你在查询后没有检查mysqli_error()
。
$q = "INSERT INTO User_id (user,password) VALUES ('$ui','$fn',SHA('$fn'),NOW())";
^^^ 2 columns ^^^ 4 values
以下是错误处理的非常基本的示例:
$r = mysqli_query ($db_name,$q);
if ($r === false) {
echo mysqli_error($db_name);
exit; // This needs to be a lot better
}
答案 1 :(得分:2)
这是错误的:
$q = "INSERT INTO User_id (user,password)
VALUES ('$ui','$fn',SHA('$fn'),NOW())";
为2列插入4个值不起作用。
将它添加到脚本的顶部,以便mysqli将抛出异常并告诉您确切的错误:
mysqli_report(MYSQLI_REPORT_ALL);
答案 2 :(得分:-2)
$q = "INSERT INTO User_id
(user,password)
VALUES ('$ui','$fn',SHA('$fn'),NOW())";
表格列太少,无法容纳已过帐的值。检查