PHP execute不会返回任何错误,但数据库中没有添加任何行

时间:2017-02-21 10:52:38

标签: php mysql

我有以下PHP脚本,当单击注册按钮并选中地址复选框时应该执行该脚本。

if(isset($_POST['register'])){
   if($_POST['addressCheckBox']  == 'Yes'){
      if(isset($_POST['streetNumber']) && isset($_POST['route']) && isset($_POST['sublocality_level_1'])){
         $unitDetails = trim($_POST['unitDetails']);
         $streetNumber = mysqli_real_escape_string($db, trim($_POST['streetNumber']));
         $route = mysqli_real_escape_string($db, trim($_POST['route']));
         $suburb = mysqli_real_escape_string($db, trim($_POST['sublocality_level_1']));
         $city = mysqli_real_escape_string($db, trim($_POST['city']));
         $zipCode = mysqli_real_escape_string($db, trim($_POST['zipCode']));
         $province = mysqli_real_escape_string($db, trim($_POST['province']));
         $country = mysqli_real_escape_string($db, trim($_POST['country']));
         $email = mysqli_real_escape_string($db, trim($_POST['email']));

         if(!empty($streetNumber)  && !empty($route) && !empty($city) && !empty($province) && !empty($zipCode) && !empty($country)){
            $queryInsertMemberAddress = $db->prepare("INSERT INTO address (memberEmail, unitDetails, houseNumber, streetName, suburb, city, postalCode, province, country) VALUES (?,?,?,?,?,?,?,?,?)");

            $queryInsertMemberAddress->bind_param('ssisssiss', $email, $unitDetails, $streetNumber, $route, $suburb, $city, $zipCode, $province, $country);

            $queryInsertMemberAddress->execute() or die($queryInsertMemberAddress->error);
         }
      }
   }
}

当脚本运行时,我没有收到任何错误,但数据库中没有任何行被插入。我试图通过执行以下操作来查找错误:

  1. - > execute()之后对$ queryInsertMemberAddress执行 var_dump 并返回

    object(mysqli_stmt)[19]
       public 'affected_rows' => null
       public 'insert_id' => null
       public 'num_rows' => null
       public 'param_count' => null
       public 'field_count' => null
       public 'errno' => null
       public 'error' => null
       public 'error_list' => null
       public 'sqlstate' => null
       public 'id' => null
    
  2. - > execute()之后对$ queryInsertMemberAddress变量执行 print_r ,然后返回

    mysqli_stmt Object ( [affected_rows] => 1 [insert_id] => 28 [num_rows] => 0 [param_count] => 9 [field_count] => 0 [errno] => 0 [error] => [error_list] => Array ( ) [sqlstate] => 00000 [id] => 4 
    
  3. 我无法理解为什么print_r显示一行受到影响但数据库中没有显示记录。有谁看到我哪里出错了?

1 个答案:

答案 0 :(得分:0)

广泛关注

每次调用数据库类的方法时,都应该将其包装在if语句中以检查它是否正确执行,然后反馈发现的任何错误。

  • MySQL error reporting setup

     if(!$queryInsertMemberAddress = $db->prepare("INSERT INTO address (memberEmail, ... , country) VALUES (?,...,?)")){
         die("failed prepare: ".$db->error);
     }
     if(!$queryInsertMemberAddress->bind_param('ssisssiss', $email, ..., $country)){
         die("failed binding: ".$queryInsertMemberAddress->error);
     }
     if(!$queryInsertMemberAddress->execute()){
           die("failed execution: ".$queryInsertMemberAddress->error);
     }
    
  • 删除所有mysqli_real_escape_string来电,这些电话是多余的。

  • 是否允许您的MySQL用户在该表中插入数据?您的PHP连接中指定的用户是否具有INSERT的权限?

我在这里可能完全错了,但我认为可能发生的事情是你的bind_param声明正在破坏你的SQL。

Bind Param要求给定的元素属于第一个字符串中指定的类型,你有ssisssiss所有字符串,其中有一些整数混合in。没关系,但你的数据都来自$_POST元素 ,它们总是字符串

因此,如果你var_dump你正在插入的数据,那么你将var_dump很多(string)类型的值。并且没有(int)类型值。

实施例

 $_POST['value'] = "1"; //this is a string
 $value = mysqli_real_escape_string($db,trim($_POST['value'])); //optional, from your code...
 var_dump($value); /// (string)"1";

但是你无法将Bind Param转换为所有字符串(sssssssss),因为这会与你试图插入的MySQL列的数据类型不匹配。

我认为发生的事情是,由于这些类型不匹配,bind_param缺少值和/或中止整个SQL,并且由于某种原因没有给出适当的错误反馈。

如何查看

if(!$queryInsertMemberAddress->bind_param('ssisssiss', $email, $unitDetails, ...) ){
    die("bind failed"); //bind_param returns true or false 
}

解决方案建议:

$streetNumber = (int)$streetNumber;
$zipCode = (int)$zipCode;

请告诉我这是怎么回事以及这会给你带来什么结果。