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.

2
  • 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 "<input..." from double to single. Commented Jul 24, 2014 at 20:45
  • @MichaelBerkowski That shouldn't matter. I've use this method with success before, still do actually. Commented Jul 24, 2014 at 20:58

2 Answers 2

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)!

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

1 Comment

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.
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.

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.