Page 1 of 1

Problem with Insert query

Posted: Thu Aug 19, 2004 6:59 pm
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.

Posted: Thu Aug 19, 2004 7:02 pm
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());

Posted: Thu Aug 19, 2004 7:24 pm
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)

Posted: Thu Aug 19, 2004 7:26 pm
by TNIDBMNG
That should be INSERT not NSERT I accidently deleted the I when I was posting the reply

Posted: Thu Aug 19, 2004 7:30 pm
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());

Posted: Thu Aug 19, 2004 7:35 pm
by TNIDBMNG
That worked like a charm thanks so much? Do you mind me asking why adding that or die() made it work?

Posted: Thu Aug 19, 2004 7:38 pm
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..