bryanptcs Posted November 15, 2006 Share Posted November 15, 2006 How can I delete an entry from the database from my php page? My code is below. I basically am having an admin side where the admin can select an entry and delete it without having to go into the actual db and empty it. Any suggestions? [code] <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>Contact Email<br> <input name="txtemail" type="text" id="txtemail" title="contact" /> </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 = "";$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']);$email = stripslashes($_POST['txtemail']);if (!isset($_POST['txtClass'])) { $query = "SELECT id, class, date, city, email, 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"><a href="mailto:<?php echo $row->email; ?>?subject=<?php echo $row ->class; ?>"><?php echo $row->contact; ?></a> </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', email='$email', 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/27377-delete-entry-from-the-database/ Share on other sites More sharing options...
Psycho Posted November 15, 2006 Share Posted November 15, 2006 Just make sure to properly set your where clause so you only delete the record you want.[code]<?php$sql = "DELETE FROM tablename WHERE id = nnn";mysql_query($sql)?>[/code] Link to comment https://forums.phpfreaks.com/topic/27377-delete-entry-from-the-database/#findComment-125190 Share on other sites More sharing options...
craygo Posted November 15, 2006 Share Posted November 15, 2006 Also limit it to 1 just in case. Unless you want to delete all entries where something matches.$sql = "DELETE FROM tablename WHERE id = nnn LIMIT 1";Ray Link to comment https://forums.phpfreaks.com/topic/27377-delete-entry-from-the-database/#findComment-125193 Share on other sites More sharing options...
bryanptcs Posted November 15, 2006 Author Share Posted November 15, 2006 I am not sure I made myself clear the first time, or I am just an idiot and don't know better, but I want the admin to be able to select the entry and push like a delete button of some sort.... Link to comment https://forums.phpfreaks.com/topic/27377-delete-entry-from-the-database/#findComment-125204 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.