By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,829 Members | 2,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,829 IT Pros & Developers. It's quick & easy.

view singular, not multiple records

P: 17
I want to change the following piece of code to only show the actual user who is logged in, and not let them see other peoples information.

What can i do. Im totally baffled.

Could the answer please include line numbers.

thank y ou so much for you help

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. //connect to database
  4. $mysqli = mysqli_connect("localhost", "geothermal", "password", "geothermal");
  5.  
  6. if (!$_POST)  {
  7.     //haven't seen the selection form, so show it
  8.     $display_block = "<h1>Select an Entry</h1>";
  9.  
  10.     //get parts of records
  11.     $get_list_sql = "SELECT master_id,
  12.                      CONCAT_WS(', ', l_name, f_name) AS display_name
  13.                      FROM tbl_master_name ORDER BY l_name, f_name";
  14.     $get_list_res = mysqli_query($mysqli, $get_list_sql) or die(mysqli_error($mysqli));
  15.  
  16.     if (mysqli_num_rows($get_list_res) < 1) {
  17.         //no records
  18.         $display_block .= "<p><em>Sorry, no records to select!</em></p>";
  19.  
  20.     } else {
  21.         //has records, so get results and print in a form
  22.         $display_block .= "
  23.         <form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">
  24.         <p><strong>Select a Record to View:</strong><br/>
  25.         <select name=\"sel_id\">
  26.         <option value=\"\">-- Select One --</option>";
  27.  
  28.         while ($recs = mysqli_fetch_array($get_list_res)) {
  29.             $id = $recs['master_id'];
  30.             $display_name = stripslashes($recs['display_name']);
  31.  
  32.             $display_block .= "<option value=\"".$id."\">".$display_name."</option>";
  33.         }
  34.  
  35.         $display_block .= "
  36.         </select>
  37.         <p><input type=\"submit\" name=\"submit\" value=\"View Selected Entry\"></p>
  38.         </form>";
  39.     }
  40.     //free result
  41.     mysqli_free_result($get_list_res);
  42.  
  43. } else if ($_POST) {
  44.     //check for required fields
  45.     if ($_POST["sel_id"] == "")  {
  46.         header("Location: selentry.php");
  47.         exit;
  48.     }
  49.  
  50.     //get master_info
  51.     $get_master_sql = "SELECT concat_ws(' ', f_name, l_name) as display_name
  52.                        FROM tbl_master_name WHERE master_id = '".$_POST["sel_id"]."'";
  53.     $get_master_res = mysqli_query($mysqli, $get_master_sql) or die(mysqli_error($mysqli));
  54.  
  55.     while ($name_info = mysqli_fetch_array($get_master_res)) {
  56.         $display_name = stripslashes($name_info['display_name']);
  57.     }
  58.  
  59.     $display_block = "<h1>Showing Record for ".$display_name."</h1>";
  60.  
  61.     //free result
  62.     mysqli_free_result($get_master_res);
  63.  
  64.     //get all addresses
  65.     $get_addresses_sql = "SELECT address1, address2, town, county, postcode, type
  66.                           FROM tbl_address WHERE master_id = '".$_POST["sel_id"]."'";
  67.     $get_addresses_res = mysqli_query($mysqli, $get_addresses_sql) or die(mysqli_error($mysqli));
  68.  
  69.      if (mysqli_num_rows($get_addresses_res) > 0) {
  70.  
  71.         $display_block .= "<p><strong>Addresses:</strong><br/>
  72.         <ul>";
  73.  
  74.         while ($add_info = mysqli_fetch_array($get_addresses_res)) {
  75.             $address1 = stripslashes($add_info['address1']);
  76.             $address2 = stripslashes($add_info['address2']);
  77.             $town = stripslashes($add_info['town']);
  78.             $county = stripslashes($add_info['county']);
  79.             $postcode = stripslashes($add_info['postcode']);
  80.             $address_type = $add_info['type'];
  81.  
  82.             $display_block .= "<li>$address1 $address2 $town $county $postcode ($address_type)</li>";
  83.         }
  84.  
  85.         $display_block .= "</ul>";
  86.     }
  87.  
  88.     //free result
  89.     mysqli_free_result($get_addresses_res);
  90.  
  91.     //get all tel
  92.     $get_tel_sql = "SELECT tel_number, type FROM tbl_telephone
  93.                     WHERE master_id = '".$_POST["sel_id"]."'";
  94.     $get_tel_res = mysqli_query($mysqli, $get_tel_sql) or die(mysqli_error($mysqli));
  95.  
  96.     if (mysqli_num_rows($get_tel_res) > 0) {
  97.  
  98.         $display_block .= "<p><strong>Telephone:</strong><br/>
  99.         <ul>";
  100.  
  101.         while ($tel_info = mysqli_fetch_array($get_tel_res)) {
  102.             $tel_number = stripslashes($tel_info['tel_number']);
  103.             $tel_type = $tel_info['type'];
  104.  
  105.             $display_block .= "<li>$tel_number ($tel_type)</li>";
  106.         }
  107.  
  108.         $display_block .= "</ul>";
  109.     }
  110.  
  111.     //free result
  112.     mysqli_free_result($get_tel_res);
  113.  
  114.     //get all fax
  115.     $get_fax_sql = "SELECT fax_number, type FROM tbl_fax
  116.                     WHERE master_id = '".$_POST["sel_id"]."'";
  117.     $get_fax_res = mysqli_query($mysqli, $get_fax_sql) or die(mysqli_error($mysqli));
  118.  
  119.     if (mysqli_num_rows($get_fax_res) > 0) {
  120.  
  121.         $display_block .= "<p><strong>Fax:</strong><br/>
  122.         <ul>";
  123.  
  124.         while ($fax_info = mysqli_fetch_array($get_fax_res)) {
  125.             $fax_number =  stripslashes($fax_info['fax_number']);
  126.             $fax_type = $fax_info['type'];
  127.  
  128.             $display_block .= "<li>$fax_number ($fax_type)</li>";
  129.         }
  130.  
  131.         $display_block .= "</ul>";
  132.     }
  133.  
  134.     //free result
  135.     mysqli_free_result($get_fax_res);
  136.  
  137.     //get all email
  138.     $get_email_sql = "SELECT email, type FROM tbl_email
  139.                       WHERE master_id = '".$_POST["sel_id"]."'";
  140.     $get_email_res = mysqli_query($mysqli, $get_email_sql) or die(mysqli_error($mysqli));
  141.  
  142.      if (mysqli_num_rows($get_email_res) > 0) {
  143.  
  144.         $display_block .= "<p><strong>Email:</strong><br/>
  145.         <ul>";
  146.  
  147.         while ($email_info = mysqli_fetch_array($get_email_res)) {
  148.             $email = stripslashes($email_info['email']);
  149.             $email_type = $email_info['type'];
  150.  
  151.             $display_block .= "<li>$email ($email_type)</li>";
  152.         }
  153.  
  154.         $display_block .= "</ul>";
  155.     }
  156.  
  157.     //free result
  158.     mysqli_free_result($get_email_res);
  159.  
  160.     //get personal note
  161.     $get_notes_sql = "SELECT note FROM tbl_personal_notes
  162.                       WHERE master_id = '".$_POST["sel_id"]."'";
  163.     $get_notes_res = mysqli_query($mysqli, $get_notes_sql) or die(mysqli_error($mysqli));
  164.  
  165.     if (mysqli_num_rows($get_notes_res) == 1) {
  166.         while ($note_info = mysqli_fetch_array($get_notes_res)) {
  167.             $note = nl2br(stripslashes($note_info['note']));
  168.         }
  169.  
  170.         $display_block .= "<p><strong>Personal Notes:</strong><br/>$note</p>";
  171.     }
  172.  
  173.     //free result
  174.     mysqli_free_result($get_notes_res);
  175.  
  176.     $display_block .= "<br/>
  177.     <p align=\"center\"><a href=\"".$_SERVER["PHP_SELF"]."\">select another</a></p>";
  178. }
  179. //close connection to MySQL
  180. mysqli_close($mysqli);
  181. ?>
  182. <html>
  183. <head>
  184. <title>My Records</title>
  185. </head>
  186. <body>
  187. <?php echo $display_block; ?>
  188. </body>
  189. </html>
  190.  
Feb 12 '08 #1
Share this Question
Share on Google+
1 Reply


Markus
Expert 5K+
P: 6,050
That's alot of code to look through, but if i understand you right you could just add the LIMIT to your sql query.

[php]
$_query = "SELECT * FROM `tbl_name` WHERE `something` = 'something' LIMIT 1";
[/php]
Feb 12 '08 #2

Post your reply

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