Jump to content

[SOLVED] Insert syntax


psyqosis

Recommended Posts

Hi. I thought I understood this, but I keep getting an error. Can anyone help? Thanks.

 

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 'desc) VALUES ('test1', 'test2', 'test3')' at line"

 

CODE (I'm using 3 form inputs to insert data via submit button):

 

<?php

$database = "edit" or die(mysql_error());

$db = mysql_connect("edit", "edit", "edit") or die(mysql_error());

mysql_select_db($database, $db) or die(mysql_error());

?>

 

<?php

  if($add == 1) {

  $sql = "INSERT INTO events (title, date, desc) VALUES ('$title', '$date', '$desc');";

  mysql_query($sql, $db) or die(mysql_error()); //error comes from here

?>

<h3>Record added to database. Thanks.</h3> Add another record by filling in the form below or return to the main menu by

<a href="index.php">clicking here</a>.

<?php

    }

?>

 

<form method=post action="<?php echo $PHP_SELF ?>" enctype="multipart/form-data">

<h3> Create New Record </h3>

<center>

<table width=400 cellpadding=0 cellspacing=0 border=0>

  <tr><td align=right nowrap>Product Title:&nbsp</td><td><input size=70 type="text" name="title"></td></tr>

  <tr><td align=right nowrap>Date:&nbsp</td><td><input size=70 type="text" name="date"></td></tr>

  <tr><td align=right nowrap>Description:&nbsp</td><td><input size=70 type="text" name="desc"></td></tr>

  <tr><td colspan=2 align=right><input type=hidden name="add" value="1"><input type="submit" value="Submit Record"></td></tr>

</table>

Link to comment
Share on other sites

i think this:

 

<?php 
  if($add == 1) {
  $sql = "INSERT INTO events (title, date, desc) VALUES ('$title', '$date', '$desc');";
  mysql_query($sql, $db) or die(mysql_error()); //error comes from here
?>

 

should be:

 

<?php 
  if($add == 1) {
  $sql = "INSERT INTO events (title, date, desc) VALUES ('$title', '$date', '$desc')";
  mysql_query($sql, $db) or die(mysql_error()); //error comes from here
?>

Link to comment
Share on other sites

The word "date" is a reserved word in MySql. If you use a reserved word as a table or column name, you need to enclose it in backticks ( ` ):

<?php
$sql = "INSERT INTO events (title, `date`, desc) VALUES ('$title', '$date', '$desc')";
$rs =  mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_error());
?>

 

Ken

Link to comment
Share on other sites

I think desc is a reserved word in mysql

 

one of teh reasons I always use the back tick in my queries - even if they are not 'nessesscary'...

 

try this

 

$sql = "INSERT INTO `events` (`title`, `date`, `desc`) VALUES ('$title', '$date', '$desc')";

 

and make sure that the data you are adding fits the expected values of the fields in your database.

 

kenrbnsn - its date is it?? I would remember but as I always use backticks I never have this problem!! ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.