Results 1 to 4 of 4

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

  1. #1
    Join Date
    Feb 2012
    Location
    Dhaka, Bangladesh
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How can I retrieve the unique identifier(primary key) of a mysql table?

    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.



  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    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.

  3. #3
    Join Date
    Feb 2012
    Location
    Dhaka, Bangladesh
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

    Code:
    <?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!");
    }
    	}
    
    ?>

  4. #4
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    You are already running the query you need to achieve this. You just need to get the info from the query.
    PHP Code:
    <?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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •