Jump to content

Delete Selected Record


Dysan

Recommended Posts

How do I delete a record, upon the user clicking the check box displayed next to each record, and a delete button being clicked?

 

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
  die(mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("SELECT * FROM person");

while($row = mysql_fetch_array($result))
{
  echo '<input type="checkbox" name="checkbox" value="checkbox">';
  echo $row['FirstName'];
  echo $row['LastName'].'<br /><br />';
}

mysql_close($con);
?>

Link to comment
Share on other sites

OK, I'm confused.

 

Can we take this slowly? How do I use the "FirstName" field as the unique identifier, instead of id?

 

I have come up with the following code. What do I need to do next?

 

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
  die(mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("SELECT * FROM person");

while($row = mysql_fetch_array($result))
{
  echo '<input type="checkbox" name="checkbox[]" value="'.$row['FirstName'].'">';
  echo $row['FirstName'];
  echo $row['LastName'].'<br /><br />';
}

mysql_close($con);
?>

Link to comment
Share on other sites

Here's how I'd do it:

 

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
  die(mysql_error());
}

mysql_select_db("db", $con);

if(isset($_POST['checkbox'])){
   $delete_arr = $_POST['checkbox'];
   foreach($delete_arr as $value){
      $query = "DELETE FROM `db` WHERE `id` = '$value'";
      $run = mysql_query($query);
   }
  echo "Items deleted successfully.";
}

$result = mysql_query("SELECT * FROM person");
echo '<form name="deleteForm" id="deleteForm" action="filename.php" method="post" enctype="multipart/form-data">';
while($row = mysql_fetch_assoc($result)){
  extract($row);
  echo '<input type="checkbox" name="checkbox[]" value="'.$id.'">';
  echo $FirstName;
  echo $LastName.'<br /><br />';
}
echo '<input type="submit" />';
echo '</form>';

mysql_close($con);
?>

 

I haven't tested this but it should work.

Link to comment
Share on other sites

You'd replace $id with $FirstName, but I'd highly recommend not using the First Name as the unique identifier.  There could be a lot of table rows with the first names are the same.  Every table you make in a database should have an id associated to it.  I'd highly recommend using the row id verse any other field where data could be repeated.

Link to comment
Share on other sites

For what I'm building the application for, there can only be one instance of a name. Upon the user entering the names/data, the database will be searched, to ensure that the database doesn't already contain an instance of the selected name.

 

In other words, the name field, is similar to the username within an email address (the bit before the @) once it's taken, it can't be used again.

 

Is it possible to split the code up into to files.

Link to comment
Share on other sites

Upon the user selecting records to delete, and after the "Delete" button has been clicked, I want to display a div using JavaScript, containing delete confirm buttons "Yes" and "No".

 

Upon the "Yes" button being clicked, how do I delete the selected records.

 

Also, how do I create a separate file/page that displays a "Yes" and "No" confirm buttons, and navigate to it if JavaScript was disable on the previous page?

Link to comment
Share on other sites

Instead of using js to display a div, perhaps a better idea would be to have it display a confirm box:

 

<script type="text/javascript">
var confirmed = confirm("Are you sure you wish to delete this person?");
if(confirmed == true){
// Run script to post the form data
}
</script>

Link to comment
Share on other sites

Good idea, but how do I navigate to a confirm page that deletes the records using PHP, if JavaScript is turned off?

 

e.g.

 

Upon clicking "Yes", the delete code on page is activated.

Upon clicking "No", the user is returned to where he/she was before.

 

<html>
<body>

<form name="form1" method="post" action="">
  Are you sure you want to delete?<br>
  <br>
  <input type="submit" name="Submit" value="Yes">
  <input type="submit" name="Submit2" value="No">
</form>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.