Log in

View Full Version : How can I retrieve the unique identifier(primary key) of a mysql table?



Priyo
02-21-2012, 08:07 PM
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.

http://img18.imageshack.us/img18/2255/problemzg.jpg

fastsol1
02-22-2012, 01:28 AM
During the login process get the Customer_ID from the db while you are checking the username and password and then assign the id to a session var or what ever you are doing with the other info, then you can use it just like the username and password later.

Priyo
02-22-2012, 06:37 AM
The customer is NOT writing down the ID, he/she is unaware of it because it is auto-generated in the mysql table. How can I run a query which will give me the output and which I can store in a SESSION and display later?

Here is the code I am using for the login page.


<?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'");
$query_rows = mysql_num_rows($query);
if($query_rows > 0) {
echo("Successful Login!");
session_start();
$_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!");
}
}

?>

fastsol1
02-23-2012, 01:33 AM
You are already running the query you need to achieve this. You just need to get the info from the query.

<?php
ob_start("ob_gzhandler");
session_start(); // This MUST be at the very top of your pages. It can only come after ob_start() and nothing else.
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'");
$query_rows = mysql_num_rows($query);
if($query_rows > 0) {
echo("Successful Login!");
$row = mysql_fetch_assoc($query); // The data from the DB
$_SESSION['cname'] = $name;
$_SESSION['id'] = $row['Customer_ID']; // The customer_id from the DB
//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!");
}
}

?>
Now you can use $_SESSION['id'] on any other page that has session_start(); at the top of it. Read the comments I put in the script.