PDO更新帮助执行pdo更新

时间:2014-12-07 11:02:26

标签: php mysql pdo

该功能非常简单:

变量:$ table是更新正在进行的表 和$ fields是表中的字段, 和$值从一个帖子生成并放入$ values数组中 和$ where是表的索引字段的id的值 $ indxfldnm是索引字段名称

function SQLUpdate($table,$fields,$values,$where,$indxfldnm) {

  //Connect to DB
    $dbaddr = DB_HOST;
    $dbusr = DB_USER;
    $dbpwd = DB_PASSWORD;
    $dbname = DB_DATABASE;

  $db = new PDO('mysql:host='.$dbaddr .';dbname='.$dbname.';charset=UTF8', $dbusr, $dbpwd); 

  //build the fields
  $buildFields = '';
  if (is_array($fields)) {

    //loop through all the fields
    foreach($fields as $key => $field) :
      if ($key == 0) {
        //first item
        $buildFields .= $field;
      } else {
        //every other item follows with a ","
        $buildFields .= ', '.$field;
      }
    endforeach;

  } else {
    //we are only inserting one field
    $buildFields .= $fields;
  }

  //build the values
  $buildValues = '';
  if (is_array($values)) {

    //loop through all the values
    foreach($values as $key => $value) :
      if ($key == 0) {
        //first item
        $buildValues .= '?';
      } else {
        //every other item follows with a ","
        $buildValues .= ', ?';
      }
    endforeach;

  } else {
    //we are only updating one field
    $buildValues .= ':value';
  }
 $sqlqry = 'UPDATE '.$table.' SET ('.$buildFields.' = '.$buildValues.') WHERE `'.$indxfldnm.'` = \''.$where.'\');';

   $prepareUpdate = $db->prepare($sqlqry); 
  //execute the update for one or many values
  if (is_array($values)) {
    $prepareUpdate->execute($values);
  } else {
    $prepareUpdate->execute(array(':value' => $values));
  }

  //record and print any DB error that may be given
  $error = $prepareUpdate->errorInfo();
  if ($error[1]) print_r($error);



 echo $sqlqry;
 return $sqlqry;
} 

到目前为止一切顺利 然而它不起作用 将值传递到适当的更新语句中的字段有问题 但我对pdo并没有那么好并且设置它 帮助修复代码将参数绑定到更新中的值会有所帮助 非常感谢

谢谢

2 个答案:

答案 0 :(得分:1)

尝试在您的功能中获取此功能

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

    // Prepare statement
    $stmt = $conn->prepare($sql);

    // execute the query
    $stmt->execute();

    // echo a message to say the UPDATE succeeded
    echo $stmt->rowCount() . " records UPDATED successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

答案 1 :(得分:0)

将代码更改为其他版本:

这消除了多值问题

function SQLUpdate($table,$fields,$values,$where,$indxfldnm) {


  $dbdata = array();
  $i=0;

    foreach ($fields as $fld_nm) 
    {
        if ($i > 0) {
        $dbdata[$fld_nm] = $values[$i]; }   
        $i++;

    } //end foreach
$buildData = '';
foreach ($dbdata as $key => $val) {
    if (empty($val)) {$buildData .= '`'.$key.'` = \'NULL\', ';} else {
    $buildData .= '`'.$key.'` = \''.$val.'\', ';}

}
$buildData = substr($buildData,0,-2);


    $dbaddr = DB_HOST;
    $dbusr = DB_USER;
    $dbpwd = DB_PASSWORD;
    $dbname = DB_DATABASE;
$prepareUpdate ='';
try {
  $db = new PDO('mysql:host='.$dbaddr .';dbname='.$dbname.';charset=UTF8', $dbusr, $dbpwd); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET CHARACTER SET utf8"); 

 $sqlqry = 'UPDATE '.$table.' SET '.$buildData.' WHERE `'.$indxfldnm.'` = \''.$where.'\';';

   $prepareUpdate = $db->exec($sqlqry); 
  //execute the update for one or many values
}
 catch(PDOException $e)
    {
    $e->getMessage();
    print_r($e);
    }


 return $sqlqry;


} 
//END: SQLUpdate