PHP - Is It Possible To Delete Data From Webpage But Do Not Delete From Database?
Hi,
I wish to find out is there any possible that I can delete some data inside my php website but inside my sql database, the record will still at there?
Thank you.
Similar TutorialsHi all. Here is my scripts which allow user to check multiple rows of data and delete it , but it require select data and click for twice to delete the rows , what should be the error? Code: [Select] <form name="frmSearch" method="post" action="insert-add.php"> <table width="600" border="1"> <tr> <th width="50"> <div align="center">#</div></th> <th width="91"> <div align="center">ID </div></th> <th width="198"> <div align="center">First Name </div></th> <th width="198"> <div align="center">Last Name </div></th> <th width="250"> <div align="center">Mobile Company </div></th> <th width="100"> <div align="center">Cell </div></th> <th width="100"> <div align="center">Workphone </div></th> <th width="100"> <div align="center">Group </div></th> </tr> </form> <? echo "<form name='form1' method='post' action=''>"; while($objResult = mysql_fetch_array($objQuery)) { echo "<tr>"; echo "<td align='center'><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"$objResult[addedrec_ID]\"></td>"; echo "<td>$objResult[addedrec_ID] </td>"; echo "<td>$objResult[FirstName]</td>"; echo "<td>$objResult[LastName] </td>"; echo "<td>$objResult[MobileCompany] </td>"; echo "<td>$objResult[Cell] </td>"; echo "<td>$objResult[WorkPhone] </td>"; echo "<td>$objResult[Custgroup] </td>"; echo "</tr>"; } echo "<td colspan='7' align='center'><input name=\"delete\" type=\"submit\" id=\"delete\" value=\"Delete\">"; if (isset($_POST['delete']) && isset($_POST['checkbox'])) // from button name="delete" { $checkbox = ($_POST['checkbox']); //from name="checkbox[]" $countCheck = count($_POST['checkbox']); for($d=0;$d<$countCheck;$d++) { $del_id = $checkbox[$d]; $sql = "DELETE from UserAddedRecord where addedrec_ID = $del_id"; $result2=mysql_query($sql) or trigger_error(mysql_error());;; } if($result2) { $fgmembersite->GetSelfScript(); } else { echo "Error: ".mysql_error(); } } echo "</form>"; Thanks for every reply. <body> <?php include 'sql.php'; $query = "SELECT * FROM validation"; $result = mysqli_query($con , $query); $rows = mysqli_fetch_assoc($result) ; $totals = mysqli_num_rows($result) ; ?> <div id="css"> <form > <table width="80%" border="0" cellpadding="2" cellspacing="2" > <caption><h2>Personal Details of Customers</h2></caption> <tr class="white"> <td bgcolor="#330033"> </td> <td bgcolor="#330033"> Id Number </td> <td bgcolor="#330033"> Full Name </td> <td bgcolor="#330033"> Email Address </td> <td bgcolor="#330033"> Website </td> <td bgcolor="#330033"> Comment </td> <td bgcolor="#330033"> Time </td> </tr> <?php while($rows=mysqli_fetch_assoc($result) { <tr> <input type="raido" name="ID" value="<?php echo $rows['ID']; ?>" /> <td bgcolor="#FFFFCC"><?php echo $rows['ID'];?></td> <td bgcolor="#FFFFCC"><?php echo $rows['Name'];?> </td> <td bgcolor="#FFFFCC"><?php echo $rows['Email'];?></td> <td bgcolor="#FFFFCC"><?php echo $rows['Website'];?></td> <td bgcolor="#FFFFCC"><?php echo $rows['Comment'];?></td> <td bgcolor="#FFFFCC"><?php echo $rows['Time'];?></td> <td> </td> <td> <a href="delete.php? ID= "$rows[ID]" /"> <input type="submit" name="del" value="Delete" /> </a> <input type="button" name= "edit" value="Edit" /> </td> </tr> }?> </table> </form> </div> </body> Hi guys, I am recent working on my php to delete the information in a database. When I insert the value in the url bar something is like: www.mysite.com/delete.php?myfavorites=what ever it is&user=test. When I inserted the value in the url bar, it did not search for the value in a database to delete it while match with the username in the url bar. Code: [Select] <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbuser'); define('DB_PASSWORD', 'mydbpass'); define('DB_DATABASE', 'mydbname'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $favorites = clean($_GET['favorites']); $username = clean($_GET['user']); if($favorites == '' && $username == '') { // both are empty $errmsg_arr[] = 'Favorites are missing.'; $errflag = true; if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['favorites'])) { $insert[] = 'favorites = \'' . clean($_GET['favorites']) . '\''; } if(isset($_GET['username'])) { $insert[] = 'username = \'' . clean($_GET['username']) . '\''; } if (count($insert)>0) { $names = implode(',',$insert); if($favorites && $username) { mysql_query("DELETE valuedata FROM favorites WHERE username='$username'"); $deleted = mysql_affected_rows(); if($deleted > 0) { echo("The value in favorites are deleted"); } else { echo("failed"); } }else{ echo("failed"); } } } mysql_close($link); ?> If you know how to delete the value in a database for each row while it match with the username in the url, please say so as i would like to know how to do it. Any advise would be much appreciated. Thanks, Mark Hi guys, I need a bit of your help. I have successfully deleted a row in a database after I have inserted the value name with a var function that match with a username in the url bar. Now I would like to delete more than one row, but I don't know how to do this? What I am trying to achieve is to enter the url something like this: www.mysite.com/delete.php?favorites=the value1&use=test It will search for a value in the database and then delete it. But if I add the &favorites=the value2 or more than 2, then search for the values in each row while looking for a username before delete the rows Here's the current code: Code: [Select] <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbuser'); define('DB_PASSWORD', 'mydbpass'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $favorites = clean($_GET['favorites']); $username = clean($_GET['user']); if($favorites == '' && $username == ''){ // both are empty $errmsg_arr[] = 'Both name and email are missing. You must enter one or the other.'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['favorites'])) { $insert[] = 'favorites = \'' . clean($_GET['favorites']) . '\''; } if(($favorites) && isset($username)) { mysql_query("DELETE FROM favorites WHERE username='$username' AND favorites='$favorites'"); $deleted = mysql_affected_rows(); if($deleted > 0) { echo "favorites rows is deleted"; } else { echo("favorites are already deleted"); } } } ?> Any advice would be much appreciated. Thanks, Mark I'm trying to set it so that it will delete an entire populated directory based upon a value in the database then after finishing that to go back and delete that row in the database. my current code is Code: [Select] <?php $page_title = "Central Valley LLC | Photo Addition" ?> <?php include("header.php"); ?> <?php include("nav.html"); ?> <div id="content"> <form action="delprod.php" method="post" enctype="multipart/form-data"> <label for="which">Choose A Product To Remove:</label> <?php $con = mysql_connect("localhost","phoenixi_cv","centraladmin"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("phoenixi_cvproducts", $con); $result = mysql_query("SELECT * FROM Products"); echo "<select name=\"which\">"; while($row = mysql_fetch_array($result)) { echo "<option "; echo "value=\"" . $row['id'] . "\">"; echo $row['Name'] . "</option>"; } echo "</select>"; mysql_close($con); ?> <br /> <input type="submit" name="submit" value="Submit" /> </form> </div><!--#content--> <?php include("footer.html") ?> and the delete script Code: [Select] <?php $con = mysql_connect("localhost","phoenixi_cv","centraladmin"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("phoenixi_cvproducts", $con); $result = mysql_query("SELECT id FROM Products WHERE id=$_POST['which']"); $row = mysql_fetch_array($result) chdir('assets'); chdir('images'); $mydir = $row . '/'; $d = dir($mydir); while($entry = $d->read()) { if($entry!="." && $entry!="..") { unlink($_POST['which'] . '/' . $entry); } } rmdir($mydir); $result = mysql_query("DELETE * FROM Producs WHERE id=$_POST['which']"); ?> Thank you in advance for all your help. any easier ways of approaching this will be welcome as well Hi all, I'am new with PHP, and I need help with this one. I have this XML structu Code: [Select] <?xml version="1.0" encoding="UTF-8"?> <popis> <student> <prezime>Brkic</prezime> <ime>Ivica</ime> <index>D-142</index> </student> <student> <prezime>Cizek</prezime> <ime>Pero</ime> <index>D-143</index> </student> </popis> and I have this PHP code to get all elements from XML file to html table: <?php $doc = new DOMDocument(); $doc->load( 'student.xml' ); $popis = $doc->getElementsByTagName( "student" ); echo <<<EOF <table> <tr> <th width=100 >IME</th> <th width=100>PREZIME</th> <th width=100>INDEX</th> </tr> EOF; foreach( $popis as $student ) { $imena = $student->getElementsByTagName( "ime" ); $ime = $imena->item(0)->nodeValue; $prezimena= $student->getElementsByTagName( "prezime" ); $prezime= $prezimena->item(0)->nodeValue; $indexi = $student->getElementsByTagName( "index" ); $index = $indexi->item(0)->nodeValue; echo <<<EOF <tr> <td>{$ime}</td> <td>{$prezime}</td> <td>{$index}</td> </tr> EOF; } echo '</table>'; ?> Now I would like to know how to delete node from XML document? Something like, when I enter number of node I wanna delete in some iput field, or by searching, what ever, just that I can specify which node to delete... My eng is not very good, hope you understand... ty It echoes only ERROR and i don't know why?can i get some help please? 1.index.php: Code: [Select] ... <tr> <td width="128"><?php echo $row_Recordset1['pacientID']; ?></td> <td width="111"><?php echo $row_Recordset1['nume']; ?></td> <td width="128"><?php echo $row_Recordset1['prenume']; ?></td> <td width="122"><?php echo $row_Recordset1['cnp']; ?></td> <td width="192"><?php echo $row_Recordset1['data']; ?></td> <td width="192"><a href="delete_inregistrare.php?pacientID=<? echo $rows['pacientID']; ?>">Elimiare Pacient</a></td> </tr> ... 2.delete_inregistrare.php Code: [Select] <?php $host="#####"; $username="#####"; $password="######"; $db_name="model_healthcare"; $tbl_name="pacienti"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $pacientID=$_GET['pacientID']; $sql="DELETE FROM $tbl_name WHERE pacientID='$pacientID'"; $result=mysql_query($sql); if($result){ echo "Deleted Successfully"; } else { echo "ERROR"; } mysql_close(); ?> This script add and delete data to the database, it add it correctly but it's not able to delete it. I appreciate somebody more expert to me to help me. thank you. <?php require 'include/db.inc.php'; $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); switch ($_POST['action']) { case 'Add Character': // escape incoming values to protect database $alias = mysql_real_escape_string($_POST['alias'], $db); $real_name = mysql_real_escape_string($_POST['real_name'], $db); $address = mysql_real_escape_string($_POST['address'], $db); $city = mysql_real_escape_string($_POST['city'], $db); $state = mysql_real_escape_string($_POST['state'], $db); $zipcode_id = mysql_real_escape_string($_POST['zipcode_id'], $db); $alignment = ($_POST['alignment'] == 'good') ? 'good' : 'evil'; // add character information into database tables $query = 'INSERT IGNORE INTO comic_zipcode (zipcode_id, city, state) VALUES ("' . $zipcode_id . '", "' . $city . '", "' . $state . '")'; mysql_query($query, $db) or die (mysql_error($db)); $query = 'INSERT INTO comic_lair (lair_id, zipcode_id, address) VALUES (NULL, "' . $zipcode_id . '", "' . $address . '")'; mysql_query($query, $db) or die (mysql_error($db)); // retrieve new lair_id generated by MySQL $lair_id = mysql_insert_id($db); $query = 'INSERT INTO comic_character (character_id, alias, real_name, lair_id, alignment) VALUES (NULL, "' . $alias . '", "' . $real_name . '", ' . $lair_id . ', "' . $alignment . '")'; mysql_query($query, $db) or die (mysql_error($db)); // retrieve new character_id generated by MySQL $character_id = mysql_insert_id($db); if (!empty($_POST['powers'])) { $values = array(); foreach ($_POST['powers'] as $power_id) { $values[] = sprintf('(%d, %d)', $character_id, $power_id); } $query = 'INSERT IGNORE INTO comic_character_power (character_id, power_id) VALUES ' . implode(',', $values); mysql_query($query, $db) or die (mysql_error($db)); } if (!empty($_POST['rivalries'])) { $values = array(); foreach ($_POST['rivalries'] as $rival_id) { $values[] = sprintf('(%d, %d)', $character_id, $rival_id); } // alignment will affect column order $columns = ($alignment = 'good') ? '(hero_id, villain_id)' : '(villain_id, hero_id)'; $query = 'INSERT IGNORE INTO comic_rivalry ' . $columns . ' VALUES ' . implode(',', $values); mysql_query($query, $db) or die (mysql_error($db)); } $redirect = 'list_characters.php'; break; case 'Delete Character': // make sure character_id is a number just to be safe $character_id = (int)$_POST['character_id']; // delete character information from tables $query = 'DELETE FROM c, l USING comic_character c, comic_lair l WHERE c.lair_id = l.lair_id AND c.character_id = ' . $character_id; mysql_query($query, $db) or die (mysql_error($db)); $query = 'DELETE FROM comic_character_power WHERE character_id = ' . $character_id; mysql_query($query, $db) or die (mysql_error($db)); $query = 'DELETE FROM comic_rivalry WHERE hero_id = ' . $character_id . ' OR villain_id = ' . $character_id; mysql_query($query, $db) or die (mysql_error($db)); $redirect = 'list_characters.php'; break; case 'Edit Character': // escape incoming values to protect database $character_id = (int)$_POST['character_id']; $alias = mysql_real_escape_string($_POST['alias'], $db); $real_name = mysql_real_escape_string($_POST['real_name'], $db); $address = mysql_real_escape_string($_POST['address'], $db); $city = mysql_real_escape_string($_POST['city'], $db); $state = mysql_real_escape_string($_POST['state'], $db); $zipcode_id = mysql_real_escape_string($_POST['zipcode_id'], $db); $alignment = ($_POST['alignment'] == 'good') ? 'good' : 'evil'; // update existing character information in tables $query = 'INSERT IGNORE INTO comic_zipcode (zipcode_id, city, state) VALUES ("' . $zipcode_id . '", "' . $city . '", "' . $state . '")'; mysql_query($query, $db) or die (mysql_error($db)); $query = 'UPDATE comic_lair l, comic_character c SET l.zipcode_id = ' . $zipcode_id . ', l.address = "' . $address . '", c.real_name = "' . $real_name . '", c.alias = "' . $alias . '", c.alignment = "' . $alignment . '" WHERE c.character_id = ' . $character_id . ' AND c.lair_id = l.lair_id'; mysql_query($query, $db) or die (mysql_error($db)); $query = 'DELETE FROM comic_character_power WHERE character_id = ' . $character_id; mysql_query($query, $db) or die (mysql_error($db)); if (!empty($_POST['powers'])) { $values = array(); foreach ($_POST['powers'] as $power_id) { $values[] = sprintf('(%d, %d)', $character_id, $power_id); } $query = 'INSERT IGNORE INTO comic_character_power (character_id, power_id) VALUES ' . implode(',', $values); mysql_query($query, $db) or die (mysql_error($db)); } $query = 'DELETE FROM comic_rivalry WHERE hero_id = ' . $character_id . ' OR villain_id = ' . $character_id; mysql_query($query, $db) or die (mysql_error($db)); if (!empty($_POST['rivalries'])) { $values = array(); foreach ($_POST['rivalries'] as $rival_id) { $values[] = sprintf('(%d, %d)', $character_id, $rival_id); } // alignment will affect column order $columns = ($alignment = 'good') ? '(hero_id, villain_id)' : '(villain_id, hero_id)'; $query = 'INSERT IGNORE INTO comic_rivalry ' . $columns . ' VALUES ' . implode(',', $values); mysql_query($query, $db) or die (mysql_error($db)); } $redirect = 'list_characters.php'; break; case 'Delete Selected Powers': if (!empty($_POST['powers'])) { // escape incoming values to protect database-- they should be numeric // values, but just to be safe $powers = implode(',', $_POST['powers']); $powers = mysql_real_escape_string($powers, $db); // delete powers $query = 'DELETE FROM comic_power WHERE power_id IN ("' . $powers . '")'; mysql_query($query, $db) or die (mysql_error($db)); $query = 'DELETE FROM comic_character_power WHERE power_id IN ("' . $powers . '")'; mysql_query($query, $db) or die (mysql_error($db)); } $redirect = 'edit_power.php'; break; case 'Add New Power': // trim and check power to prevent adding blank values $power = trim($_POST['new_power']); if ($power != '') { // escape incoming value $power = mysql_real_escape_string($power, $db); // create new power $query = 'INSERT IGNORE INTO comic_power (power_id, power) VALUES (NULL, "' . $power . '")'; mysql_query($query, $db) or die (mysql_error($db)); } $redirect = 'edit_power.php'; break; default: $redirect = 'list_characters.php'; } header('Location: ' . $redirect); ?> Hi. I have the following code to delete a row when the date has passed. Code: [Select] $today = date("dmY"); $vLongDate = date("dmY", strtotime($thisDate)); if ($today > $vLongDate) { mysql_query("DELETE FROM events WHERE id < '$id'"); } $today is current date, while $vLongDate is the date that is on my database. Am i doing the if statement correctly?? I'm working on an old cms I built a few years ago and for some reason my Delete page isn't working. It has to be something simple I'm overlooking. The database connection is made just above this code and is pulling the thumb and title listed by id. When I push delete I'm getting the cmd=delete&id=2 passed on so something is up with my last if statement (I think). Here is the code: if(!isset($cmd)) { //display all the news $result = mysql_query("select * from verizon order by id"); //run the while loop that grabs all the news scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the enws $title=$r["title"];//take out the title $id=$r["id"];//take out the id $thumb=$r["thumb"]; echo "<div id='delete'> <table> <tr> <td width='350px'><strong>$title</strong></td><td><a href='verizondelete.php?cmd=delete&id=$id'> Delete</a></td> </tr> <td colspan='2' align='center'><img src='../upload/verizon/$thumb'></td> </table> </div><br />"; } } if($cmd=="delete") { $sql = "DELETE FROM verizon WHERE id='$id'"; $result = mysql_query($sql); echo "<h1 style='text-align:center;'>deleted!</h1>"; } } ?> Hello i have a text box and a button on my webpage and want to get the text box to find the username i type into it from the database and when it does it will then go to number and delete any text that is under usernames number field as you can see i have no idea how. Code: [Select] $result = mysql_query("DELETE FROM mytable WHERE username='$username1' AND number='$number1'" ); Code: [Select] <form name="form1" method="post" action="p.php"> <p><input type="submit" value="Enter" /></p> <input name="username" type="text" id="username" size="15" maxlength="35" value="<?php echo $username1; ?>" /> </form> i trying to delete messages out of my message database but im not sure if i'm using this code right or there stuff missing to it :S delete.php <?php require_once('settings.php'); checkLogin('1 2'); $id=$_GET["msgs"]; mysql_query("DELETE messages FROM messages WHERE message_id='$id'"); ?> link usages to delete <a href="delete.php?msgs=<? echo $row['message_id'] ?>">[X]</a> I am trying to find a way of deleting entries from a database after a certain time, and also when there is another field in the database set to 0. i have got so far with the code (will delete when other field is at 0) but nothing happens when i try to add in the time. my code im trying to use is below mysql_query("DELETE FROM temp_users WHERE book_stage='0' and WHERE time() > [creation+60]"); creation being the name of the field in the database that holds the unix time stamp of when the entry was created. i need the entry to be deleted one hour after it was created if the book_stage hasnt been changed to 1 during this hour. many thanks, gavin hello i have this code here to delete people form a call list but it is not deleting form it could I get a some help? the form Code: [Select] <?php include '../config.php'; $query="SELECT * FROM call_list WHERE ecs = 'Jam' order by date desc"; $result=mysql_query($query); echo mysql_error(); //////////////// Now we will display the returned records in side the rows of the table///////// while($row = mysql_fetch_array($result)) { echo " <table id='call_list'> <form name='Call_delet' action='del.php' method='get'> <tr> <td class='call_names'> $row[Fname] $row[Lname] </td> <td class='call_numbers'> $row[phone] </td> <td class='call_email'> $row[email] </td> <td class='call_email'> $row[calltime] </td> <td> <a href='del.php?del=$row[id]'>Del</a> </td> </tr> </form> </table> "; } ?> del.php page Code: [Select] <?php include '../config.php'; if (isset($_GET['id']) && is_numeric($_GET['id'])) { $id = $_GET['id']; $result = mysql_query("DELETE FROM call_list WHERE id=$id") or die(mysql_error()); header("Location: view.php"); } else { echo"dident work"; } ?> Hello PHP Freaks,
I think this is just if else statement problem but im kinda lose to it.
The whole code is succesful in deleting the data from database the problem is i wanted to put a script that remind the user if he/she really sure to delete the data. By the way this is the script.
<?php // connect to the database include 'Connect.php'; // confirm that the 'student_id' variable has been set if (isset($_GET['student_id']) && is_numeric($_GET['student_id'])) { // get the 'student_id' variable from the URL $student_id = $_GET['student_id']; // delete record from database if ($stmt = $mysql->prepare("DELETE FROM student_information WHERE student_id = ? LIMIT 1")) { $stmt->bind_param("i",$student_id); $stmt->execute(); $stmt->close(); } else { echo "ERROR: could not prepare SQL statement."; } $mysql->close(); // redirect user after delete is successful header("Location: Admin_Home.php"); } else // if the 'student_id' variable isn't set, redirect the user { header("Location: Admin_Home.php"); } ?>Please help me modify these codes and where to put the missing line/s of codes. Hi, I have created a table named multipath with database name "mydatabase".. I have created a table to store the data in the tables. Check boxes are created to select one row each to delete. But I am unable to delete the rows. Why?? Here's my PHP code <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="mydatabase"; // Database name $tbl_name="multipath"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF">#</td> <td align="center" bgcolor="#FFFFFF"><strong>Path ID</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Path</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> <td bgcolor="#FFFFFF"><?php echo $rows['path_id']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['path']; ?></td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <? // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $result = mysql_query($sql); } // if successful redirect to delete.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table> Please u can reply me at sawyerford007ATgmail DOTcom I have a PHP CRUD application and when i delete a row i need it to delete from the MySQL and the image from the uploads folder as well. I have researched and tried dozens of ways with the unlink option but nothing works. If i take out the unlink from my code it will delete fine from the DB. I am new to coding and PHP so any help would be awesome. The file_path is correct. The uploads is the name of the folder where the image is stored and the $_POST["image"] is the column name in MySQL where the image name is stored. delete.php <?php //start PHP session session_start(); if (!isset($_SESSION['success'])) { header("Location: login_page.php"); die(); } // check if value was posted if($_POST){ // include database and object file include_once 'config/database.php'; $file_path = 'uploads/' . $_POST["image"]; if(unlink($file_path)) { // delete query $query = "DELETE FROM myDBname WHERE id = ?"; $stmt = $con->prepare($query); $stmt->bindParam(1, $_POST['object_id']); } if($stmt->execute()){ // redirect to read records page and // tell the user record was deleted echo "Record was deleted."; }else{ echo "Unable to delete record."; } } ?> this is the delete button code echo "<a delete-id='{$id}' class='btn btn-danger delete-object'>"; echo "<span class='glyphicon glyphicon-remove'></span> Delete"; echo "</a>"; This is the javascript for the delete button as well. // delete record $(document).on('click', '.delete-object', function(){ var id = $(this).attr('delete-id'); bootbox.confirm({ message: "<h4>Are you sure?</h4>", buttons: { confirm: { label: '<span class="glyphicon glyphicon-ok"></span> Yes', className: 'btn-danger' }, cancel: { label: '<span class="glyphicon glyphicon-remove"></span> No', className: 'btn-primary' } }, callback: function (result) { if(result==true){ $.post('delete.php', { object_id: id }, function(data){ location.reload(); }).fail(function() { alert('Unable to delete.'); }); } } }); return false; });
If you need any other info that would help you help me just let me know and i will get that in here ASAP. Thanks again for any help on this. Hey everyone i having some trouble updating records in my databse using checkbox from a form. I can update the records, but i can't figure out how to remove records at the same time. I've posted the code that i'm using to update below foreach($_POST['tag'] as $k=>$l){ $tag = mysql_real_escape_string($_POST['tag'][$k]); $check_tags = mysql_query("SELECT * FROM file_cats WHERE category='$tag'") or die(mysql_error()); $num_tags = mysql_num_rows($check_tags); if($num_tags==0){ $add_tag = mysql_query("INSERT into file_cats (fileID, category) VALUES('$fileID', '$tag')") or die(mysql_error()); } } This code inserts new records based on what the user checked. If it exist it won't add a new row, if it doesn't exist then it will add a new row. but like i said how can remove a row if the box is no longer checked? I've tried using foreach(empty($_POST['tag'] as $k => $l){ $tag = empty($_POST['tag']['$k']; } But that didn't work basically nothing was deleted. Has anyone done something like this before? if so i would appreciate any help you could give No errors come up when I run this but it just doesn't delete the entry. <? $rec = $_POST['stock']; if (!$rec){ die ('Please eneter a stock number'); } $link = mysql_connect("xxxx","xxx","xxx"); mysql_select_db('wadkin', $link) or die( "Unable to select database"); mysql_query("DELETE FROM 'wadkin'.'stocklist' WHERE 'stocklist'.'Stock Number' = $rec") or die ('Query failed'); mysql_close($link); ?> Can someone please correct, (btw I have also tried... "DELETE FROM stocklist WHERE 'Stock Number' = $rec") Not sure if I wrote this correctly but if anyone can just scan through this and let me know if it's fine. Thanks Code: [Select] <?php $db_link=mysql_connect ("localhost", "", "") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("commercial"); $sql = "DELETE FROM `comm` WHERE `date_created` < DATE_SUB(NOW(),INTERVAL 14 DAY)"; $result2 = mysql_query($sql, $db_link); ?> |