在同一页MYSQLI上的多个预准备语句

时间:2017-07-06 13:47:04

标签: php mysqli prepared-statement

我的代码存在一些问题。似乎我无法准备多个准备好的陈述,这对我来说有点不寻常。

如果您能发现错误或帮助我,我会很高兴,因为我无法弄清楚这个问题。

我的错误:

  

致命错误:在第37行的/Applications/XAMPP/xamppfiles/htdocs/platform/creating_user.php中调用boolean上的成员函数bind_param()

我正在尝试检查用户的电子邮件是否已存在于数据库中,然后注册用户。

当我不执行$ check时,代码工作正常。

$check->execute();

我还想对我的工作流程(我的代码构建方式)做出一些回应。好吗?

谢谢!

<?php 


$db = new mysqli("localhost","root","","database");
session_start();


if (isset($_POST)){ 
    if(
        !empty($_POST["name"]) 
      & !empty($_POST["city"]) 
      & !empty($_POST["zip"]) 
      & !empty($_POST["email"]) 
      & !empty($_POST["tel"]) 
      & !empty($_POST["password"])
    ) {

        $name       = encrypt($_POST["name"]);
        $city       = encrypt($_POST["city"]);
        $zip        = encrypt($_POST["zip"]);
        $email      = encrypt($_POST["email"]);
        $tel        = encrypt($_POST["tel"]);
        $password   = encrypt($_POST["password"]);


        if(!empty($name) && !empty($city) && !empty($zip) && !empty($email) && !empty($tel) && !empty($password)) {

            $check = $db->prepare("SELECT email FROM user WHERE email = ?");
            $check->bind_param('s', $email); 
            $check->execute();

            if ($check->num_rows == 1) {  
                header("Location: index.php");
                die();
            } else {


            $insert = $db->prepare("INSERT INTO user (name, city, zip, email, tel, password, created) VALUES (?, ?, ?, ?, ?, ?, NOW())");
            $insert->bind_param("ssssss",$name, $city, $zip, $email, $tel, $password);


            if ($insert->execute()){ 

                $db->close();
                $_SESSION["user"] = $email;
                header("Location: created_user");
                die();

            } else {
                header("Location: create-user");
                die();
            }

        }

        } else {
            header("Location: create-user");
            die();
        }


    } else {
        header("Location: create-user");
        die();
    }
} else {
    header("Location: create-user");
    die();
} 


?>

1 个答案:

答案 0 :(得分:0)

在php.net上,我找到了this

  

mysqli :: prepare()返回一个语句对象,如果有错误则返回FALSE   发生。

所以这意味着你的- (NSString*)convertStringToHTMLEscape:(NSString*)stringContent { stringContent = [stringContent stringByReplacingOccurrencesOfString:@"{" withString:@"%7B"]; stringContent = [stringContent stringByReplacingOccurrencesOfString:@"}" withString:@"%7D"]; stringContent = [stringContent stringByReplacingOccurrencesOfString:@"[" withString:@"%5B"]; stringContent = [stringContent stringByReplacingOccurrencesOfString:@"]" withString:@"%5D"]; stringContent = [stringContent stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; stringContent = [stringContent stringByReplacingOccurrencesOfString:@"\"" withString:@"%22"]; stringContent = [stringContent stringByReplacingOccurrencesOfString:@"\\" withString:@"%5C"]; stringContent = [stringContent stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"]; return stringContent; } 对象出了问题。这可能是:

  • 密码错误
  • 用户名错误
  • ...