0

I have a table showing information from a mysql table and in the last column I have a checkbox like this :

<form method="POST" action="php/delete.php">
  <thead>
    <tr
      <th>Nom</th>
      <th>Espéce</th>
      <th>Cri</th>
      <th>Propiétaire</th>
      <th>Age (années)</th>
      <th><input type="submit" name="supprimer" value="Supprimer" /></th>
    </tr>
  </thead>
  <tbody>
    <?php
      $connexion = mysql_connect($hote, $login, $mdp);
      mysql_select_db($bd, $connexion);
      $req = "Select * from animaux;";
      $resultat = mysql_query($req, $connexion);
      while (list($id, $nom, $esp, $cri, $prop, $age) = mysql_fetch_row($resultat)) {
    ?>
      <tr>
        <td><?= $nom ?> </td>
        <td><?= $esp ?></td>
        <td><?= $cri ?></td>
        <td><?= $prop ?></td>
        <td><?= $age ?></td>
        <td> <input type="checkbox" name="choix[]" value=<?= $id ?>></td>
      </tr>
  <?php } ?>
  </form>

I am using the following script to delete the rows :

  include("../BD/bd_params.inc.php");

  $connexion = mysql_connect($hote,$login,$mdp);
  mysql_select_db($bd, $connexion);

  $del = $_POST['choix'];

  foreach ($del as $val) {
    $req = "delete from `animaux` where id = '$val'";
    $resultat = mysql_query($req,$connexion);

    if (!resultat) {
      die('Requête invalide : ' . mysql_error());
      break;
    } 
  }

However when i submit the form nothing happens, nothing is shown and nothing is deleted. Does anyone what could be causing my script not working ?

(oh, and sorry for my english ...)

14
  • Looks correct to me. Try turning on errors and see if you get any info. Commented Nov 1, 2011 at 18:52
  • at the top of your script add error_reporting(E_ALL); and ini_set('display_errors', 'on'); Commented Nov 1, 2011 at 20:10
  • it doesn't print anything... However now i am able to delete only element at time, even if several are selected. And when i'm adding var_dump($_POST); when i check several boxes, it shows : array(2) { ["supprimer"]=> string(9) "Supprimer" ["choix"]=> array(1) { [0]=> string(1) "6" } } . So apparently, the array 'choir' doesn't work properly... Commented Nov 1, 2011 at 20:40
  • Okay, apparently it's only working with the first checkbox checked. Furthermore, in the array, the only value is a string so i have to cast it before making any sql query. Any idea how to have all values in the POST array ? Commented Nov 2, 2011 at 19:57
  • if you do a print_r($_POST), what do you see? Commented Nov 2, 2011 at 20:19

2 Answers 2

1
if (!resultat) {

should be

if (!$resultat) {
     ^--missing $

The 'break' call is superfluous. die() will kill the script, so it'll be impossible for the break to be reached.

Have you verified that there is something being sent to the server. What does var_dump($_POST) show?

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

6 Comments

var_dump($_POST); shows array(2) { ["supprimer"]=> string(9) "Supprimer" ["choix"]=> array(1) { [0]=> string(1) "1" } }. I guess i have to cast the strings into integers ?
OK. So a checkbox is being sent. So... Add some debugging output: var_dump($req); var_dump($resultat), etc... and see what comes out.
var_dump($req) : string(36) "delete from animaux where id = '1'" var_dump($resultat)` : bool(true)
According to that, the db operation worked. You'll have to check why it's not actually deleting anything.
It's working now, but only if id < 5 and if only one checkbox is checked.
|
0

I've created a new version of your pastebin here:

http://pastebin.com/KdqscV87

Basically, you want to open the form before you start the loop and close the form after you finish the loop.

I've removed the hidden (class="invisible") form (you can't have a form within a form) from the loop. I'm not sure what you're needing there, but it won't work inside the other form.

3 Comments

Thank you this is working ! (the hidden class="invisible" actually appears when the <th>nom</th> is clicked. it makes disappear the first form and appear this one thanks to javascript.)
OH, ok. I'd say to create that form after the loop and then manipulate it with javascript to show it where you need it with the correct ids, etc. when you click on that th. That way, you don't have multiple forms, just one that you can change based on the th that was clicked.
You mean like using a javascript function that have the id of the th clicked, and thanks to this displaying the corresponding values in the other form ?

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.