Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I really need some help with this. I'm trying to create a table that will send an email when you press a button, and then after that deletes the record. to delete one record you need to specify the id (or in this case order id) in the query. but I always get the error:
Notice: Undefined index: order_id 


how do I fix this ??
this is my full 3 scripts

table:
<?php 
include "navbar.php";
include "config.php";
?> 
<style>
    .container {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 20px;
  }
    tr:first-of-type th:first-child 
    {
        border-top-left-radius: 10px; 
  
    }
  tr:first-of-type th:last-child{
    border-top-right-radius: 10px; 

  }
  
  /* tbody td:first-of-type th:last-child{
    border-bottom-right-radius: 10px;
  }
  tbody td:first-of-type th:last-child{
    border-bottom-left-radius: 10px;
  } */

</style>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css"/>
<div class="container">
<div class="table-responsive">
<table class="table table-dark table-hover" style=" width: 50%;   margin-left: auto; margin-right: auto; margin-top: 5%;">
  <thead>
    <tr>
      <th scope="col">order_id</th>
      <th scope="col">klant_id</th>
      <th scope="col">broodjes_ID</th>
      <th scope="col">klantnaam</th>
      <th scope="col">klant email</th>
      <th scope="col">Bevestigen?</th>
      <th scope="col">Afwijzen</th>
    </tr>
  </thead>
  <tbody>
  <?php
   $sql = "SELECT * FROM orders";
   $stmt = $conn->prepare($sql); 
   $stmt->execute();
   $result = $conn -> query($sql);
   $result = $stmt->get_result(); // get the mysqli result

      while($row = $result-> fetch_assoc()): ?>
      <tr>
        <td class="order_id"><?php echo $row["order_id"];?></td>
        <td class="klant_id"><?php echo $row["klant_id"];?></td>
        <!--Datum is eigenlijk meer voor de klant wanneer het kan worden opgehaald daar moet ene sorrt rekensom mee worden gemaakt-->
        <td class="broodjes_ID"><?php echo $row["broodjes_ID"];?></td>
        <td class="klantnaam"><?php echo $row["klantnaam"];?></td>
        <td class="klantemail"><?php echo $row["klantemail"];?></td>
        <td><a href="mail/bevestigordermail.php?id="<?php echo $row['order_id']; ?>  > 
            <input type="hidden" name="name" value="<?php echo $row['order_id']; ?>">
            <button type="button" name="bevestigen" class="btn btn-success" >class="fas fa-check" style="color: rgba(255, 255, 255, 1)"></button>
        </td>
        <td><a href="mail/afwijzingordermail.php?id="<?php echo $row['order_id']; ?>> <!-- het moest if zijn omdat je bij delete.php? id hebt gezet-->
            <input type="hidden" name="name" value="<?php echo $row['order_id']; ?>">
            <button type="button" name="delete" class="btn btn-danger">class="fas fa-times" style="color: rgba(255, 255, 255, 1)"></button>
        </td>
      </tr>
      <?php endwhile; ?>
 </tbody>
</table>
</div>
</div>


example of a mail:
<?php include "mailen.php"; 
include "../config.php";?>
<!-- moet ik heir prepared statements gebruiken?-->
<html>
   <!-- sending a mail -->
   <body>
      
      <?php
      //moet misschien nog in prepared statements maar eerst maar voor zorgen dat dit werkt
      //tijd plus 1 uur
         $newtime = date('H:i', time() + 3600);
         echo $newtime;
         $sql = "SELECT * FROM orders";
  
         $stmt = $conn->prepare($sql); 
         $stmt->execute();
         $result = $conn -> query($sql);
         $result = $stmt->get_result();
       
             while($row = $result-> fetch_assoc()){
                  $email = $row['klantemail'];
                  $klant = $row['klantnaam'];
                  $onderwerp = "Bestelling";
                  $bericht = "Geachte $klant, uw bestelling is bevestigd! U kunt uw order ophalen om $newtime";
                  mailen($email, $klant, $onderwerp, $bericht);
                  header("Location: ../delete.php");
             }

      ?>
      
   </body>
</html>


my delete script
<?php

include "config.php"; // Using database connection file here
//werkt nog niet hij kan namelijk de id niet vinden dus dat moet nog gefixd worden
// $id = $_GET['id'];
// var_dump($id);
// sql to delete a record
$order_id= $_GET['order_id'];
$sql = "DELETE FROM orders WHERE order_id=?";
$stmt = $conn->prepare($sql); 
$stmt->bind_param('s', $order_id);
if ($stmt->execute()) {
  $result = $stmt->get_result();
  echo "order deleted successfully";
} else {
  echo "Error order record: " . $conn->error;
}

// //go back to index.php
    //   header('Location: index.php'); 
    //  exit;


please help me idk what to do now

What I have tried:

tried looking this up on stack overflow but tbh I didn't really understand what they meant there

tried seeing what my $order_id shows and it tells me that its NULL

also already tried doing $order_id = $_GET['id']; gives the same error
Posted
Updated 9-Dec-21 2:12am
v4
Comments
Richard Deeming 9-Dec-21 9:53am    
REPOST
This is the same as your previous question:
$_Get['id']; doesnt work[^]

1 solution

You have a couple of issues with your code:
PHP
<a href="mail/bevestigordermail.php?id="<?php echo $row['order_id']; ?>  >

First, you're closing the href attribute before you'd actually appended the order ID. You need to move the closing " so that it's after the echo statement, like so:
PHP
<a href="mail/bevestigordermail.php?id=<?php echo $row['order_id']; ?>"  >
                                                                      ^

Second, you've not named the URL parameter order_id, you've named it id so it won't be picked up by PHP. If you rename the URL parameter then the PHP file should pick it up:
PHP
<a href="mail/bevestigordermail.php?order_id=<?php echo $row['order_id']; ?>"  >
                                    ^^^^^^^^

Once last remark on your HTML structure, you're wrapping <button> elements in an <a> element which isn't really a good idea. If you're intending on using a button then might I recommend you use a <form> element instead? Something like:
PHP
<form action="mail/bevestigordermail.php?order_id=<?php echo $row['order_id']; ?>" method="get">
  <button type="submit">....</button>
</form>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900