How can I finish this program using a query to get id value, using a $_GET[ ] variable to hold and r

Discussion in 'PHP' started by Sean Van Zant, Nov 29, 2018.

  1. Sean Van Zant

    Sean Van Zant New Member

    Joined:
    Nov 26, 2018
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    I am coding a program that uses the id value from a url variable with a query to eventually replace data in a table. It is a program from a coding assignment I have using PHP & MySQL.

    In the details. php file there is a customer id that is passed in that I need to create a new variable for. I need also to write a conditional statement that checks to see if any value was passed in. If not, I need to echo out an error. I need to write a query to the query that uses a WHERE clause that looks for the customer id that is equal to the id variable. I need to return only one result using the bind_param() to get the results with. I also need to replace the data in the first box.

    I have googled and read through the posts that were in the first page of the search results and tried another query or two through google and read some of those results that might have matched some of the program I was trying to write but didn't have too much luck with any of those queries. And that is why I am looking for help with this coding assignment.


    details.php:
    Code:
    <?php
    
       include 'connection.php'; // includes the connection.php file to connect
       to the database
    
       // query to database and prepares and executes
       $query = "SELECT id, first_name, last_name, city, state_id, postal_code
       FROM customers ORDER BY last_name asc";
       $stmt = $db->prepare($query);
       $stmt->execute();
       $stmt->store_result();
       $stmt->bind_result($id, $first_name, $last_name);
    
       $custID = $_GET[$id];
    
       if ($custID == NULL)
           echo "<p>"You have not entered search details."</p>";
    
    ?>
    
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Granger Customer Details</title>
    <link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
    </head>
    
    <body>
    
    <!--start container to center design in browser-->
    <div class="container">
      <div class="row" style="margin-bottom:30px">
       <div class="col-xs-12">
         <ul class="nav nav-pills">
           <li class="nav-item">
                   <a class="nav-link" href="index.php">Home</a>
    
    
    customers.php:
    Code:
    <?php
    
       include 'connection.php'; // includes the connection.php file to connect
       to the database
    
       // query to database and prepares and executes
       $query = "SELECT id, first_name, last_name FROM customers ORDER BY
       last_name asc";
       $stmt = $db->prepare($query);
       $stmt->execute();
       $stmt->store_result();
       $stmt->bind_result($id, $first_name, $last_name);
    
       // count the number of customers
       $total_customers = $stmt->num_rows;
    
    ?>
    
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Granger Customers</title>
    <link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
    </head>
    
    <body>
    
    <!--start container to center design in browser-->
    <div class="container">
    
       <div class="row" style="margin-bottom:30px">
           <div class="col-xs-12">
               <ul class="nav nav-pills">
                 <li class="nav-item">
                   <a class="nav-link" href="index.php">Home</a>
                 </li>
                 <li class="nav-item">
                   <a class="nav-link" href="customers.php">Customers</a>
                 </li>
                 <li class="nav-item">
                   <a class="nav-link" href="">Search</a>
                 </li>
                 <li class="nav-item">
                   <a class="nav-link" href="">Add New</a>
                 </li>
               </ul>
           </div>
       </div>
    
    
    <div class="row">
    
       <div class="col-xs-12">
         <table class="table table-bordered table-striped table-hover">
              <p><strong>There are a total of <?PHP echo $total_customers; ?>
    customers.</strong></p> <!-- output total number of customers -->
                 <thead>
                   <tr class="success">
                     <th class="text-center">#</th>
                     <th>Customer ID</th>
                     <th>Last Name</th>
                     <th>First name</th>
                     <th class="text-center">Details</th>
                   </tr>
                 </thead>
                 <tbody>
    
    
    
    
    
    <?php while($result = $stmt->fetch()): ?>      
    <tr>
       <td clas="text-center">#</td>
       <td><?php echo $id; ?> </td>
       <td><?php echo $first_name; ?> </td>
       <td><?php echo $last_name; ?> </td>
       <td class="text-center"><a class="btn btn-xs btn-primary"
    href="details.php?id=20597">Details</a></td>
    </tr>
    <?php endwhile; ?>
    
                 </tbody>
               </table>
       </div>
    
    </div>
    
    </div>
    <!--end container to center design in browser-->
    
    </body>
    
    </html>
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice