0

我试图减少将大量不同的 $_POST 数据插入数据库所需的 PHP 代码量。我目前正在使用带有命名参数的 PDO,并且每次都手动列出它们。也许有比我在示例中尝试的更清洁/更好的方法,但是,我处于无法重构我已经列出的 100 个捕获变量的位置,如下所示:

$digital_fee = (isset ($_POST['digital_fee']) ? $_POST['digital_fee'] : '');    
$banner_1 = (isset ($_POST['banner_1']) ? 1 : 0);

剩下的 PHP - SQL 是这样写的:

$statement = $db->prepare("INSERT INTO $table
                            (digital_fee, banner_1)
                            VALUES
                            (:digital_fee, :banner_1)
                            ON DUPLICATE KEY UPDATE
                            digital_fee=:digital_fee, banner_1=:banner_1");

$statement->bindParam(':arn_mkt_stn', $arn_mkt_stn);
$statement->bindParam(':digital_fee', $digital_fee);

$statement->execute();

所以我正在测试类似下面的东西,我想把它变成一个类/函数,并调用它传入现有的 assoc 数组,例如 myFunction($db, $table, $arr_digital);

// example function to handle the PDO named params db inserts

$digital_fee ='500';
$banner_1 = '0';

$arr_digital = array("digital_fee"=>$digital_fee, "banner_1"=>$banner_1);   
$table = 'myTable';

//create sql string with $vars
$sql = "INSERT INTO $table (";  
foreach ($arr_digital as $key => $val){
    $sql.= "$key, ";    
}

$sql.= ") VALUES ( ";
foreach ($arr_digital as $key => $val){
    $sql.= ":$key, ";
}

$sql.= ") ON DUPLICATE KEY UPDATE ";    
foreach ($arr_digital as $key => $val){ 
    $sql.= "$key=:$key, ";  
}   
echo $sql;

$statement = $db->prepare($sql);


$statement->bindParam(':arn_mkt_stn', $arn_mkt_stn);
foreach ($arr_digital as $key => $val){
         $statement->bindParam(":$key, $val");
}

$statement->execute();

运行它会返回一个我认为与格式相关的错误(希望如此) - 这是消息,感谢收到任何指针,抱歉,这是一篇很长的帖子。

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 
     ') VALUES ( '500', '0', ) ON DUPLICATE KEY UPDATE digital_fee='500', banner_1='0'' at line 1'

富有的。

4

2 回答 2

1

检查您的 SQL 语句。即使不需要,您也总是添加逗号。尝试这样的事情:

$comma = "";
$sql = "INSERT INTO $table (";  
foreach ($arr_digital as $key => $val){
    $sql.= $comma . "$key ";    
    $comma = ","
}

$comma = "";
$sql.= ") VALUES ( ";
foreach ($arr_digital as $key => $val){
    $sql.= $comma . ":$key ";
    $comma = ","
}

这不会在第一次运行时添加任何内容,但随后的迭代将在变量前添加一个逗号。

于 2012-09-26T16:01:48.360 回答
0
    $digital_fee ='500';
    $banner_1 = '0';

    $arr_digital = array("digital_fee"=>$digital_fee, "banner_1"=>$banner_1);   
    $table = 'myTable';

    //create sql string with $vars
    foreach ($arr_digital as $key => $val){
        $columns[]= $key;  
        $vals [] = ':'.$key;
        $updates[]= "$key = :$key";

    }
    $column = implode(', ', $columns);
    $values = implode(', ', $vals);
    $update = implode(', ', $updates);

    $sql = "INSERT INTO $table ($column) VALUES ($values) ON DUPLICATE KEY UPDATE $update";  
    echo $sql;

    $statement = $db->prepare($sql);


    $statement->bindParam(':arn_mkt_stn', $arn_mkt_stn);
    foreach ($arr_digital as $key => $val){
            $statement->bindParam(":$key", $val);
    }

    $statement->execute();

立即尝试..应该没问题

于 2012-09-26T16:13:53.747 回答