Forum Moderators: coopster

Message Too Old, No Replies

Trying to Add an Additional Query After Else Statement

         

capulet_x

3:34 am on Mar 27, 2007 (gmt 0)

10+ Year Member



Hello, everyone...
I was hoping I could get an extra pair of eyes on the piece of code. This is just an excerpt because the code up to the line:

header("Location: ../PRODUCT_CONNECT.php");

works perfectly. Basically its a simple logon page which echos an error if the logon is wrong or ELSE does the code in the excerpt. So there are two queries in this form, the one (not shown) which checks the validity of the ussername and password and the one shown below which I hope will eventually (with someone's help..) SELECT two field from my database and set the results to a cookie which I can apply on the page PRODUCT_CONNECT. php. For testing I had disabled the line which redirects the user to PRODUCT_CONNECT.php so that I could check the echo results...
The problem is I don't get any echo results. In fact, I don't get any errors either. It's like the query doesn' exist.

/////////////code excerpt- everything above works perfectly///////////////

} else {
mysql_free_result($rsLogin);
setCookie("cookie77",$HTTP_POST_VARS['username'],time() + 86400,"/");
// Expire Date: 1
// Expire Time: 0
header("Location: ../PRODUCT_CONNECT.php");
/////// THE QUERY BELOW DOESN'T SEEM TO BE WORKING....
$query = "SELECT item, warranty FROM `db-1` WHERE username= '%s' AND access = '%s'";
$result = mysql_query($query)or die(mysql_error());
while (list($item,$warranty) = mysql_fetch_row($result)){
$profile = $item+":"+$warranty;
setCookie("artistconn",$HTTP_POST_VARS['$profile'],time() + 86400,"/");
echo "$result, $profile";

}

//////-code excerpt- below is just a form//////////

If the cookie thing doesn't work can someone please guide me on a way to initiate my second query based on the results of the login and maybe post the results to my following page PRODUCT_CONNECT.

As always any assistance is greatly appreciated.

Thank you.

cameraman

5:43 am on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This line:
header("Location: ../PRODUCT_CONNECT.php");

leaves the current script, redirecting to that page.
Try moving it to the blank line below the echo.

capulet_x

5:55 am on Mar 27, 2007 (gmt 0)

10+ Year Member



It doesn't show in my example but I did disable it...yet my query is still "invisible" to the script. No echo ... no error.

mcibor

7:53 am on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Capulet and welcome to Webmasterworld!

1. Try not to use $HTTP_POST_VARS, as this method is depreciated. Use $_POST instead.
2. You shouldn't be using username= '%s', it's possible only in print, not in mysql_query

} else {
mysql_free_result($rsLogin);
setCookie("cookie77",$_POST['username'],time() + 86400,"/");
// Expire Date: 1
// Expire Time: 0
//header("Location: ../PRODUCT_CONNECT.php");
/////// THE QUERY BELOW DOESN'T SEEM TO BE WORKING....
$query = "SELECT item, warranty FROM `db-1` WHERE username='$username' AND access='$access'";
$result = mysql_query($query)or die("Error: ".mysql_error());
while (list($item,$warranty) = mysql_fetch_row($result)){
//$profile = $item+":"+$warranty;
$profile = $item.":".$warranty;//I think, but am not sure what you wanted to do here
setCookie("artistconn",$_POST[$profile],time() + 86400,"/");//are you sure there is a posted variable $item.":".$warranty? Otherwise the cookie value will be null. Moreover even if you set the cookie it will be overwritten in the next loop...
echo "$result, $profile";//result is just pointer to mysql query results, moreover if you echo once you cannot set any cookies anymore.

}

I would also put
error_reporting(E_ALL);
so that you get all errors, warnings and notices.

Hope this set's you on the right track.
But to say the truth I have no idea what this code should do...

PS. One more question: does this query need to be performed every time? Cause now it is performed if the first if is false...

Regards
Michal

capulet_x

8:25 am on Mar 27, 2007 (gmt 0)

10+ Year Member



Michal, thank you for your response...

What I am trying to do is run an additional query after the login is successful. This query will select the fields item and warranty which correspond to the user name and access code that where submitted. The two fields, item and warranty, will then be set as a string (which will read item:warranty ) the string is then set to a cookie on the users computer so that it can be referred to later in another script.

} else {
mysql_free_result($rsLogin);
setCookie("cookie77",$_POST['username'],time() + 86400,"/");
// Expire Date: 1
// Expire Time: 0
//header("Location: ../PRODUCT_CONNECT.php");
/////// THE QUERY BELOW DOESN'T SEEM TO BE WORKING....
$query = "SELECT item, warranty FROM `db-1` WHERE username='$username' AND access='$access'";
$result = mysql_query($query)or die("Error: ".mysql_error());
while (list($item,$warranty) = mysql_fetch_row($result)){
//$profile = $item+":"+$warranty;
$profile = $item.":".$warranty;//I think, but am not sure what you wanted to do here
setCookie("artistconn",$_POST[$profile],time() + 86400,"/");//are you sure there is a posted variable $item.":".$warranty? Otherwise the cookie value will be null. Moreover even if you set the cookie it will be overwritten in the next loop...
echo "$result, $profile";//result is just pointer to mysql query results, moreover if you echo once you cannot set any cookies anymore.
}

mcibor

8:49 am on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got few questions then:

1. Can there be more item:warranty pairs for one user?
2. Is the user validated before the top else? I mean what is the if there.

if all answers are no, then the following code should work:


} else {
mysql_free_result($rsLogin);
setCookie("cookie77",$_POST['username'],time() + 86400,"/");

//I would also store here md5 of a pass as well, or just session, but it's up to you.

$query = "SELECT item, warranty FROM `db-1` WHERE username='$username' AND access='$access'";
$result = mysql_query($query)or die("Error: ".mysql_error());
list($item,$warranty) = mysql_fetch_row($result);//it will fetch first row only

$profile = $item.":".$warranty;

setCookie("artistconn",$profile,time() + 86400,"/");
echo "$profile";
header("Location: ../PRODUCT_CONNECT.php");

}

If the answer to 2. is yes, then change

} else {
mysql_free_result($rsLogin);
...
to

mysql_free_result($rsLogin);
...

Regards
Michal

capulet_x

4:19 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



Michal ,

Your help has been tremendous!
It's working perfectly now!
I'm pretty new to PHP but I hope to develope my knowledge to at least a point where I can one day return the favor.

Thanks Again!