2014-12-07 129 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