我从用户那里获取三个输入:UserName,FirstName,Email。
我想检查一下,如果数据库中存在用户名,则应检查其对应的名字和电子邮件。如果这三个都正确则打印消息。
html代码:
<html>
<body>
Username: <input type="text" name="username"/>
FirstName: <input type="text" name="name"/>
Email: <input type="text" name="email"/>
</body>
</html>
PHP代码:
if(isset($_POST['submit']))
{
$usrnm=$_POST['username'];
$name=$_POST['name'];
$email=$_POST['email'];
$user_name = "root";
$password = "";
$database = "show_your_talent";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found)
{
$res="SELECT UserName,Fname,Email FROM reg WHERE UserName='$usrnm'";
$result = mysql_query($res);
$count=mysql_num_rows($result);
if($count==1)
{
echo "<script type='text/javascript'> alert('Password has been sent to your email id..')</script>";
}
else
{
echo "<script type='text/javascript'> alert('Wrong')</script>";
}
}
mysql_close($db_handle);
}
答案 0 :(得分:2)
首先请使用html中的form标签和方法post。然后使用此
if ($db_found)
{
$name=$_POST['name'];
$email=$_POST['email'];
$res="SELECT * FROM reg WHERE UserName='".$usrnm."'";
$res1="SELECT * FROM reg WHERE UserName='".$usrnm."' and Fname ='".$name."'";
$res2="SELECT * FROM reg WHERE UserName='".$usrnm."' and Fname ='".$name."' and Email='".$email."'";
$result = mysql_query($res);
$result1= mysql_query($res1);
$result2 = mysql_query($res2);
$count=mysql_num_rows($result);
$count1=mysql_num_rows($result1);
$count2=mysql_num_rows($result2);
if(($count==1) && ($count1==1) && ($count2==1) )
{
echo "<script type='text/javascript'> alert('Password has been sent to your email id..')</script>";
}
else
{
echo "<script type='text/javascript'> alert('Wrong')</script>";
}
}
答案 1 :(得分:2)
您缺少表单标记和(命名)提交按钮以符合条件语句if(isset($_POST['submit'])){...}
如果没有它,您的代码将无法执行。
<html>
<body>
<form action="handler.php" method="post">
Username: <input type="text" name="username"/>
FirstName: <input type="text" name="name"/>
Email: <input type="text" name="email"/>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
然后,您需要将查询修改为:
$res="SELECT * FROM reg WHERE UserName='$usrnm'
AND Email='$email' AND Fname='$name'";
将stripslashes()
和mysql_real_escape_string()
添加到POST变量中。
即:(并为所有POST变量执行此操作)
$usrnm = stripslashes($_POST['username']);
$usrnm = mysql_real_escape_string($_POST['username']);
然而,最好使用准备好的陈述;见下文。
将error reporting添加到文件的顶部,这会发出错误信号。
error_reporting(E_ALL);
ini_set('display_errors', 1);
旁注:错误报告应仅在暂存时完成,而不是生产。
将or die(mysql_error())
包括在mysql_query()
中。你没有做的事情。