我正在尝试将用户信息保存到数据库中,但我收到错误。
我的输入表格:
<form method="post" action="do_signup.php">
Name:<input type="text" id="name" name="name"/>
<br/>
Username:<input type="text" id="user" name="user"/>
<br/>
Choose Password: <input type="password" name="pass" id="pass"/>
<br/>
Confirm Password:<input type="password" name="pass2" id="pass2"/>
<br/>
E-Mail:<input type="email" name="email" id="email"/>
<input type="submit"/>
</form>
do_signup.php
<?php
include 'includes/userdbConnect.php';
$name=$_POST['name'];
$username=$_POST['user'];
$password=$_POST['pass'];
$password2=$_POST['pass2'];
$email=$_POST['email'];
if($password!=$password2)
echo"<script> alert('Password and confirm password does not match');
window.location='signup.php'</script>";
exit;
$sql = mysql_query("INSERT INTO `riz`.`users` (`id`, `Username`, `Password`, `Email`) VALUES (NULL, '$username', '$password', '$email');") or die("SELECT Error: ".mysql_error());
if($sql)
{
echo "<script> alert('Signup successfully!');
window.location='signin.php'</script>";
exit;
}
else
{
echo "<script> alert('Temporary problem, try again!');</script>";
}
?>
userdbConnect.php。
<?php
error_reporting(E_ERROR);
global $link;
$servername='localhost';
$dbname='riz';
$dbusername='root';
$dbpassword='';
$table_Name="users";
$link = mysql_connect($servername,$dbusername,$dbpassword);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
else
{
mysql_select_db($dbname,$link) or die ("could not open db".mysql_error());
}
?>
我的do_signup.php从signup.php获取所有信息,我已经通过echo检查了所有的值。它也正常工作,直到我检查密码和确认密码。但INSERT查询不起作用,我得到空白页面。指导我在哪里做错了。提前致谢
答案 0 :(得分:1)
您的问题出在if
声明中。
if($password!=$password2)
echo"<script> alert('Password and confirm password does not match');
window.location='signup.php'</script>";
exit;
没有括号,只会针对条件执行第一行。实际上它是这样做的:
if($password!=$password2) {
echo"<script> alert('Password and confirm password does not match');
window.location='signup.php'</script>";
}
exit;
所以无论条件如何,每次都exit
。
您需要做的就是正确添加括号:
if($password!=$password2) {
echo"<script> alert('Password and confirm password does not match');
window.location='signup.php'</script>";
exit;
}
此外,不推荐使用mysql_ *函数,并且您的脚本容易受到SQL注入攻击。请仔细阅读:How can I prevent SQL injection in PHP?