Guest User

Untitled

a guest
Jan 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. $digital_fee = (isset ($_POST['digital_fee']) ? $_POST['digital_fee'] : '');
  2. $banner_1 = (isset ($_POST['banner_1']) ? 1 : 0);
  3.  
  4. $statement = $db->prepare("INSERT INTO $table
  5. (digital_fee, banner_1)
  6. VALUES
  7. (:digital_fee, :banner_1)
  8. ON DUPLICATE KEY UPDATE
  9. digital_fee=:digital_fee, banner_1=:banner_1");
  10.  
  11. $statement->bindParam(':arn_mkt_stn', $arn_mkt_stn);
  12. $statement->bindParam(':digital_fee', $digital_fee);
  13.  
  14. $statement->execute();
  15.  
  16. // example function to handle the PDO named params db inserts
  17.  
  18. $digital_fee ='500';
  19. $banner_1 = '0';
  20.  
  21. $arr_digital = array("digital_fee"=>$digital_fee, "banner_1"=>$banner_1);
  22. $table = 'myTable';
  23.  
  24. //create sql string with $vars
  25. $sql = "INSERT INTO $table (";
  26. foreach ($arr_digital as $key => $val){
  27. $sql.= "$key, ";
  28. }
  29.  
  30. $sql.= ") VALUES ( ";
  31. foreach ($arr_digital as $key => $val){
  32. $sql.= ":$key, ";
  33. }
  34.  
  35. $sql.= ") ON DUPLICATE KEY UPDATE ";
  36. foreach ($arr_digital as $key => $val){
  37. $sql.= "$key=:$key, ";
  38. }
  39. echo $sql;
  40.  
  41. $statement = $db->prepare($sql);
  42.  
  43.  
  44. $statement->bindParam(':arn_mkt_stn', $arn_mkt_stn);
  45. foreach ($arr_digital as $key => $val){
  46. $statement->bindParam(":$key, $val");
  47. }
  48.  
  49. $statement->execute();
  50.  
  51. Fatal error:
  52. Uncaught exception 'PDOException' with message
  53. 'SQLSTATE[42000]: Syntax error or access violation: 1064
  54. You have an error in your SQL syntax; check the manual that corresponds
  55. to your MySQL server version for the right syntax to use near
  56. ') VALUES ( '500', '0', ) ON DUPLICATE KEY UPDATE digital_fee='500', banner_1='0'' at line 1'
  57.  
  58. $digital_fee ='500';
  59. $banner_1 = '0';
  60.  
  61. $arr_digital = array("digital_fee"=>$digital_fee, "banner_1"=>$banner_1);
  62. $table = 'myTable';
  63.  
  64. //create sql string with $vars
  65. foreach ($arr_digital as $key => $val){
  66. $columns[]= $key;
  67. $vals [] = ':'.$key;
  68. $updates[]= "$key = :$key";
  69.  
  70. }
  71. $column = implode(', ', $columns);
  72. $values = implode(', ', $vals);
  73. $update = implode(', ', $updates);
  74.  
  75. $sql = "INSERT INTO $table ($column) VALUES ($values) ON DUPLICATE KEY UPDATE $update";
  76. echo $sql;
  77.  
  78. $statement = $db->prepare($sql);
  79.  
  80.  
  81. $statement->bindParam(':arn_mkt_stn', $arn_mkt_stn);
  82. foreach ($arr_digital as $key => $val){
  83. $statement->bindParam(":$key", $val);
  84. }
  85.  
  86. $statement->execute();
  87.  
  88. $comma = "";
  89. $sql = "INSERT INTO $table (";
  90. foreach ($arr_digital as $key => $val){
  91. $sql.= $comma . "$key ";
  92. $comma = ","
  93. }
  94.  
  95. $comma = "";
  96. $sql.= ") VALUES ( ";
  97. foreach ($arr_digital as $key => $val){
  98. $sql.= $comma . ":$key ";
  99. $comma = ","
  100. }
Add Comment
Please, Sign In to add comment