0

I'm making a "delete" function on my form. I'm not quite happy because it doesn't work! I have tried this code:

     while($row = $result->fetch_assoc())
     {

     $ordernr = $row['ordernr'];
     $klantnaam = $row['klantnaam'];
     $productnaam = $row['productnaam'];
     $productid = $row['productid'];

     echo "<tr>";
     echo "<td width='150px'>" . $ordernr . "</td>";
     echo "<td width='150px'>" . $klantnaam . "</td>";
     echo "<td width='300px'>" . $productnaam . "</td>";
     echo "<td width='100px'>" . $productid . "</td>";

     echo "<td align='center' width='50px'><input name='delete[$ordernr]' type='checkbox'></td>";

     echo "</tr>";
     }

     echo "<tr>";

     echo "<td><input type='submit' name='verwijderen' value='Verwijderen'/></td>";
     echo "</tr>";
     echo "</table>";
     echo "</form>";

     $delete = $_POST['delete'];

     if (isset($_POST['verwijderen'])) {
         foreach($delete as $ordernr => $delete)
         {
             $ordernr = mysqli_real_escape_string($ordernr);
             $query = mysqli_query("DELETE FROM overzicht WHERE ordernr= $ordernr");
         }
     }

Do you guys know what I've done wrong?

Thanks in advance!

3
  • Have you checked that your $ordernr is being passed back? put an echo in your for loop and that echo's out the sql Commented Feb 25, 2014 at 11:01
  • delete[$ordernr] perhaps the issue. You can use delete[] with a value as $ordernr and on POST part u get an array and using the loop you can delete the items which are checked !! Commented Feb 25, 2014 at 11:01
  • judging by the responses here... I'd say it's hard to pinpoint just one problem. I'm surprised there are no parse errors! Commented Feb 25, 2014 at 11:09

5 Answers 5

1

1)

echo "<td align='center' width='50px'><input name='delete[$ordernr]' type='checkbox'></td>";

transforms to

echo "<td align='center' width='50px'><input name='delete' type='checkbox' value='$ordernr'></td>";

2)

foreach($delete as $ordernr => $delete)
do it:
foreach($delete as $ordernr => $del)

you are overwriting the $_POST

Sign up to request clarification or add additional context in comments.

2 Comments

Do I have to define $del somewhere? (because I actually haven't)
that was a comment so you know you have to change it. when you use $delete a second time it replaces the original one that comes from $_POST
0

mysqli_query syntaxt is

mysqli_query($con,"your query");

Comments

0

I'd go and say that your input checkbox needs a value. Like

<input type="checkbox" name="delete[1]" value="1" />

On the other hand, then it would be easier to have

<input type="checkbox" name="delete[]" value="$ordernr" />

And then you loop with

foreach ($_POST["delete"] as $id) delete(id)

Comments

0

where con variable in your query...

$query = mysqli_query($con,"DELETE FROM overzicht WHERE ordernr= $ordernr");

1 Comment

Yes, you're right. I've forgotten to put $con there. My bad!
0

You're iterating while reusing your variable here:

foreach($delete as $ordernr => $delete)

Where the second $delete will overwrite the value of the first, and throw a spanner in the works.

I suggest to rename the prior instances of $delete to $deleteList and do:

foreach($deleteList as $ordernr => $delete)

Keep in mind that much of your code could be done in different ways that may be more suitable. You will find those as you follow your path in learning PHP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.