2014-12-07 92 views
0

的功能是非常简单的:PDO更新帮助执行PDO更新

的变量:$表的更新是发生 表和$字段在表中的字段, 和$生成的值从后并投入$值阵列 和$哪里是表 的索引字段的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不太熟悉并将其设置为 有点帮助,可以修复代码以将参数绑定到更新中的值 不胜感激

谢谢

回答

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; 
?> 
+0

我有多个值,它的构建,我需要的字段和值转换成可以重现“更新” sqlquery的办法多个字段。这是问题。我如何构建SQL?把它发送到准备课程? – 2014-12-07 15:25:14

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