Dysan Posted November 9, 2007 Share Posted November 9, 2007 Hi. I have the following code, that displays records in my database. How do I display each record along with a checkbox, then when a delete button is clicked, delete the selected records? mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM person"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 9, 2007 Share Posted November 9, 2007 <?php if (isset($_POST['submit'])){ foreach ($_POST['delete'] as $id){ $query = "DELETE FROM person WHERE personID='$id'"; $result = mysql_query($query)or die(mysql_error()); } echo "Records Deleted<p>"; } $result = mysql_query("SELECT * FROM person"); echo "<form action='{$_SERVER['PHP_SELF']}' method='post'>"; while($row = mysql_fetch_array($result)){ echo "<input type='submit' name='delete[]' value='{$row['personID']}'> "; echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } echo '<input type="submit" name="submit" value="Delete Records">'; echo '</form>'; ?> Wherever you see the row "personID" is where you need to replace that with whatever your unique ID is for that table. Quote Link to comment Share on other sites More sharing options...
bwochinski Posted November 9, 2007 Share Posted November 9, 2007 If you're deleting a lot of records better to do one query... $query = "DELETE FROM person WHERE personID=(".implode(",",$_POST['delete']).")"; Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 9, 2007 Author Share Posted November 9, 2007 So you mean do this? <?php if (isset($_POST['submit'])){ foreach ($_POST['delete'] as $id){ $query = "DELETE FROM person WHERE personID=(".implode(",",$_POST['delete']).")"; $result = mysql_query($query)or die(mysql_error()); } echo "Records Deleted<p>"; } $result = mysql_query("SELECT * FROM person"); echo "<form action='{$_SERVER['PHP_SELF']}' method='post'>"; while($row = mysql_fetch_array($result)){ echo "<input type='submit' name='delete[]' value='{$row['personID']}'> "; echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } echo '<input type="submit" name="submit" value="Delete Records">'; echo '</form>'; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.