2011-07-19 2 views
0

I働き、次のコードを持っているが、私が通常使用するものであるPDOを使用していない:パラメータ化されたPDOを使用して列と値を追加できますか?

<?php 
    if($_SERVER['REQUEST_METHOD'] == 'POST') { 

     // will go all the way upto 50 
     $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4'); 

     $update = ''; 

     foreach($fields as $dbfield => $field) { 
      if ($update) $update.= ','; 

      $update.= ' '.$dbfield.'='; 

      if (isset($_POST[$field])) { 
       $update.= 1; 
      } else { 
       $update.= 0; 
      } 
     } 

     // show generated query 
     echo 'UDPATE table SET'.$update.' WHERE 1=1'; 
    } 
?> 

<html> 
    <head> 
     <title></title> 
    </head> 

    <body> 
     <form method="post"> 
      <input type="checkbox" name="cb1" /> 
      <input type="checkbox" name="cb2" /> 
      <input type="checkbox" name="cb3" /> 
      <input type="checkbox" name="cb4" /> 
      <!-- all the way to 50 --> 

      <input type="submit" value="submit" /> 
     </form> 
    </body> 
</html> 

私は上記のコードから、更新するcolumn namescolumn valuesを取得していますので、私はわかりませんどのようにPDOを使用してこれを行うには?

私は下に次のコード実行した:

<?php 
    if($_SERVER['REQUEST_METHOD'] == 'POST') { 

     $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4'); 

     $update = ''; 

     foreach($fields as $dbfield => $field) { 
      if ($update) $update.= ','; 

      $update.= ' '.$dbfield.'='; 

      if (isset($_POST[$field])) { 
       $update.= 1; 
      } else { 
       $update.= 0; 
      } 
     } 

     $DBH = new PDO("mysql:host=localhost;dbname=database", "user", "pass"); 
     $DBH -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     $STH = $DBH -> prepare("update table1 set :update where id = :id"); 

     $STH -> bindParam(':update', $update, PDO::PARAM_STR, 255); 
     $STH -> bindParam(':id', $id, PDO::PARAM_INT, 4); 

     $STH -> execute(); 
    } 
?> 

<html> 
    <head> 
     <title></title> 
    </head> 

    <body> 
     <form method="post"> 
      <input type="checkbox" name="cb1" /> 
      <input type="checkbox" name="cb2" /> 
      <input type="checkbox" name="cb3" /> 
      <input type="checkbox" name="cb4" /> 
      <!-- all the way to 50 --> 

      <input type="submit" value="submit" /> 
     </form> 
    </body> 
</html> 

をしかし、それは私に次のエラー与える:

[Tue Jul 19 09:15:44 2011] [error] [client ::1] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' db_field1=0, dbfield2=1, dbfield3=1, dbfield4=0' where id = 1' at line 1' in /var/www/page1.php:30\nStack trace:\n#0 /var/www/page1.php(30): PDOStatement->execute()\n#1 {main}\n thrown in /var/www/page1.php on line 30, referer: http:// localhost/page1.php

+0

それを試してみて、あなたの結果を投稿? –

+0

私はそれを試しました。私が得るエラーを見てください。 – oshirowanen

+0

デバッグしてください。構文エラーです。あなたの質問を出力して、あなたのエラーを見つけてください。 –

答えて

0

用途:

$DBH = new PDO("mysql:host=localhost;dbname=database", "user", "pass"); 
$DBH -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$STH = $DBH -> prepare("update table1 set " . $update . " where id = :id"); 

//$STH -> bindParam(':update', $update, PDO::PARAM_STR, 255); 
$STH -> bindParam(':id', $id, PDO::PARAM_INT, 4); 

$STH -> execute(); 

の代わり:

$DBH = new PDO("mysql:host=localhost;dbname=database", "user", "pass"); 
$DBH -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$STH = $DBH -> prepare("update table1 set :update where id = :id"); 

$STH -> bindParam(':update', $update, PDO::PARAM_STR, 255); 
$STH -> bindParam(':id', $id, PDO::PARAM_INT, 4); 

$STH -> execute(); 

それとも、これを使用することができます:

if($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4'); 

    $update = array(); 
    $values = array(); 

    foreach($fields as $dbfield => $field) { 
     $update[] = $dbfield . " = ? "; 
     $values[] = isset($_POST[$field]) ? 1 : 0; 
    } 

    $values[] = $id; 

    $DBH = new PDO("mysql:host=localhost;dbname=database", "user", "pass"); 
    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    $STH = $DBH->prepare("update table1 set " . join(',', $update) . " where id = ?"); 

    $STH->execute($values); 
} 

の代わり:

if($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $fields = array('db_field1'=>'cb1', 'dbfield2'=>'cb2', 'dbfield3'=>'cb3', 'dbfield4'=>'cb4'); 

    $update = ''; 

    foreach($fields as $dbfield => $field) { 
     if ($update) $update.= ','; 

     $update.= ' '.$dbfield.'='; 

     if (isset($_POST[$field])) { 
      $update.= 1; 
     } else { 
      $update.= 0; 
     } 
    } 

    $DBH = new PDO("mysql:host=localhost;dbname=database", "user", "pass"); 
    $DBH -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    $STH = $DBH -> prepare("update table1 set :update where id = :id"); 

    $STH -> bindParam(':update', $update, PDO::PARAM_STR, 255); 
    $STH -> bindParam(':id', $id, PDO::PARAM_INT, 4); 

    $STH -> execute(); 
} 
+0

これは、PDOの使用の全目的を破るものではありませんか? – oshirowanen

+0

いいえ、そうではありません。あなたができることのバリエーションを1つ追加します。 –

+0

第2版を確認する –

関連する問題