[SOLVED] INSERT query not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

[SOLVED] INSERT query not working

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Post by dwfait »

I still get could not insert item into database :(
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

'by' is a mysql reserved word, so either rename that column or backquote it, `by` (i'd rename it ;))
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Post by dwfait »

thanks, i changed the field to author and it works :)
Post Reply