Insert Query trouble

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
cone13cone
Forum Newbie
Posts: 24
Joined: Fri Mar 20, 2009 8:32 pm

Insert Query trouble

Post by cone13cone »

This is probably stupid but why am i getting syntax errors with this insert query:

Code: Select all

$query = "INSERT INTO approved (
                        JID, add, subtract, description, date
                    ) VALUES (
                        '$jid', '$add', '$subtract', '$description', NOW()
                    )";
$result = mysql_query($query, $connection);
rabeehkm
Forum Newbie
Posts: 4
Joined: Fri May 29, 2009 6:34 am

Re: Insert Query trouble

Post by rabeehkm »

Hi cone13cone,

i actually didnt understand your problem, in which line u have syntax error???

Still try the following,
//create a page for connect(connect.inc.php) and put it in include folder.

Code: Select all

 
<?
 
$dbhost='localhost';
$dbusername='root';
$dbuserpass='';
$dbname='your-db-name';
@$link_id = mysql_pconnect($dbhost, $dbusername, $dbuserpass) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db($dbname,$link_id);
 
?>
 
In the main page,

Code: Select all

 
<?
include('connect.inc.php');
 
$query = "INSERT INTO approved (
                        JID, add, subtract, description, date
                    ) VALUES (
                        '$jid', '$add', '$subtract', '$description', date("Y-m-d")
                    )";
$result = mysql_query($query);
 
?>
 
For furthur queries mail me [redacted] - ask me here
--------
Rabeeh
Last edited by Benjamin on Fri May 29, 2009 11:00 am, edited 1 time in total.
Reason: Changed code type from text to php. Removed email address.
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: Insert Query trouble

Post by anand »

Try this

Code: Select all

$query = "INSERT INTO approved (
                        `JID`, `add`, `subtract`, `description`, `date`
                    ) VALUES (
                        '$jid', '$add', '$subtract', '$description', NOW()
                    )";
$result = mysql_query($query, $connection);
This will help you I guess.
cone13cone
Forum Newbie
Posts: 24
Joined: Fri Mar 20, 2009 8:32 pm

Re: Insert Query trouble

Post by cone13cone »

Database query failed: 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 'add, subtract, description, date ) VALUES ( '6482', '3', '0', 'hdfh' at line 2

This is the error I get.

Ive used insert queries plenty of times before with no trouble, which is why I'm not sure whats going wrong. If check my connection, echo'd back my $_POST and $_GET arrays and really don't know how else to approach it.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Insert Query trouble

Post by mikemike »

Try quoting all of your column and table names with backticks (`). 'add' is a MySQL reserved word, this means that you can use it but it must be quoted with backticks. It's good practice to backtick everything so that your applications are future-proof should MySQL choose to add reserved words at a later date.
Post Reply