Web Hosting Talk







View Full Version : error in sql syntax when posting form data


kath421
05-04-2005, 06:39 AM
As part of a university assignment, I have to create a HTML form which will update a MySQL database. The code I'm working with at the moment is below. I've just started with PHP/MySQL etc, so I'm sorry for my cluelessness.

Here's the code from my ADD EVENT page


<?
include ("admin/connect.php");

if(isset($_POST['added']))
{
$event_add = "INSERT INTO $event VALUES('$date','$time','$venue','$cover','$artist','')";
mysql_query($event_add) or die(mysql_error());

print('Details added to the event calendar');
}
else
{
?>
<form name="add" action='<?php echo $_SERVER['PHP_SELF'] ?>' method="POST">
<b>Date: </b> <input type="text" name="date" size=40> <BR>
<b>Time:</b> <input type="text" name="time" size=40><BR>
<b>Venue: </b> <input type="text" name="venue" size=40><BR>
<b>Cover: </b> <input type="text" name="cover" value="$" rows=5 cols=40><BR>
<b>Artist: </b> <input type="text" name="artist" rows=5 cols=40><BR>
<p><input type="submit" value="Submit"></p>
<input type= 'hidden' name= 'added' value= 'set' >

</form>
<?
}
?>


this is my EVENT table structure


event_id int(11) Not null auto_increment primary key
event_date date Not null default: 0000-00-00
event_time varchar(5) Not null
event_venue varchar(30) Not null
event_cover varchar(4) Not null
artist_name varchar(10) Not null


I'm getting the following error when I submit the form data:


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('2005-12-12','10pm','rics','5','minors','')' at line 1



I truly have no idea what this means. Could it be something to do with the formatting of the date? I've tried googling the error, but the results were all fairly useless.

Insert_Name_Here
05-04-2005, 07:12 AM
syntax for insert in this case is:

INSERT into table_name (column1, column2....)
values (value1, value2...)


So you should have something like:

INSERT into event (event_date, event_time....) values ('$date', '$time',...)

http://dev.mysql.com/doc/mysql/en/insert.html

error404
05-04-2005, 07:27 AM
The column list is optional, omiting it is acceptable, just somewhat silly in case the order of columns in the database ever changes..

I suspect the problem lies in that your input data columns are in the wrong order. They need to be in the same order as the database schema (unless you specify the columns you want to update as the above poster suggests).

Insert_Name_Here
05-04-2005, 07:33 AM
The primary key is auto_increment. Column list is not optional, unless you want to generate the primary key every time.....

Burhan
05-04-2005, 07:34 AM
In other words, change your VALUES part to VALUES ('','$date',...

The first '' is for the event_id, which you omitted in your code.

Insert_Name_Here
05-04-2005, 07:46 AM
That is dreadful programming practice. If this was code that needed to be worked on by a group or team nobody would have any clue what was being inserted where unless they had a db schema.

lauriate
05-04-2005, 09:11 AM
Problem is "INSERT INTO $event"

Should be "INSERT INTO event"

event is the table name