Page 1 of 1

Query issue

Posted: Fri Nov 17, 2006 8:54 am
by 182x
Hey guys

I have created the following two queries the first one works fine however the second (insert query) )doesn't. No error message is supplied the data just doesn't enter the table. Any advice would be great thanks.

Code: Select all

$query="UPDATE cu SET curr ='".$HTTP_SESSION_VARS['curr]."' WHERE Id = '".$HTTP_SESSION_VARS['Id']."'";

mysql_query($query, $link_id);
		
$query2="INSERT INTO acc VALUES('','$tha', '$dep', '$HTTP_SESSION_VARS[Id]')";

mysql_query($query2, $link_id);

Query

Posted: Fri Nov 17, 2006 8:56 am
by timclaason
For Query2, you may want to check to see if the first column you're inserting into is required (ie cannot be null).
Otherwise, you could reframe the query like this:

Code: Select all

$query2 = "INSERT INTO table (field1, field2, field3) VALUES ('$value1', '$value2', '$value3')";

Posted: Fri Nov 17, 2006 9:00 am
by 182x
hi yes the first field is required its an id field which is an autonumber. Is the code incorrect in any other way?

Thanks.

Posted: Fri Nov 17, 2006 9:06 am
by volka
182x wrote:No error message is supplied the data just doesn't enter the table.
That's because you don't have any error handling in your script.
try

Code: Select all

$query2="INSERT INTO acc VALUES('','$tha', '$dep', '$HTTP_SESSION_VARS[Id]')";

echo '<div>Debug: ', htmlentities($query2), "</div>\n";
mysql_query($query2, $link_id) or die(mysql_error());

Query

Posted: Fri Nov 17, 2006 9:18 am
by timclaason
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


You don't have to put the autonumber field in the query
I've always found it better to do:

Code: Select all

$query = "INSERT INTO table (field1, field2, field3) VALUES ('$value1', '$value2', '$value3')";
And yeah, you should put an or die() at the end of your mysql_query.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]