Jump to content

Header function runs before update to database is made


matt_jo84

Recommended Posts

I have been having some problems with a part of my login script. This is the two lines of script I am having problems with:

 

mysql_query("UPDATE " . $userName. " SET login = '$loginIN' WHERE pass = '$password' AND course = '$courseID'");

header( 'url=http://www.somewebsite.com/logged_IN.htm' );

 

The problem is that when this php script is executed the second line executes before the first and an update to the database is not made. I also tried the script without the second line and the first works then. If anyone could help me out with this problem I would really appreciate it.

Link to comment
Share on other sites

You really should make sure the query is done before you execute the header() function:

<?php
$q = "UPDATE " . $userName. " SET login = '$loginIN' WHERE pass = '$password' AND course = '$courseID'";
$rs = mysql_query($q) or die("There was a problem with the query <pre>$q</pre><br>" . mysql_error());
header( 'url=http://www.somewebsite.com/logged_IN.htm' );
?>

 

Ken

Link to comment
Share on other sites

For debugging purposes, change the header() line to:

<?php
exit("The number of rows affected by the query <pre>$q</pre> was " . mysql_affected_rows($rs));
?>

If this reports that zero rows were affected, check your query to make sure it is logically correct.

 

Ken

Link to comment
Share on other sites

The problem is that when this php script is executed the second line executes before the first and an update to the database is not made.

 

Unless you've found an obscure bug within PHP, which I doubt, this is entirely impossible.  Code executes sequentially.  The interpretor doesn't arbitrarily decide to execute some lines, skip others, or randomly jump from location to location.

 

The reason I bring this up is that you seem to think you're doing everything correct and that PHP is the cause of the error.  The posts before mine are giving you advice on how to find the error that you made.

 

Slightly less than 100% of the time your code is not working, it's a mistake the programmer made.  Keep that in mind and your life will be much easier in terms of debugging.

Link to comment
Share on other sites

Hello roopurt18, I completely understand what you are saying, i'm am not assuming php is at fault which is the reason I am attempting to use a forum to see if I am in fact doing something wrong. I understand that code executes sequentially. I am simple telling you what I am experiencing, no assumptions are being made about PHP itself or that I am doing everything right because I am not an by any means an expert with PHP.

 

As for kenrbnsn:

I tried what you said and I get this "Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource", and then nothing in printed out for "mysql_affected_rows($rs)", but the right variables for update are shown and the correct update to the database is made since the header function was removed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.