Problem with Insert query

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
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Problem with Insert query

Post by TNIDBMNG »

I have the following query in my PHP application..

Code: Select all

$sql="INSERT INTO tblITPriorities (categoryId, description, assignedTo, date) SELECT ".$category.", ".$task.", ".$assignedto.", ".$date.";";
For some reason it is not working. I don't get any errors but nothing inserts.

categoryID is a INT
description is varchar
assignedTo is a INT
date is a datetime

I checked the values of my variables and they are all fine. Can anyone see what's wrong with this? I'm pretty new to PHP & MySQL.

Thanks in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$sql = "INSERT INTO tblITPriorities (categoryId, description, assignedTo, date) VALUES('$category','$task','$assignedTo','$date')";

btw, you won't get any errors unless you ask to get them from mysql:

Code: Select all

$query = mysql_query($sql) or die(mysql_error());
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Post by TNIDBMNG »

I do have this after I just didn't post it because I wasn't receiving the error there.

Code: Select all

<?php
mysql_query($sql,$cnx);
?>
I changed my code to what you said but it's still not working.

Code: Select all

<?php
$sql="INSERT INTO tblITPriorities (categoryID, description, assignedTo, date) VALUES (".$category.",".$task.",".$assignedto.",".$date.");";
?>
If I view what $sql represents I get this
NSERT INTO tblITPriorities (categoryID, description, assignedTo, date) VALUES (2,test,4,2004-08-19)
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Post by TNIDBMNG »

That should be INSERT not NSERT I accidently deleted the I when I was posting the reply
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try copying my query string exactly as-is and add "or die(mysql_error())" or similar to your mysql_query() call like this:

Code: Select all

mysql_query($sql,$cnx) or die(mysql_error());
TNIDBMNG
Forum Newbie
Posts: 21
Joined: Wed Aug 18, 2004 12:22 pm

Post by TNIDBMNG »

That worked like a charm thanks so much? Do you mind me asking why adding that or die() made it work?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

adding the die didn't make it work.. it just makes sure that if there is an error, you see it.

the reason yours was failing was this:

VALUES (2,test,4,2004-08-19)

test is a string, but mysql would think it's a variable/keyword/database/table/column/alias name.. and the date would get mangled too. Strings need to be in quotes of some form or another (either single or double quotes) so the 'test' and date need quoting.. My query has those things..
Post Reply