简体   繁体   中英

Php Table to display records with specific ID

I am trying to get a specific query to work based on the id sent to another page and produce a table of items belonging to a specific person. I have 2 different tables a person table that list the person and primary key of id. On the 2nd table vehicles, i have a primary key of vehicleid and a id that is the person id. the table does not filter the responses to the specific person. In the menu bar i have upd** and the id of the person example upd=15, every every wayi have tried to get the php to work to produce a list of vehicles just belonging to that person has failed. I have lost track of how many tutorials and blogs i have looked at.

Example of the start of my Code. ***

<?php require "config/config.php"; ?> 

<?php if(isset($_GET['upd'])){ $id = $_GET['upd']; $query = "SELECT * FROM vehicles 
WHERE id=$id"; $fire = mysqli_query($con,$query) or die("Can not fetch the data.".mysqli_error($con)); $user = mysqli_fetch_assoc($fire); } ?>
Select p.name, p.other_person_info, v.car_make, v.car_model From person as p 
Left Join vehicles ON p.id = v.id WHERE p.id = '15'

In other words, LEFT JOIN returns all rows from the left table regardless of whether a row from the left table has a matching row from the right table or not. If there is no match, the columns of the row from the right table will contain NULL. So person is represented as p and vehicles table is represented as v and it will only output where your person id is equal to 15

you are vulnerable of SQL Injection. I suggest study PDO.

Regarding to your question. Below are the codes:

<?php
  include('DBHOST','localhost'); //if you are using localhost
  include('DBNAME',''dbname); //your dbname
  include('DBUSER','root'); //username of your localhost
  include('DBPASS','yourpassword');

  try
   {
     $conn = new PDO("mysql: host=".DBHOST.";charset=utf8mb4; dbname=".DBNAME, DBUSER, DBPASS);
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

     $qry = "SELECT * FROM tbl_yourtable";
     $stmt = $conn->prepare($qry);
     $stmt->execute();
     $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
     }
     catch(PDOException $e)
     {
      die("Error Connection".$e->getMessage());
     }
     ?>

then in your table:

     <?php
      foreach($result as $value)
      {
       echo '<tr>
           <td>' .$value['id']. '</td>
           <td>' .$value['name']. '</td>';
      }
      ?>

Just like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM