what is wrong with this code?

Reply

Join Date: Nov 2008
Posts: 74
Reputation: sacarias40 is an unknown quantity at this point 
Solved Threads: 0
sacarias40's Avatar
sacarias40 sacarias40 is offline Offline
Junior Poster in Training

what is wrong with this code?

 
0
  #1
Jan 11th, 2009
i get this error:
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 'index, user_id, username, message) VALUES(' ', '4', 'zack', 'jh')' at line 1
  1. <?php
  2.  
  3. session_start();
  4.  
  5. $_SESSION['user_id'] = 4;
  6.  
  7. $user_id = $_SESSION['user_id'];
  8.  
  9. $_SESSION['username'] = "zack";
  10.  
  11. $username = $_SESSION['username'];
  12.  
  13. $message = $_POST['shout_mes'];
  14.  
  15. $insert = $_POST['shout'];
  16.  
  17.  
  18. if(!empty($message) && $insert = "shout"){
  19.  
  20. include('mysqlcon.php');
  21.  
  22. $select = mysql_select_db($db, $con);
  23. if(!$select){
  24. die('could not select db: ' . mysql_error());
  25. }
  26.  
  27.  
  28. $sql = mysql_query("INSERT INTO shoutbox (index, user_id, username, message) VALUES(' ', '$user_id', '$username', '$message')");
  29.  
  30. if(!$sql){
  31. echo "error" . mysql_error();
  32. }ELSE{
  33. header("location: index.php");
  34. }
  35.  
  36. }ELSE{
  37.  
  38. echo "there was an error: " . mysql_error();
  39. }
  40.  
  41. ?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: what is wrong with this code?

 
0
  #2
Jan 11th, 2009
'index' is a reserved word. You must name that column something else.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 74
Reputation: sacarias40 is an unknown quantity at this point 
Solved Threads: 0
sacarias40's Avatar
sacarias40 sacarias40 is offline Offline
Junior Poster in Training

Re: what is wrong with this code?

 
0
  #3
Jan 11th, 2009
ok here is what worked.

  1. $sql = mysql_query("INSERT INTO shoutbox (user_id, username, message) VALUES('$user_id', '$username', '$message')");
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 190
Reputation: mwasif is an unknown quantity at this point 
Solved Threads: 25
mwasif mwasif is offline Offline
Junior Poster

Re: what is wrong with this code?

 
0
  #4
Jan 12th, 2009
It is not a good habit to use reserve words as column or table names. But if you still need to use that then simply enclose the column name in backticks (`).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC