Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $digital_fee = (isset ($_POST['digital_fee']) ? $_POST['digital_fee'] : '');
- $banner_1 = (isset ($_POST['banner_1']) ? 1 : 0);
- $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();
- // 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'
- $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();
- $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 = ","
- }
Add Comment
Please, Sign In to add comment