How can I retrieve the primary key of a mysql table with PHP?

[b]Scenario:
A customer registers in my website. Customer information is stored in a table in MySQL called ‘customer’. Customer uses username and password combination to log in and place his order.

Now I want to display the Customer_ID(primary key of customer table) in the order form. But problem is that the customer only uses name and password to log in, not the ID. How can I obtain/retrieve the corresponding Customer_ID of the username-password combination with a SELECT statement and display it in the order form?

I tried with SESSION but it didn’t work as the Customer_ID is not being inserted by the customer when logging in.

Anyone care to help? My last thread had visits but no posts. Its sad. If anyone can solve this problem, I can submit my project. This is the only remaining problem.[/b]

The Customer table looks like -
[table]
[tr]
[td]Customer_ID[/td][td]Customer_Name[/td][td]Password[/td][td]Home_Address[/td][td]Contact_Number[/td]
[/tr]
[tr][td]1[/td][td]David[/td][td]david89[/td][td]14, Bacon Street[/td][td]22554411[/td][/tr]
[tr][td]2[/td][td]Martha[/td][td]ilovemyself33[/td][td]Regent Park[/td][td]11223344[/td][/tr]
[/table]

Here is the code of how I tried and failed -
[php]<?php
ob_start(“ob_gzhandler”);
include(‘config.php’);

if($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
$name = mysql_real_escape_string($_POST[Customer_Name]);
$password = mysql_real_escape_string($_POST[Password]);
//query
$query = mysql_query(“SELECT * FROM customer WHERE Customer_Name=’$name’ AND Password=’$password’”);
$id_query = mysql_query(“SELECT Customer_ID FROM customer WHERE Customer_Name=’$name’ AND Password=’$password’”);

$id_query_rows = mysql_num_rows($id_query);

if($id_query_rows > 0) {
session_start();
$_SESSION[‘cID’] = $id_query;
}

$query_rows = mysql_num_rows($query);

if($query_rows > 0) {
echo(“Successful Login!”);
$_SESSION[‘cname’] = $name;
//the following code is for redirecting when submit button is pressed
//another alternative is saving the .php file to ‘UTF-8 without BOM’
header (“location: logged.php”);

} else {
echo(“Login Failed!”);
}
}

?>[/php]

There is an output, which is 0. So it doesn’t serve my purpose. I can’t seem to figure out the problem.

very simple
[php]

<?php session_start(); ob_start("ob_gzhandler"); include('config.php'); if($_POST['submit') { $name = mysql_real_escape_string($_POST['Customer_Name']); $password = mysql_real_escape_string($_POST['Password']); $query = mysql_query("SELECT * FROM customer WHERE Customer_Name='$name' AND Password='$password'"); if(mysql_num_rows($query) != 0) { $row = mysql_fetch_assoc($query); $_SESSION['cID'] = $row['Customer_ID']; header('Location: logged.php'); } else { echo("Login Failed!"); } } ?>

[/php]

One other thing, its not a good idea to use unencrypted passwords in a login script. you can use md5() and others. Also, you can’t send text to the screen (ie, echo,print, etc) before using header(), you’ll get a lot of error messages if you do. If the page changes, then the login was sucessful :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service