Query issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
182x
Forum Newbie
Posts: 3
Joined: Fri Nov 17, 2006 8:50 am

Query issue

Post 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);
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

Query

Post 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')";
182x
Forum Newbie
Posts: 3
Joined: Fri Nov 17, 2006 8:50 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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());
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

Query

Post 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]
Post Reply