0

I'm attempting to delete a row from a table with data that is generated from a MySQL table. I took a look at both of these questions: How to delete rows of database results using checkbox and Deleting multiple rows using checkboxes, PHP and MySQL. I understand both of the answers, but I'm running into one problem. Each question has their checkbox setup like this:

<input name="checkbox[<?php echo $row['id']?>]"

and

<input name="checkbox[]" type="checkbox" value="<?php echo $row['topic_id'];?>">

I am unable to use either of these in my code, as my table is being generated in php, and using brackets and quotes within my code (the checkbox name or value property, to be exact) throws a syntax error in Sublime. I'm wondering if there is any workaround to this?

Here is a chunk of my table:

<tbody>
    <?php
        $query = mysqli_query($db, "SELECT * FROM SOISInventoryList WHERE numInStock = 0 ORDER BY itemName");

        if(!$query){
            echo "Could not grab query.";
        }

        else{
            while($row = mysqli_fetch_assoc($query)){
                echo "<tr>";
                echo "<td class='strikeOut'>" . $row['itemName'] . "</td>";
                echo "<td class='strikeOut'>" . $row['numInStock'] . "</td>";
                echo "<td class='strikeOut'>" . "$" . $row['pricePerUnit'] . "</td>";
                echo "<td class='delete'>" . "<input type='checkbox' name='delete'>";
                echo "</tr>";
            }                                
        }
    ?>
</tbody>

Any kind of help or suggestions are appreciated.

Community
  • 1
  • 1
madjaeg
  • 13
  • 4
  • To use either of these, you would just need to change the double quotes like `name="checkbox[]"` to single `name='checkbox[]'` _or_ change the quotes on the outer string `" – Michael Berkowski Jul 24 '14 at 20:45
  • @MichaelBerkowski That shouldn't matter. I've use [this method](http://stackoverflow.com/a/14475126/) with success before, still do actually. – Funk Forty Niner Jul 24 '14 at 20:58

2 Answers2

0

You don't have to keep yourself tied to the exact syntax you're using there. You could just as easily pass that entire array off to JavaScript, and have THAT render it out on the page. In the end, all you really need from each checkbox is "is it checked" and if so, "what's the ID assigned to it?".

Then when you post it back to PHP, you can determine what to do with that information. Like, say, delete a row with that ID.

durbnpoisn
  • 4,666
  • 2
  • 16
  • 30
0

Your syntax problems (in short) come from the fact that you're using the echo command to print out each line of HTML individually. For the type of task you're trying to do, try out the alternative syntax

<?php while($row = mysqli_fetch_assoc($query)) : ?>
  <tr>
    <td class='strikeOut'> <?= $row['itemName'] ?> </td>
    (other TDs)
    <td class='delete'><input type='checkbox' name='delete[<?= $row['id']?>]'></td>
  </tr>
<?php endwhile ?>

Printing HTML in this manner saves a lot of headaches (and looks much nicer)!

Chris Barcroft
  • 562
  • 5
  • 12
  • Glad to help. One word of warning, the short echo tag I used ( = ?> ) is only enabled by default in PHP 5.4+. If you're using an earlier version, you'll need to enable short_open_tag in your php.ini file. – Chris Barcroft Jul 24 '14 at 21:32