-1

My query is not working in deleteartikel.php, it doesn't delete anything out of the database.

In form.php I made a delete button but it doesn't delete the row itself. I can't find the mistake.

form.php

 <?php
include 'dbconnect.php';

$sql = "SELECT * FROM artikel";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr>";
         echo "<td>".$row['Productcode']."</td>";
         echo "<td>".$row['Product']."</td>";
         echo "<td>".$row['Type']."</td>";
         echo "<td>".$row['Fabriekcode']."</td>";
         echo "<td>".$row['Inkoopprijs']."</td>";
         echo "<td>".$row['Verkoopprijs']."</td>";
         echo "<td><form action='deleteartikel.php' method='post'>
         <input type='submit' name='delete' value='delete'>
         </form></td>";
     }
} else {
     echo "0 results";
}

$conn->close();
?>
</tbody>
</table>
</div>

<!-- Footer navigatie -->
<footer>&copy;ToolsForEver 2017 alle rechten voorbehouden.</footer>

</body>
</html>

deleteartikel.php

<?php
if(isset($_POST['delete'])) {

include('dbconnect.php');

$productcode = $_POST['Productcode'];

$conn->query("DELETE FROM artikel WHERE Productcode = '$productcode'");

header('Location: artikel.php');

}
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Kainé
  • 19
  • 3
  • 10
  • sidenote: form cannot be child of table – Funk Forty Niner Jan 11 '17 at 17:00
  • You're not passing the Productcode in the form. – aynber Jan 11 '17 at 17:01
  • and where is the name attribute for $_POST['Productcode']; – Funk Forty Niner Jan 11 '17 at 17:01
  • 2
    If an answer solved your problem, consider accepting the answer. Here's how http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work then return here and do the same with the tick/checkmark till it turns green. This informs the community, a solution was found. Otherwise, others may think the question is still open and may want to post (more) answers. You'll earn points and others will be encouraged to help you. *Welcome to Stack!* – Jay Blanchard Jan 11 '17 at 17:13
  • You have a history of off-topic or heavily downvoted questions and are at risk of losing your question-asking privileges. You should [read this before you post your next one](http://meta.stackoverflow.com/questions/254262/before-you-post-your-next-question). – Jay Blanchard Jan 11 '17 at 17:14

1 Answers1

0

You never post the product code so the query fails, there is no value sent. You have to add a hidden field to your form:

echo "<form action='deleteartikel.php' method='post'>
      <input type='hidden' name='Productcode' value='". $row['Productcode'] ."'> 
      <input type='submit' name='delete' value='delete'>
      </form>";

Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe! Don't believe it?

In addition, as Fred pointed out above, a form cannot be the child of a table cell, so you need to refactor your code such that you have a form containing a table for each row.

Error checking in deleteartikel.php would have revealed the issue with the missing value for your query. Without error checking in the script you can find the errors in your web server's error logs.

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119