【问题标题】:PDO Update Help executing pdo updatePDO 更新 帮助执行 pdo 更新
【发布时间】:2014-12-08 00:15:21
【问题描述】:

函数非常简单:

变量:$table 是正在更新的表 $fields 是表中的字段, 并且 $values 从帖子生成并放入 $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 和设置它 修复代码以将参数绑定到更新中的值的一些帮助将 不胜感激

谢谢

【问题讨论】:

    标签: php mysql pdo


    【解决方案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;
    ?>
    

    【讨论】:

    • 我有多个值和它构建的多个字段,我需要将字段和值转换为可以重现“更新”sqlquery 的方式。这就是问题所在。我如何构建sql?将其发送到准备课程?
    【解决方案2】:

    将代码更改为不同的构建:

    这消除了多值问题

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 2018-05-12
      • 2013-02-25
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 2011-04-15
      相关资源
      最近更新 更多