PHP session drag

Associate
Joined
19 Oct 2005
Posts
528
Hey folks im trying to drag customer number from the database that is equal to the current session logged in. I have the session working and i have it logged in but the code doesnt seem to work.

Custname is the session variable maybe its a problem with that?

<?php
$CustNumber = $_REQUEST['CustName'];
echo $_REQUEST['CustNumber'];
// Connect to the database.
include("db_connect.php");
// Query the database.
$query = "SELECT CustNumber from Customer WHERE CustName = '$CustNumber'";
$query_result = @mysql_query ($query);
$row = @mysql_fetch_array ($query_result);
if ($row > 1)
{?>
<table border="2" cellpadding="1" cellspacing="1">
<tr><td><b>CustNumber</b></td><td><?php echo $row[CustNumber]?></td></tr>
</table>
<?php
}
else
{
print "That record was not found.\n";
}
?>

Any help would be appreciated thankyou.
 
Shouldn't it be....

PHP:
if ($row > 0)

Your query is only set up to return 1 row? So if one row is returned, your condition will print that the record was not found.
 
hmmmmmmmmmmmmmmmmmm

Ok so i made the changes and it didnt work, i think their is a problem with the taking the session name as when i put in the customer name it works, but with the session name it doesnt.

Code:
<?php
$CustNumber = $_SESSION['CustName'];
echo $_SESSION['CustNumber'];
// Connect to the database.
	include("db_connect.php");
// Query the database.
$query = "SELECT CustNumber from Customer WHERE CustName = '$CustNumber'";
$query_result = @mysql_query ($query);
$row = @mysql_fetch_array ($query_result);
if ($row > 1)
	{?>
	<table border="2" cellpadding="1" cellspacing="1">
	<tr><td><b>CustNumber</b></td><td><?php echo $row[CustNumber]?></td></tr>
	</table>
    <?php
	}
else
	{
    print "That record was not found.\n";
    }
	?>

I must not be grabbing the session name correctly?
 
Ripper^ said:
did you try $row > 0 ? because it wont return anything atm. Either that or $row = 1


I tried $row > 1 with find the username if equal to 'dave' and it returned the field i wanted. So i dont see any problem in doing that. I think the problem lies with the session username not being equal to the current user logged in.
Im stumped. :confused:
 
i've done something reasonably similar recently, i'm no expert with php but here's the code I used for mine. The code is basically for a login page but you can change it to suit your's i'd imagine. This is assuming you have username/password login fields.

Code:
Code:
<?php

$username = $_POST['username'];
$password = $_POST['password'];

$query = "SELECT * FROM users WHERE username1 = '$username' AND password1 = '$password'";


$result = mysql_query($query);


$num_rows = mysql_num_rows($result);
 



if ($num_rows > 0) {
$_SESSION['username'] = $_POST['username'];
}
else
{
}

if (isset($_SESSION['username'])) {
echo "Welcome <b>" . $_SESSION['username'] . "</b>";
}
else
{
echo "<b>Please login</b>";
}

?>

Basically if it returns a result from the database (if $num_rows > 0) then it'll set the session username the same as was entered into the field. Then the next bit of code checks if the session username is set (which it will be if you've logged in successfully), and welcome you (you could change this to echo $_SESSION['CustNumber'] or whatever).

Hope this helps.
 
Back
Top Bottom