MySQL update using PDO and prepared statement not working

后端 未结 3 894
失恋的感觉
失恋的感觉 2021-01-17 13:35

I\'m having a strange problem with php PDO and mysql.

I have the following table:

create table test_table ( id integer, value text );
相关标签:
3条回答
  • 2021-01-17 13:58

    http://php.net/manual/en/pdo.prepare.php states:

    You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.

    As this indicates, the likely reason behind your code working on one server and not another is that PDO::ATTR_EMULATE_PREPARES is disabled on the server which the code fails on. As the documentation says, this attribute effectively removes the restriction preventing you from using a parameter marker of the same name twice (along with some other restrictions).

    0 讨论(0)
  • 2021-01-17 14:15
    try {
         $db = new PDO('mysql:host=localhost;dbname=vendor_management_system', 'root', '');
         $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    
    
    
    
    $fields[] = 'car_name';
    $fields[] = 'model_no';
    $fields[] = 'maker_id';
    $fields[] = 'dealer_id';
    
    $values[] = "testcar";
    $values[] = "no#1";
    $values[] = 2;
    $values[] = 4;
    
    
    
    echo SQLUpdate('car_details', $fields, $values,'car_id = 32 and car_name = "testname"',$db);
    
    
    
    
    //START: SQLUpdate
    //$fields = array of fields in DB
    //$values = array of values respective to the $fields
     function SQLUpdate($table,$fields,$values,$where,$db) {
    
    
    
      //build the field to value correlation
      $buildSQL = '';
      if (is_array($fields)) {
    
            //loop through all the fields and assign them to the correlating $values
            foreach($fields as $key => $field) :
          if ($key == 0) {
                //first item
                $buildSQL .= $field.' = ?';
              } else {
                //every other item follows with a ","
                $buildSQL .= ', '.$field.' = ?';
              }
        endforeach;
    
      } else {
        //we are only updating one field
            $buildSQL .= $fields.' = :value';
      }
    
      $prepareUpdate = $db->prepare('UPDATE '.$table.' SET '.$buildSQL.'
    WHERE '.$where);
    
      //execute the update for one or many values
      if (is_array($values)) {
        $affected_rows=$prepareUpdate->execute($values);
        return $affected_rows;
      } else {
        $affected_rows=$prepareUpdate->execute(array(':value' => $values));
        return $affected_rows;
      }
    
    
      //record and print any DB error that may be given
      $error = $prepareUpdate->errorInfo();
      if ($error[1]) print_r($error);
    
    } 
    //END: SQLUpdate
    
    0 讨论(0)
  • 2021-01-17 14:15
    $maker_id=1;
    $stmt = $db->prepare("UPDATE car_details SET maker_id=?");
    $affected_rows=$stmt->execute(array($maker_id));
    echo $affected_rows.' were affected';
    
    0 讨论(0)
提交回复
热议问题