使用预准备语句的代码有什么问题?

时间:2014-01-20 17:35:32

标签: php mysqli prepared-statement

我刚开始在这段代码中使用预处理语句,但我遇到了问题。我正在使用它们来查看具有某个电子邮件的用户是否已经存在,但它不起作用。这是我的代码:

$mysqli = new mysqli("localhost", "root", "mypass", "mydb");

$uname = $mysqli->real_escape_string($_POST["uname"]);
$email = $mysqli->real_escape_string($_POST["email"]);
$passwd = $mysqli->real_escape_string($_POST["passwd"]);

$stmt1 = $mysqli->prepare("SELECT * FROM users WHERE email=?");
$stmt1->bind_param("s", $email);
$stmt1->execute();
$res1 = $stmt1->get_result();
$i = 0;
while($row1 = $res1->fetch_assoc()) {
    $i = $i + 1;
}
if($i != 0) {
    die("A user with that email already exists.");
}

然后,我继续使用相同的代码检查用户名是否已被使用,只需将对uname的电子邮件的引用换成并将stmt1切换为stmt2。我的代码在这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

这是我用于我的网站的登录页面。最好根据您的要求进行更改。

<?php
global $con;;
$con = mysql_connect("localhost:3306", 'rvani', "rv6861", TRUE) or die (mysql_error());
mysql_select_db("rvani", $con);

/// Connection

$pwd = trim($_POST['pwd']);
$email = trim($_POST['email']);`enter code here`
if($pwd == ""){
    echo'PasswordNull';
    return;
    }
if($email == ""){
    echo'EmailNull';
    return;
    }
$quy = "select * from MLogin where email_id='$email'";
global $con;
try {
    $res= mysql_fetch_assoc(mysql_query($quy, $con));
    }
catch (Exception $e1)
    {
    echo 'Error';
    }
if(md5($pwd) === $res['Password']){
    //echo'Successfully';
    echo'Welcome';
    return ;
}else{
    echo'PasswordNotMatch';
    return ;
}    
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
相关问题