Jump to content

error when inserting data into MySQL


retro

Recommended Posts

Firstly, please accept my apologies if I have made a glaringly obvious error.  I am a newcomer to PHP and it is all quite overwhelming to me still! 

 

I have a form with various inputs, with an action of submit.php

 

The code for submit.php is as follows:

 

<?php
include("dbinfo.inc.php");
$connection = mysql_connect($server,$username,$password);

if (!$connection){
echo mysql_errno().": ".mysql_error()."<br/>";
exit;
}

if(!mysql_select_db($database)){
echo("Database not found<br>");
}

//retrieve post data
$baen = $_POST['baen'];
$CustID = $_POST['CustID'];
$DelDate = $_POST['DelDate'];

//insert order data into database
$query = "INSERT INTO 'orders' (customer,order,date)
VALUES
(
'" . $CustID . "',
'" . $baen . "',
'" . $DelDate . "'
)";

$result = mysql_query ($query) or die("SQL Error: " . mysql_error());
mysql_close();
?>

 

I know that the post data is working fine, and is giving the correct results.  However, I get the following error:

 

SQL 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 ''orders' (customer,order,date) VALUES ( '2', '2', '2008-04-30'

(and yes, it is missing the end bracket like that)

 

I am using MySQL 4.  The table is called orders.  It has the following fields:

 

ID (type: int10, attributes: unsigned, null: no, extra: auto_increment)

customer (type: int10, null: no, default: 0)

order (type: text, null: no)

date (type: date, null: no, default: 0000-00-00)

 

I have used pretty much entirely the same code before with a different form and table, and it worked fine.  I have looked in the PHP manual, and couldn't ascertain the problem.

 

Can anyone tell me what I'm doing wrong?

 

Many thanks in advance - any assistance would be greatly appreciated!

Link to comment
Share on other sites

You shouldn't be placing quotes around the table name. Ether use nothing, or backticks(`)

 

You'll need to use backticks if the table name or field name is a reserved word (see: http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html) - though it's usually better not to name your fields/tables with these words in the first place.

Link to comment
Share on other sites

The fastest way to debug a SQL query that you use in PHP is to 'echo' the query and then try using that in your SQL client ( eg phpmyadmin )

 

You are quoting your table name 'orders' with single quotes. I don't know if that is right or not. Try it with out quotes, or if you do want to quote it use the backtick('`') char.

Link to comment
Share on other sites

Ahh, the quotes around the table name was an accident!  I had the table without the quotes whatsoever.  I had read elsewhere on the forum that there are certain reserved words that would need quotes, so I tried that.

 

I took the quotes out, and it gave me the full error once again (including the closing bracket and after that the line number).

 

I had no idea what the reserved words were, or that they were reserved for field names in tables, too.  It turns out that the problem was in my use of 'order' as a field name.  It all works fine now!

 

Thanks for the help there, guys!  I certainly didn't expect such a quick response, or to get this working tonight!  I am most impressed!

 

Thanks also for the debugging tip, dptr1988.. I'll certainly keep that in mind!

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.