Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello this used to work before but since I have added a php mailer file to send an email it has stopped working. I want to be able to delete my record after the user has pressed on one of the two buttons. but right now I'm getting the error
Notice: Undefined index: id
how do I fix this?

full error
 Notice: Undefined index: id in C:\xampp\htdocs\bestelapp\delete.php on line 5
Error order record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1


codes
these are the buttons that you press and then the system will send an email and delete the records
<?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><form action="mail/bevestigordermail.php?order_id=<?php echo $row['order_id']; ?>" method="get">
              <button type="submit" class="btn btn-success" >class="fas fa-check" style="color: rgba(255, 255, 255, 1)"></button>
            </form>
        </td>
        <td><form action="mail/afwijzingordermail.php?order_id=<?php echo $row['order_id']; ?>" method="get">
              <button type="submit" class="btn btn-danger" >class="fas fa-times" style="color: rgba(255, 255, 255, 1)"></button>
            </form>
        </td>
      </tr>
      <?php endwhile; ?>
 </tbody>
</table>
</div>
</div>


the delete file: he can't find the id in here anymore

<?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
$sql = "DELETE FROM orders WHERE order_id=?";
$stmt = $conn->prepare($sql); 
$stmt->bind_param('i', $id);
if ($stmt->execute()) {
  echo "order deleted successfully";
} else {
  echo "Error order record: " . $conn->error;
}

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



and lastly for (if it helps) my mail file
so when you press the button it will first fo right here and then to the delete script
<?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>


What I have tried:

I have tried adding the id to my mail file but that doesn't do anything
Posted
Updated 10-Dec-21 0:51am
v5
Comments
Richard Deeming 8-Dec-21 6:06am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
Richard MacCutchan 8-Dec-21 7:08am    
Your form is using the POST method, so parameters must be accessed via $_POST. See PHP Form Handling[^].
Chris Copeland 8-Dec-21 9:26am    
I'm not even sure this is valid HTML, if you look at the actual element:

<a href='mail/afwijzingordermail.php?id="<?php echo $row['order_id']; ?>"' method="post">

It's a link with a method? That doesn't work, the method attribute is meant to be used with <form> elements. Also the link looks malformed, there's some double-quotation marks in there. I'm not sure if PHP would strip that out or not..
Rebecca2002 8-Dec-21 10:05am    
ok I edited it with prepared statments and I guess the problem is that it can't even get order_id in the table

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