注册表 - 电子邮件不会显示

时间:2015-06-21 12:50:53

标签: php mysql forms email registration

我正在为网站做一个注册表,我总是收到错误

mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\registration\registration.php on line 60

现在,我认为我更正了它,因为错误不再出现,我可以向表格(phpmyadmin)提交名称和密码。但是,电子邮件不会出现! 它保持这样:

姓名:teste 密码:teste 电子邮件:user_email

我做错了什么?我跟随this tutorial,我只纠正了一些不起作用的事情。不会显示任何错误消息,它只是不会出现在我的数据库中的电子邮件。我已经尝试将mysql更改为msqli,只会让它显示更多错误。

我的registration.php文件(我审查了root,pass等)

<html>
<head>
    <title> Registration Form </title>
    </head>

<body>

<form method='post' action='registration.php'>
<table width='400' border='2' align='center'>
<tr>
    <td colspan='5' align='center'><h1>Registration Form</h1></td>
</tr>
<tr>
    <td align='center'>User Name:</td>
    <td><input type='text' name='name' /></td>
</tr>
<tr>
    <td align='center'>User Password:</td>
    <td><input type='password' name='pass' /></td>
</tr>
<tr>
    <td align='center'>Email:</td>
    <td><input type='text' name='email' /></td>
</tr>
<tr>
    <td colspan='5' align='center'><input type='submit' name='submit' value='Sign Up' /></td>
</tr>
</table>

</body>
</html>

<?php 
error_reporting(E_ALL ^ E_DEPRECATED);
mysql_connect('localhost','root','','');
mysql_select_db("");

if (isset($_POST['submit'])) {

$user_name = $_POST['name'];
$user_pass = $_POST['pass'];
$user_email = $_POST['email'];

if ($user_name==''){
echo "<script>alert ('Please enter your name!')</script>";
exit ();
}
if ($user_pass==''){
echo "<script>alert ('Please enter your password!')</script>";
exit ();
}
if ($user_email==''){
echo "<script>alert ('Please enter your email!')</script>";
exit ();
}

$check_email = "select * FROM users_db WHERE user_email='$user_email'";

$run = mysql_query($check_email);

if(mysql_num_rows($run)>0) {
echo "<script>alert('Email $user_email already exist in our database, please try another one!')</script>";
exit ();
}

$query = "INSERT INTO users_db (user_name,user_pass,user_email) VALUES ('$user_name','$user_pass','user_email')";
if(mysql_query($query)) {
echo "<script>alert('Registration Successful!')</script>";

}
}


?>    

2 个答案:

答案 0 :(得分:0)

当您插入时,您将插入文字字符串"user_email"而不是用户提供的值:

('$user_name','$user_pass','user_email')

(注意缺少$字符。)

然而, 这不是您唯一的问题 。您的代码 极易受攻击 SQL注入攻击 。基本上,您的代码允许Internet上的任何随机用户在您的数据库上编写和执行他们自己的代码。请start by reading this。您将要开始使用预准备语句并将用户提供的值作为值绑定到这些语句,而不是直接在代码中包含用户提供的值。

答案 1 :(得分:0)

您错过了此查询中变量名称的$

$query = "INSERT INTO users_db 
             (user_name,user_pass,user_email) 
          VALUES ('$user_name','$user_pass','$user_email')";