DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   MySQL (http://www.daniweb.com/forums/forum126.html)
-   -   what is wrong with this code? (http://www.daniweb.com/forums/thread167750.html)

sacarias40 Jan 11th, 2009 5:07 pm
what is wrong with this code?
 
i get this error:
Quote:

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
<?php

session_start();

$_SESSION['user_id'] = 4;

$user_id = $_SESSION['user_id'];

$_SESSION['username'] = "zack";

$username = $_SESSION['username'];

$message = $_POST['shout_mes'];

$insert = $_POST['shout'];


if(!empty($message) && $insert = "shout"){

include('mysqlcon.php');

$select = mysql_select_db($db, $con);
if(!$select){
die('could not select db: ' . mysql_error());
}


$sql = mysql_query("INSERT INTO shoutbox (index, user_id, username, message) VALUES(' ', '$user_id', '$username', '$message')");

if(!$sql){
echo "error" . mysql_error();
}else{
header("location: index.php");
}

}else{

echo "there was an error: " . mysql_error();
}

?>

Fest3er Jan 11th, 2009 7:51 pm
Re: what is wrong with this code?
 
'index' is a reserved word. You must name that column something else.

sacarias40 Jan 11th, 2009 11:00 pm
Re: what is wrong with this code?
 
ok here is what worked.

$sql = mysql_query("INSERT INTO shoutbox (user_id, username, message) VALUES('$user_id', '$username', '$message')");

mwasif Jan 12th, 2009 7:33 am
Re: what is wrong with this code?
 
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 (`).


All times are GMT -4. The time now is 1:15 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC