470,849 Members | 1,279 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

How to set Dynamic WHERE showing customer rows depend on customer_ID using PHP?

132 100+
Hi all once again!!

I have two tables in MYSQL, tblLogin and tblCustomers with the following Columns: tblLogin{LoginID as Primary_Key, UserName, Password} and tblCustomer{Customer_ID as Primary_Key, Customer_FullName, OrderID, OrderDetails, submission_date, Customer_LoginID as Foreign_key}.
What I want is to let customers to view their own records only due to their login information.
To accomplish this I set two forms namely Login_Form.php and Results.php where after pressing SUBMIT BUTTON of Login_Form.php the user should be able to see his own records displayed on Results.php form dynamically.
Here is my code for these forms!
1. Login_form.php
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.   <meta content="text/html; charset=ISO-8859-1"
  4.  http-equiv="content-type">
  5.   <title></title>
  6. </head>
  7. <body>
  8. <form target="_self" enctype="text/plain" method="post"
  9.  action="Results.php" name="Login_Form"><label>User
  10. name</label> &nbsp; 
  11. <label>Password<br> <br> </label><input name="UserName" value="User Name">
  12. &nbsp;<input name="Password" value="Password"
  13.  type="password"><br>
  14.   <br>
  15. &nbsp; 
  16. &nbsp; &nbsp; <input name="Submit" value="submit"
  17.  type="submit"><br>
  18.   <br>
  19. </form>
  20. </body>
  21. </html>
2. Results.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dbhost = 'localhost:3306';
  3. $dbuser = 'root';
  4. $dbpass = 'rootpassword';
  5. $conn = mysql_connect($dbhost, $dbuser, $dbpass);
  6. if(! $conn )
  7. {
  8.   die('Could not connect: ' . mysql_error());
  9. }
  10. $sql = 'Customer_ID, Customer_FullName, OrderID, OrderDetails,submission_date, Customer_LoginID WHERE .....?';
  12. mysql_select_db('tblCustomers');
  13. $retval = mysql_query( $sql, $conn );
  14. if(! $retval )
  15. {
  16.   die('Could not get data: ' . mysql_error());
  17. }
  18. while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
  19. {
  20.     echo "Customer_ID :{$row['Customer_ID']}  <br> ".
  21.          "Full Name: {$row['Customer_FullName']} <br> ".
  22.          "Order Details: {$row['OrderDetails']} <br> ".
  23.          "Date : {$row['submission_date']} <br> ".
  24.          "--------------------------------<br>";
  25. echo "Fetched data successfully\n";
  26. mysql_close($conn);
  27. ?>
Now how to fetch specific data due to specific customer? Any hint would be highly appreciated
Mar 10 '13 #1
3 1736
12,516 Expert Mod 8TB
You can use the where condition in your select query to filter for the results you're looking for.
Mar 10 '13 #2
Mr Key
132 100+
Is better you leave the question unanswered rather than typing one line blablaaa.
I know very well how to write WHERE CONDITION, the issue is to capture the input from one login form to SQL!
Please the other time read first before u respond
Mar 12 '13 #3
13,262 8TB
It's more of a PHP question than a MYSQL one then. I would suggest putting the userId in the session when the user logs in though rather than submitting with every request to your results page. Then you look it up from the session using $_SESSION['userId'] syntax and append it to the where clause if your table structure supports that.
That way you can always filter anything from anywhere based on that userId.
You can read more about session variables here: http://www.w3schools.com/php/php_sessions.asp
Mar 12 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by archana | last post: by
1 post views Thread by Nazish | last post: by
reply views Thread by ZSvedic | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.