PHP - Deleting An Entry From Mysql Without Deleting All Other Entries Echod
I have a bad way with words, so if the title doesnt make much sense nor my post, I apolagize!
But I am using a while loop and mysql_fetch_array to echo all information in a table in mysql. With each entry, I have included a button that executes a delete query for that entry. Code: [Select] $delquery="DELETE FROM requests WHERE user = '".$row['user']."'"; mysql_query($delquery,$link2); echo "<font size='19px' color='#009933'>User deleted from request database.When I click the button, the message is echod in every entry listed on the page, and nothing happens. I figured Id use delete from the username, so that way mysql will know which one to delete... but Idk when I think about it thats dumb and makes no sense. Im stuck. This side of the application im trying to make is the admin side where I can process requests. After I process the request I delete it from the database so I know I processed it. I have no idea what query to use for this. Anybody know how I could do this? Thanks Similar TutorialsI'm creating a newsletter and the unsubscribe isn't deleting the database entry like I'm asking it to. Everything else works fine, it even successfully says the user has been removed, but it doesn't actually delete the database entry. I've spent two days trying to figure out why. Here's the code:
Newsletter sign up: <?php //DB Connect Info $servername = ""; $database = ""; $username = ""; $password = ""; // Create connection $conn = mysqli_connect($servername, $username, $password, $database); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } /*$createTable = $conn->prepare ("CREATE TABLE IF NOT EXISTS email_user ( id int(11) NOT NULL AUTO_INCREMENT, email varchar(200) NOT NULL, hash varchar(250) NOT NULL, PRIMARY KEY (id) )"); $createTable->execute(); */ function input_security($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } $email = input_security($_POST['email']); $insertData = input_security($insertData); if(isset($_POST['submit'])) { extract($_POST); if($email!="") : $email=mysqli_real_escape_string($conn,$email); $emailval = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/'; if(preg_match($emailval, $email)) : $db_check=$conn->query("SELECT * FROM email_user WHERE email='$email'"); $count=mysqli_num_rows($db_check); if($count< 1) : $hash=md5($email.time()); $link = '/unsubscribe.php?key='.$hash; // Change your domain $fetch=$conn->query("INSERT INTO email_user(email,hash) VALUES('$email','$hash')"); $to="$email"; //change to ur mail address $strSubject="Maintenance Fee Relief, LLC | Email Subscription"; $message = '<p>Thank you for subscribing with us.</p>' ; $message .= '<p>Click here to unsubscribe your email : <a href="'.$link.'">unsubscribe</p>' ; $headers = 'MIME-Version: 1.0'."\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; $headers .= "From: info@"; $mail_sent=mail($to, $strSubject, $message, $headers); $msg_sucess="Your request has been accepted!."; else : $msg="This $email email address is already subscribe with us."; endif; else : $msg="Please enter your valid email id"; endif; else : $msg="Please fill all mandatory fields"; endif; } ?> <div class="newsletter-sign-up-header-form"> <div id="logerror"><?php echo @$msg; ?><?php echo @$msg_sucess; ?></div> <form method="post"> <span><input type="email" name="email" placeholder="Email Address - Join Newsletter" class="newsletter-sign-up-header-email" required></span> <span><button name="submit" value="submit" title="Submit" class="newsletter-sign-up-header-submit-button">Submit</button></span> </form> </div> <?php //DB Connect Info $servername = ""; $database = ""; $username = ""; $password = ""; // Create connection $conn = mysqli_connect($servername, $username, $password, $database); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?> <?php if(@$_GET['key']!=""): $hash=mysqli_real_escape_string($conn,$_GET['key']); $fetch=$conn->query("SELECT * FROM email_user WHERE hash = '$hash'"); $count=mysqli_num_rows($fetch); if($count==1) : $row=mysqli_fetch_array($fetch); $conn->query("DELETE email_user WHERE id='$user_id'"); $msg="Your email id unsubscribe with us"; else : $msg="Please click valid link."; endif; else : header("Location:404.php"); endif; ?> <!doctype html> <html lang="en"> <head> <title>Unsubscribe</title> </head> <body> <div align="center"> <h2><?php echo $msg; ?></h2> <a href="https://www.--.com">--.com</a> </div> Quote
Ok I am trying to delete some rows from a database. I have done this before with success so I thought I could use the same method just modify it a little and it would work. Man was I wrong. Any help on this is much appreciated below is my code. Code for the Query to get the data from the database and for displaying the results in a table: Code: [Select] //GET DATA $sql = "SELECT * FROM `".$tblname."` ORDER BY $orderby $sort LIMIT $startrow,$limit"; $result = mysql_query($sql) or die(mysql_error()); $result2 = mysql_query($sql); //START TABLE AND TABLE HEADER echo "<form name='form1' method='post' action=''><table style='font-size:9.5px;'>\n<tr><th>Delete</th>"; $array = mysql_fetch_assoc($result); foreach ($array as $key=>$value) { if($config['nicefields']){ $field = str_replace("_"," ",$key); $field = ucwords($field); } $field = columnSortArrows($key,$field,$orderby,$sort); echo "<th>" . $field . "</th>\n"; } echo "</tr>\n"; //reset result pointer mysql_data_seek($result,0); //start first row style $tr_class = "class='odd'"; //LOOP TABLE ROWS while($row = mysql_fetch_assoc($result)){ echo "<tr ".$tr_class.">\n<td><a href='remove_rec.php?id=". $rows['ID'] . "'><p>Delete</p></a>"; echo "</td>"; foreach ($row as $field=>$value) { echo "<td>" . $value . "</td>\n"; } echo "</tr>\n"; //switch row style if($tr_class == "class='odd'"){ $tr_class = "class='even'"; }else{ $tr_class = "class='odd'"; } } //END TABLE echo "</table>\n</form>"; Here is the code that should delete the row: Code: [Select] $tblname = 'tablename'; // get value of id that sent from address bar $id=$_GET['ID']; // Delete data in mysql from row that has this id $sql="DELETE FROM $tblname WHERE ID='$id'"; $result=mysql_query($sql); // if successfully deleted if($result){ echo "Deleted Successfully"; echo "<BR>"; echo "<a href='leads3.php'>Back to Results</a>"; } else { echo "ERROR"; } When I click on the link to delete a record it redirects me to the appropriate page with the "Deleted Successfully" message but when I go to view the results the row was not deleted. Any help on this would, again, be greatly appreciated. I've tried reading through some of the threads but couldnt understand some of them. I've made a newsfeed script which works how i want it to. Now i want to add the function to delete a row from the database from an "admin panel" on the website. So far i have this: <?php include("includes.php"); doConnect(); $get_news = "SELECT id, title, text, DATE_FORMAT(datetime, '%e %b %Y at %T') AS datetime FROM newsfeed ORDER BY datetime DESC"; $result= mysqli_query($mysqli, $get_news) or die(mysqli_error($mysqli)); while ($row = mysqli_fetch_array($result)) { echo '<strong><font size="3">'. $row['title'] .' </font></strong><br/><font size="3">'. $row['text'] .'</font><br/><font size="2">'. $row['datetime'] .'</font><br/><br/><a href="delnews.php?del_id=' .$row['id']. '"> <strong>DELETE</strong></a>';} ?> then my delnews.php is: <?php include("includes.php"); doConnect(); $query = "DELETE FROM newsfeed WHERE id = "$_POST['id']""; $result = mysql_query($query); echo "The data has been deleted."; ?> I believe the problem is $_POST['id']. i've tried different things in there but none work. It displays the echo line but doesnt actually delete anything. I am new to php so this may be a stupid mistake, but try and play nice! Thanks How would I make a script that would delete any row in a MySQL table that was more than 7 days old? Hey guys, first post and I've just returned to PHP after about 4 years of no coding, so be gentle! In a nutshell I'm creating a photo album and I've pretty much got the majority of it complete, apart from a few tweaks and the obvious ongoing development. I'm at the stage now where I need to moderate images being uploaded, so I've made an admin only script which displays the uploaded images with links that say approve and delete. Uploaded images are stored in uploads/ which are left there until i move them to img/, plus the filename is stored in mysql so I can "<img src='../uploads/".$row['filename']."' width='200'>". Now, I would like to make the Approve button move the image from uploads/ to img/ and I'd like the delete button to remove both the entry from MySQL and the file from the uploads folder and I'm not too sure on how to make it work. Here's what I have so far in the mod.php file (mod for moderation) Code: [Select] $image = mysql_query("SELECT * FROM images WHERE id"); while($row = mysql_fetch_assoc($image)) { echo " <table> <td> <tr> <img src='../uploads/".$row['filename']."' width='200'><br /> </tr> </td> <td> <tr> <a href=''>Approve</a> <a href=''>Delete</a> </tr> </td> </table> "; } You'll have to ignore the table tags, I'm still getting used to positioning items on the screen lol. Any clues would be greatly appreciated Live long and prosper. Hi, I am having difficulty deleting rows in my table using check boxes. I have all the check boxes displaying for each row and a delete button below the table. However when i click on the delete button it isnt deleting the checked rows, i have displayed the code below that i am using; Code: [Select] <?php $result = mysql_query("SELECT * FROM contact ORDER BY msg_id ASC"); echo "<table border='1'> <tr> <th>Delete</th> <th>Message ID</th> <th>Name</th> <th>Email</th> <th>Message</th> </tr>"; while($row = mysql_fetch_array($result)) { ?> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['del_id']; ?>"></td> <?php echo "<td>" . $row['msg_id'] . "</td>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['msg'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <?php // Check if delete button active, start this if(isset($_GET['delete'])) { for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM contact WHERE id=".$_GET['del_id']; $result = mysql_query($sql); } // if successful redirect to delete_multiple.php if($result){ } } mysql_close(); ?> Please help Thank you I want to delete rows in a table of my database using check-box. Here are my codes below: Code: [Select] <?php $con = mysql_connect('localhost', 'root', '') or die ('Connection Failed'); mysql_select_db('img', $con) or die ('Connection Failed'); $display = mysql_query("SELECT * FROM photos WHERE email='$lemail'"); echo '<input type="submit" value="Delete" name="del"/>'; echo "<table> <tr> <th>#</th> <th>Images</th> <th>Image description</th> <th>Delete</th> </tr>"; while($row = mysql_fetch_array($display)) { echo "<tr>"; echo "<td>".$row['img_ID']."</td>"; echo "<td><img src='folder/".$row['imaged']."' alt='alt text' width='100' height='100' class='thumb'/> </td>"; echo "<td>".$row['image_description']."</td>"; echo '<td><input type="checkbox" name="delete[]" value="'.$row['img_ID'].'"/></td>'; echo "</tr>"; } echo "</table>"; echo "</form>"; if (isset($_POST['delete'])) { $del = $row['img_ID']; for($i=0;$i<count($_POST["delete"]);$i++) { if($_POST["delete"][$i] != "") { $str = "DELETE FROM photos WHERE img_ID='$del' "; mysql_query($str); echo "Record Deleted."; } } } mysql_close($connect); ?> Here are the problems: 1/ Not working at all, I mean no image is being deleted 2/ At then end, I display a message that the record has been deleted, but if I check multiple checkbox, it keeps writing the message "Records deleted" multiple times My images are stored in a folder while its details in database... Help, thank you This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=334042.0 Could anyone tell me why this is not deleting the userip and post id from the database at all. Thanks Code: [Select] <?php include('dbcon.php'); if($_REQUEST['postId']) { $userip = $_SERVER['REMOTE_ADDR']; mysql_query("update collapsed_likes set likes=likes-1 where post_id= ".$_REQUEST['postId']); mysql_query("delete from collapsed_ip where userip=".$userip." AND post_id = ".$_REQUEST['postId']); $total_likes = mysql_query("SELECT * FROM collapsed_likes where post_id = ".$_REQUEST['postId']." "); $likes = mysql_fetch_array($total_likes); $likes = $likes['likes']; } echo $likes; ?> I'm just trying to get suggestions on what people think would be the best way to go with this. With the bottom part of this card where it s tarts working with the foreach loop its taking what was a UL and for each of each LI's and updating a table with its new data however this way isn't going to work as this is editing a user. Because there could be 5 in the database but the new edit user has only 4 lis so I'm sure you could all see the issue here. What I'm thinking would be best is to have it DELETE the existing characterIDs that have WHERE handler_ID = $handlerID and then just do a new INSERT statement. Anyone else think this would be the best way to go about this? Code: [Select] if (mysqli_num_rows($Qresult) == 0) { $query = "UPDATE `handlers` SET `userName` = '".$userName."', `password`='".$password."', `firstName`='".$firstName."', `lastName`='".$lastName."', `statusID`='".$statusID."', `isAdmin`='".$admin."', `defaultCharID`='".$defaultCharID."', `email`='".$email."' WHERE `ID` = '".$handlerID."'"; mysqli_query($dbc, $query); foreach ($characterIDs as $cID) { $query2 = "UPDATE `handler_characters` SET `handler_id` = '".$handlerID."', `characterID` = '".$cID."'"; mysqli_query($dbc, $query2); } $result = "good"; } Hi, this is the first time I've asked for some help from a forum. I guess this code has sort of got me stumped. So, I have a system where a user can enter a number. The number goes into a mysql database. A random number is also generated and it is added into another column. So, I want users to be able to delete the number using the random number. I've shown it to them, and I need help working my delete script. Code: [Select] <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("a8784hos_adopts", $con); $adopt = $_POST['adoptid']; $uniquecode = $_POST['uniquecode']; $result = mysql_query("SELECT uniquecode, id FROM id WHERE id='$adopt'"); mysql_fetch_array($result); if (($adopt == $row['id']) || ($uniquecode == $row['uniquecode'])){ mysql_query("DELETE FROM id WHERE id='$adopt'"); echo 'Done! <br/><br/><a href="tester.php">Go back....</a>'; } else { echo 'Bad unique code. <br/><br/><a href="tester.php">Go back....</a>'; } ?> So, the random number is uniquecode in the database, and the number is id. However, when I try this code, it always tell me I have a bad unique code. What am I doing wrong? Thanks. Does anyone have a code that will automatically delete stuff out of the database after 30 days of being in there...? I am sorry even i didn't say hello to forum but i need this snippet very urgently. i have Zero knowledge of PHP but unfortunately i need a small code as follow. I'm trying to delete the <identifier> node of some XML, using the following code; unsuccessfully.
Can someone help me identify where I'm going wrong? TIA
$newXML = ""; $xmlString = Fedora_API::callGetDatastreamContents($this->pid, 'MODS', true); $doc = DOMDocument::loadXML($xmlString); $xpath = new DOMXPath($doc); $fieldNodeList = $xpath->query("/identifier"); foreach ($fieldNodeList as $node) { $fieldNodeList->removeChild($node); } $newXML = $doc->SaveXML(); Hi, I gave the following php script: #!/usr/bin/php -f <?php require_once(dirname(dirname(__FILE__))."/modules/common/common.config.inc.php"); $path = $conf["root"]."/media/images/tricouri/tmp/"; $command = "rm -Rf".$path."*"; if(exec($command)) { echo "Folder cleared\n"; } else { echo "Folder clear failed\n"; } ?> I want to delete some .png files from the ./tmp folder and I receive a rm error: Quote rm: invalid option -- / Try `rm --help' for more information. Folder clear failed The rm -rf command sintax is ok on the linux bash, but from php i receive the error. Php safe mode is off. Can someone help, please ? Hi. What am I doing wrong he Code: [Select] mysql_query("DELETE FROM availability WHERE id = '$_POST['id']'"); I am able to echo the id but I must be entering the post item incorrectly into the delete string. Can anyone show me my mistake? Thanks Hi, When i launch my code i get the following php error: Warning: unlink(../files/"yCUuH0223e3d6d046acb58678fbb54042e0fe.pdf") [function.unlink]: Invalid argument in C:\xampp\htdocs\intra\VVS\admin\fakture-edit.php on line 74 But in the dir: C:\xampp\htdocs\intra\VVS\files\ The file exists.. here's my php code: Code: [Select] $id = mysql_real_escape_string($_GET['id']); $fil = mysql_real_escape_string($_GET['fil']); $result2 = mysql_query("SELECT * FROM fakture WHERE id=('$id') LIMIT 1"); $row2 = mysql_fetch_array($result2); if ($_GET["op"] == "process") { if (!unlink('../files/"'.$fil.'"')) I use this this same exact delete for users to delete their own account with no problem for some reason this code below is deleting all my rows in each table Code: [Select] <?php if(isset($_POST['delete'])) { // this is a check box you need to check in order to confirm if(empty($_POST['confirm'])){ echo "You need to check confirm"; }else{ mysql_query("DELETE FROM `users` WHERE `goauld`= ".(int)($_POST['delete_goauld'])); mysql_query("DELETE FROM `game` WHERE `goauld`= ".(int)($_POST['delete_goauld'])); echo "$delete_goauld was deleted<br />\n"; } } ?> There are many ways to delete everything after a character in a string. I am looking for a simple way to delete everything after a given word. I'm trying to delete rows in the database, based on what checkboxes are left empty by the user... What i have is small application in which a user can add an item to the database and give it tags... for example there is "apple" and it has the tags of "fruit, red, round" What i want is for the user to be able to edit those tags, so the user can remove "fruit" and add "sweet" instead or just remove "fruit" all at the same time. The code i have, somewhat works... This code below loops through the checkboxes and should remove the items that are no longer checked, by going through the database and grabbing all the tags and them checking it against those that are still checked and remove the ones that it didn't find checked...?? However whats happening is when i unchecked say "red" it removes all the other items..so if i had "red, fruit, round' and i want to remove "red" it leaves red but removes the rest. foreach($_POST['tag'] as $r=>$n){ $t = mysql_real_escape_string($_POST['tag'][$r]); $get_tag2 = mysql_query("SELECT * FROM file_cats WHERE fileID='$fileID'") or die(mysql_error()); while($t2 = mysql_fetch_assoc($get_tag2)){ $tmp_tag1 = $t2['category']; if($t==$tmp_tag1){ $remove_tag = mysql_query("DELETE FROM file_cats WHERE fileID='$fileID' AND category='$t' LIMIT 1") or die(mysql_error()); } } } This following piece of code just adds a new tag...however i want it to check to see if that tags already exist in the database, if it does then don't add it just add the new ones.. foreach($_POST['tag'] as $k=>$l){ $tag = mysql_real_escape_string($_POST['tag'][$k]); $add_tag = mysql_query("INSERT into file_cats (fileID, category) VALUES('$fileID', '$tag')") or die(mysql_error()); } My final problem is that i can't remove items if the second snippet of code is there, i have to remove those lines of code in order to remove items, however i can add items with both loops there. I'm basically stuck and hope that someone here can shed some light on what i'm doing wrong, because i know i'm doing something wrong i just can't find it Anyway thank you in advanced |