942,500 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1083
  • PHP RSS
Jun 25th, 2010
0

PHP/MySql insert

Expand Post »
Hey,

I am having trouble with some code I am using. I have used this same code on a different site but it doesnt seem to work now on this site.
I am trying to insert something into my database. however I keep getting this error:
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 'desc, story, cost, rrp, wt, size, top, fashion, active, lifestyle, sale) VALUES ' at line 1

My Code is below (sorry there is a bit of it, but I don't know what part is giving me issues, as line 1 isn't reffering to my code?)

PHP Syntax (Toggle Plain Text)
  1.  
  2. $OK = false;
  3. $done = false;
  4.  
  5. if (array_key_exists('publish', $_POST)) {
  6.  
  7. try {
  8. $id = NULL;
  9. $stock = $_POST['stock'];
  10. $brand = $_POST['brand'];
  11. $name = $_POST['name'];
  12. $code = $_POST['code'];
  13. $desc = $_POST['desc'];
  14. $story = $_POST['story'];
  15. $rrp = $_POST['rrp'];
  16. $cost = $_POST['cost'];
  17. $weight = $_POST['wt'];
  18. $size = $_POST['size'];
  19. $top = $_POST['top'];
  20. $fashion = $_POST['fashion'];
  21. $active = $_POST['active'];
  22. $lifestyle = $_POST['lifestyle'];
  23. $sale = $_POST['sale'];
  24.  
  25. /*** connect to db ***/
  26. $conn = dbConnect('admin');
  27. /*** set the error mode ***/
  28. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  29.  
  30. /*** our sql query ***/
  31. $done = $conn->prepare("INSERT INTO product_info (id, stock, brand, name, code, desc, story, cost, rrp, wt, size, top, fashion, active, lifestyle, sale) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  32.  
  33. /*** bind the params ***/
  34. $done->bindParam(1, $id);
  35. $done->bindParam(2, $stock);
  36. $done->bindParam(3, $brand);
  37. $done->bindParam(4, $name);
  38. $done->bindParam(5, $code);
  39. $done->bindParam(6, $desc);
  40. $done->bindParam(7, $story);
  41. $done->bindParam(8, $rrp);
  42. $done->bindParam(9, $cost);
  43. $done->bindParam(10, $weight);
  44. $done->bindParam(11, $size);
  45. $done->bindParam(12, $top);
  46. $done->bindParam(13, $fashion);
  47. $done->bindParam(14, $active);
  48. $done->bindParam(15, $lifestyle);
  49. $done->bindParam(16, $sale);
  50.  
  51. /*** execute the query ***/
  52. $done->execute();
  53. }
  54. catch(Exception $e) {
  55. echo '<h4>'.$e->getMessage().'</h4>';
  56. }
  57. }
  58. if ($done) {
  59. header ("refresh:10;url=index.php");
  60. echo 'Sending...........................<br />';
  61. exit;
  62. }
  63. // display error message if query fails
  64. if (isset($update) && !$OK && !$done) {
  65. // $message[] = "There was a problem updating the team, please try again or contact Q Web Development";
  66. $error = $update->errorInfo();
  67. if (isset($error[2])) {
  68. echo $error[2];
  69. }
  70. }

If your able to point out what I've done wrong, or how I can fix it, I would greatly appreciate it.

Cheers,

Qwaz
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
QWaz is offline Offline
62 posts
since Apr 2010
Jun 25th, 2010
0
Re: PHP/MySql insert
Try replacing line 31 with the following.
PHP Syntax (Toggle Plain Text)
  1. $done = $conn->prepare("INSERT INTO product_info (id, stock, brand, name, code, desc, story, cost, rrp, wt, size, top, fashion, active, lifestyle, sale) VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')");
That is assuming all of the columns are strings. If any of the column types are integers then replace the value of the column with the number 0 and no quotes for integer columns. (By the way integer==number).
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,003 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Multiple Columns in PHP results
Next Thread in PHP Forum Timeline: exchange rates





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC