Jump to content

How can I delete entry from the DB?


bryanptcs

Recommended Posts

I have this simple form that will update a section of HTML.  What i need to happen is to have a delete button for each entry that will delete the corresponding entry.  Here is my code so far:

[code]<p class="submit">Please fill in all fields. Thank you. </p>
<form class="form" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <p>
      <label for="txtName">Class</label>
      <br />
    <input type="text" title="Enter your name" name="txtClass" /></p>

<p>
  <label for="rate">Date</label>
  <br>
  <input type="text" title="Date" name="txtDate" /> 
  <label for="rate"></label>
  </p>
<p>City<br>
  <input type="text" title="City" name="txtcity" />
  </p>
<p>Contact<br>
  <input type="text" title="contact" name="txtcontact" />
          </p>
<p><label title="Send your message">
    <input type="submit" value="Send" />
   
    </label></p><br />
<hr>

   
</form>
<table width="100%" align="center" cellpadding="0" cellspacing="0">
  <tr class="colorbar2">
    <td width="22%"><div align="center"><span class="style3 style4"><strong>Class</strong></span></div></td>
    <td width="16%"><div align="center"><span class="style3 style4"><strong>Date of Class </strong></span></div></td>
    <td width="31%"><div align="center"><span class="style3 style4"><strong>Location of Class</strong></span></div></td>
    <td width="31%"><div align="center"><span class="style3 style4"><strong>Who to Contact</strong></span></div></td>
  </tr></table>
<?php

/**
* Create the table in your MySQL database:
*
* CREATE TABLE guests (
*  id int(10) NOT NULL auto_increment,
*  name varchar(50) NOT NULL,
*  message varchar(255) NOT NULL,
*  date timestamp(14) NOT NULL,
*  PRIMARY KEY (id)
* )
*
* Change the database login settings to your own
*
* The script is now ready to run
*/

// Change these to your own database settings
$host = "localhost";
$user = "******";
$pass = "******";
$db = "*****";

mysql_connect($host, $user, $pass) OR die ("Could not connect to the server.");
mysql_select_db($db) OR die("Could not connect to the database.");
     
$class = stripslashes($_POST['txtClass']);
$date = stripslashes($_POST['txtDate']);
$city = stripslashes($_POST['txtcity']);
$contact = stripslashes($_POST['txtcontact']);

if (!isset($_POST['txtClass'])) {
$query = "SELECT id, class, date, city, contact FROM class";
    $result = mysql_query($query);
   
    while ($row = mysql_fetch_object($result)) {

?>

  <table width="100%" align="center" cellpadding="0" cellspacing="0" border="1">
  <tr><td width="22%">
<p class="infotext"><?php echo $row->class; ?></p></td>
<td width="16%"><p class="infotext"><?php echo $row->date; ?></p></td>
<td width="31%"><p class="infotext"><?php echo $row->city; ?></p></td>
<td width="31%"><p class="infotext"><?php echo $row->contact; ?> </p></td></tr></table>


<?php
       
    }
   
?>





<?php

}

else {

    // Adds the new entry to the database
    $query = "INSERT INTO class SET class='$class', date='$date', city='$city', contact='$contact'";
    $result = mysql_query($query);

    // Takes us back to the entries
    $ref = $_SERVER['HTTP_REFERER'];
    header ("Location: $ref");
}

?> [/code]
Link to comment
https://forums.phpfreaks.com/topic/27251-how-can-i-delete-entry-from-the-db/
Share on other sites

You could have a check box infront of what you want to delete, and one button, that way you can delete multiple entries
[code]<?php
foreach($_POST['delete'] as $delete){
    mysql_query("DELETE * FROM table_name WHERE value='$delete'")or die(mysql_error());
}
?>[/code]

These would be part of your HTML:
name = delete[]
value = value of the row

Archived

This topic is now archived and is closed to further replies.

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