Page 1 of 1
[SOLVED] INSERT query not working
Posted: Thu Aug 12, 2004 3:52 am
by dwfait
This code does not seem to work :-\
Code: Select all
$link = mysql_connect("localhost", "root", "")
or die("Could not connect");
mysql_select_db("stormst_sspp")
or exit("Could not select database");
$query="INSERT INTO news SET id='',by='$sfname',date='$date',title='$title',cat='$cat',summary='$summary'";
if (mysql_query($query)) {
echo "Item entered into database.<P>";
} else {
echo "Item could not be inserted into database. <P>";
}
mysql_close($link);
Am i missing something? all that comes out is Item could not be inserted into database.
Posted: Thu Aug 12, 2004 3:59 am
by CoderGoblin
Your insert has the wrong syntax. should be:
Code: Select all
"INSERT INTO news
(id, by,date,title, cat,summary)
VALUES
('','$sfname','$date','$title','$cat','$summary') "
More likely combination to include defaults (id should be serial for MySQL or default to nextval('seq') for postgres) would be:
Code: Select all
"INSERT INTO news
(by,date,title, cat,summary)
VALUES
('$sfname','$date','$title','$cat','$summary') "
Note: any column with a known default need not be placed in the code. If the date is the current date and time for instance the date column could default to "now()" and you could also leave that out of the insert.
Posted: Thu Aug 12, 2004 5:06 am
by dwfait
I still get could not insert item into database

Posted: Thu Aug 12, 2004 5:08 am
by markl999
Follow the 3 tips at the top of
viewtopic.php?t=17096
The problem is probably register_globals, i.e you're using $sfname instead of $_POST['sfname'] (if the variables are coming from a form post). The 3 tips above should highlight where the problem is.
Posted: Thu Aug 12, 2004 5:40 am
by dwfait
All the variables are right, i checked them.
When i put in the or die() statement, i get:
Code: Select all
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 'by,date,title, cat,summary) VALUES ('','dwain','August 12 2
Posted: Thu Aug 12, 2004 5:41 am
by markl999
'by' is a mysql reserved word, so either rename that column or backquote it, `by` (i'd rename it

)
Posted: Thu Aug 12, 2004 5:52 am
by dwfait
thanks, i changed the field to author and it works
