PHP - How To Delete A Record?
New to php so I'm having trouble with deleting a record from my php table.
This is my code for my page where the records are being displayed. I have a delete statement that is not working properly. Also, I was thinking maybe I need a array to delete records? Please help! Code: [Select] <?php require_once('database.php'); session_start(); if (isset($_POST['add_grade'])) { $query = "INSERT INTO grades (student_id, grade_type, grade_name, grade_points) "; $query .= "VALUES (:student_id, :grade_type, :grade_name, :grade_points) "; $statement = $db->prepare($query); $statement->bindValue (':student_id', $_SESSION['student_id']); $statement->bindValue (':grade_type', $_POST['grade_type']); $statement->bindValue (':grade_name', $_POST['grade_name']); $statement->bindValue (':grade_points', $_POST['grade_point']); $statement->execute(); $statement->closeCursor(); if (isset($_POST['remove'])) { foreach($_POST['delete'] as $delete_id) { $query = "DELETE FROM grades WHERE grade_id = $delete_id"; mysqli_query($dbc, $query) or die ('can\'t delete user'); } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>View Course Grades</title> <link rel="stylesheet" type="text/css" href="main.css" /> </head> <body> <?php $student_name = $_SESSION['student_name']; $student_id = $_SESSION['student_id']; $query = "SELECT * FROM grades WHERE student_id = :student_id "; $statement = $db->prepare($query); $statement->bindValue (':student_id', $student_id); $statement->execute(); $grades = $statement->fetchAll(); $statement->closeCursor(); echo "<h1>Show Grades for $student_name </h1>"; foreach ($grades as $grade) { echo $grade['grade_type'] . " " . $grade['grade_name']. " " . $grade['grade_points'] . "<br />"; } $query = "select*from se266"; $result = mysql_query($dsn, $query); while($row = mysql_fetch_array($result)) { echo '<input type="checkbox" value="' .$row['grade_id'] . '"name="delete[]" />'; echo ' ' .$row['grade_type']; echo ' ' .$row['grade_name']; echo '<br />'; } ?> <div id="content"> <!-- display a table of products --> <table> <tr> <th>Grade Type</th> <th>Grade Name</th> <th>Grade Points</th> <th>Remove</th> </tr> <?php foreach ($grades as $grade) : ?> <tr> <td><?php echo $grade['grade_type']; ?></td> <td><?php echo $grade['grade_name']; ?></td> <td><?php echo $grade['grade_points']; ?></td> <td><form action="grades.php" method="post"> <input type="submit" name="remove" value="Delete" /> </form></td> </tr> <?php endforeach; ?> </table> </div> </div> <div id="footer"> </div> <form name="grades" method="post" action="grades.php"> <p>Grade Type<SELECT NAME="grade_type"> <OPTION VALUE="Mid-Term">Mid-Term <OPTION VALUE="Final">Final <OPTION VALUE="Lab">Lab </SELECT> <br> Grade Name:<input type="text" name="grade_name" value=""><br /> Grade Points:<input type="text" name="grade_point" value=""> <input type="submit" name="add_grade" value="Add Grade"> </form> </table> </body> </html> Similar TutorialsHello: I am making a dynamic food menu system for a project so a client can manage his food and drink menus. I gave him the ability to add, edit, and delete menus. However, for the "Delete Menu" option, I would like to first check and see if a Category (menuCatID) or a Menu Item (menuItemID) is associated to the Main Menu (menuID) and - if so - to *NOT* allow that Menu to be deleted. I would like to send them back to the a_Menus.php page and display a message like "You must delete all Categories and Menu Items associated with this Menu before the Menu can be deleted." This is the code: a_Menus.php Code: [Select] <a href="a_MenusDelete.php?menuID=".$menuData['menuID'].">Delete Menu</a> a_MenusDelete.php Code: [Select] <?php include('../include/myConn.php'); include('../include/myCodeLib.php'); include('include/myCheckLogin.php'); $menuID = $_REQUEST['menuID']; mysql_query("DELETE FROM theMenus WHERE menuID = $menuID"); header("Location: a_Menus.php"); ?> <html> <head> <title></title> </head> <body> </body> </html> How would I be able to accomplish this? Thanks. Hi I've been scouring the net and I cant find anything that works. Can someone tell me how to put a delete button next to each record? Any help is appreciated, thank you! Code: [Select] $term = strtolower ($_POST['term']); $sql = mysql_query("select * from $table where first like '%$term%' or last like '%$term%'"); while ($row = mysql_fetch_array($sql)){ echo 'ID: '.$row['id']; echo '<br/> first: '.$row['first']; echo '<br/> last: '.$row['last']; echo '<br/> phone: '.$row['phone']; echo '<br/> mobile: '.$row['mobile']; echo '<br/> fax: '.$row['fax']; echo '<br/> email: '.$row['email']; echo '<br/> web: '.$row['web']; echo '<br/><br/>'; } ?> </p> Hi All, I have a simple 'delete record' code that is not working. Would someone please take a gander at it and let me know if they see anything wrong that would prevent it from actually deleting a record. Code: [Select] <?php //connects to the database include ('db_connect.php'); //post variable $pk = $_POST['pk']; //MySQL code to query ---- 'contacts' = table & 'primary_key' = record id # $deleteContact = "DELETE FROM `contacts` WHERE `primary_key` = ".$pk." LIMIT 1"; //excutes query to MySQL $res = mysqli_query($link,$deleteContact); //close connection to database mysqli_close($link); ?> Any help would be great. Thank you. I have a form that submits a record and saves it to the database, I've got that working already, I'm trying to figure out if there is a cleaner way to delete a record with a button that gets a value from the unique key, in this case 'id' here is how the delete button looks like: // get info from table $query = "SELECT id, Title, Message FROM table_name"; $result = mysql_query($query); //displaying all data while($row = mysql_fetch_assoc($result)) { echo" <div class='status'><h3>{$row['Title']} <br></h3>" . " <h5>{$row['Message']} <br><br></h5>"; $id = $row['id']; //trying to get unique key from database //delete button echo "</div> <form action='delete.php' method='post' /> <input type='hidden' name='delete' value='yes' /> <input type='hidden' name='id' value='$id' /> <input type='submit' value='remove' /></form>"; } As you can see, i'm trying to give $id a unique value but for some reason i'm not getting it. The delete.php code looks like this: if (isset($_POST['delete']))// check if delete was clicked { $query = "DELETE FROM table_name WHERE id='$id'"; echo "$id Deleted successfully"; } elseif(!mysql_query($query, $db_server)) { echo "DELETE failed: $query<br />" . myql_error() . "<br /><br />"; } mysql_close(); I'm new to php and still learning so, if you think there are other ways to do this, please let me know, the button won't do anything to the data. Thanks in advance Right now I redirect to index page after I delete a record. However I am looking to make it so that I can delete a record without redirecting the page. I know this can be accomplised using Ajax. I have spent countless hours before trying to make it work, but it did not work.
So here is a basic setup I created. Can you please update it with ajax code so that I can see how it's done properly?
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home Page</title> </head> <body> <div class="record" > <a href="record.php?id=<?php echo $record_id ?>"><?php echo $record_name; ?></a> <div class="delete-record"> <a href="delete.php">Delete Record</a> </div> </div> </body> </html> Edited by man5, 18 August 2014 - 08:55 PM. Hi 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,
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.
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 I'm trying to update records in a Db. I have a record called "classOffering" and I have associated records for that offering called "classSessions". You have 1 classOffering for 1 to 6 classSessions. The process I use has the user create the classOffering and say how many classSession there will be then I create how ever many sessions they say. I then display the classSessions so they can be edited and updated in the Db. I can't seem to update and set more than one record even though there are more. I know I need to use a while or foreach statement, but I can't seem to do it. Here's my code so far: $sessSet = "UPDATE classSessions SET startDate= '$startDate', startTime= '$startTime', time= '$time' WHERE classSessions.sessionId = $sessionId "; Thanks for any help. Is it no problem to give the SUM of integers in a column, but is it also possible to give the SUM of integers in a record (automatically)?
I am creating a sticky topic feature for my forum and dont know how to keep all topics marked sticky at the top of the list generated by the while loop. Any one have any ideas? They are ordered by the post date column. Here is the query: $posts_info_query = $db->query("SELECT p.post_id, p.topic_id, p.forum_id, p.post_poster, p.post_subject, p.post_content, p.post_time, p.post_edit_by, p.post_edit_date, p.post_edit_num, p.post_approved, p.post_quoting, m.user_id, m.user_username, m.user_group, m.user_regdate, m.user_birthday, m.user_online, m.user_sex, m.user_location, m.user_show_sex, m.user_show_location, m.user_show_status, m.user_avatar, m.user_sig, m.user_posts FROM ".DB_PREFIX."posts as p LEFT JOIN ".DB_PREFIX."members as m ON p.post_poster = m.user_username WHERE p.topic_id = '$topic_id' ORDER BY p.post_time ASC LIMIT $start, $limit") or trigger_error("SQL", E_USER_ERROR); Thanks Hi guys
I am using the code below to show distinct months using one query then for each month I am trying to show a list of fixtures for the month, the problem I have is only one month is showing.
where am i going wrong?
<?php //find distinct months $sql="SELECT DISTINCT MONTH( match_date ) AS MONTH FROM tbl_fixtures WHERE season_id =$current_season UNION SELECT DISTINCT MONTH( match_date ) AS MONTH FROM tbl_gp WHERE season_id =$current_season"; $result=mysqli_query($dbConn, $sql); if(mysqli_num_rows($result)>0){ while($row=mysqli_fetch_assoc($result)){ $month=$row['MONTH']; echo '<div class="fixtureMonth"> <h2>'.$months[$month].'</h2> </div> <div class="fixtures">'; //find fixtures for month $sql="SELECT Opposition, match_date, competition, tbl_clubs.club_name AS club, image, HomeAway, team, opposition_points, points FROM tbl_fixtures INNER JOIN tbl_clubs ON tbl_fixtures.club = tbl_clubs.club_id WHERE season_id =$current_season and MONTH(match_date)=$month UNION SELECT Opposition, match_date, \"SGP\" AS competition, \"SGP\" AS club, \"sgp.png\" AS image, \"\" as HomeAway, \"\" as team, \"\" as opposition_points, points FROM tbl_gp WHERE season_id =$current_season and MONTH(match_date)=$month ORDER BY match_date"; $result=mysqli_query($dbConn,$sql)or die(mysqli_error($dbConn)); if(mysqli_num_rows($result)==0){ echo no_fixtures_found; } else{ echo '<table class="fixturesTable">'; while($row=mysqli_fetch_assoc($result)){ echo'<tr> <td>'.date("d-M-Y",strtotime($row['match_date'])).'</td> <td><img src="'.$shopConfig['url'].'images/'.$row['image'].'"/></td> <td>'.$row['Opposition'].'</td> <td>'.$row['HomeAway'].'</td> <td><b>'.$row['team'].'</b>-'.$row['opposition_points'].'</td> <td>'.$row['team'].'</td> </tr>'; } echo '</table>'; } echo '</div>'; } } ?> Hey, I'm trying to check if a username is unique, but for some reason none of the methods that I've looked up seem to work. I'm aiming for the code to check if a username is unique, and if get the user to input the name again. $get = "SELECT `i` FROM `m` WHERE `i`=\"$r\""; $checkResults = mysql_query($getRandomness); while(mysql_num_row($checkResults) > 0) { //some code... //check again $get = "SELECT `i` FROM `m` WHERE `i`=$r"; $checkResults = mysql_query($get); } The error comes from the while line, but it comes up during the first check because the record is empty. This is the error: "Fatal error: Call to undefined function mysql_num_row()" Hi guys, Doing a small project and i'm pretty new to php/mysql so hoping someone can help. Also i wasn't sure if this should be in php section or mysql, so apologises if it's in the wrong section. Basically... I have a form with a table in it with the fields (id, model, current stock level, new stock level) The table is populated using a mysql query to get the data from 1 table in a database. In the new stock column there's a simple text box to put a number in. Sooo like (ID) (Model) (current stock level) (new stock level) 1 AAA 3 [ ] 2 BBB 1 [ ] 3 CCC 4 [ ] A user will then put a new number in the 'new stock level' text box and when it gets submitted the new number replaces the number in 'current stock level' Basically i know how to do it if it was just the one record, but how do i get it to update all the ones with numbers in the new stock level column at the same time, and ignore it if it doesn't have anything entered in the text box? appreciate any help! thanks! Code: [Select] $sql = "SELECT poster_id from phpbb_posts WHERE topic_id = ".$topic_id." "; $result = $db->sql_query($sql); //same as mysql_query $row = $db->sql_fetchrow($result); // same as mysql_fetch array. var_dump($row); With this code (i commented the query function for you) im getting Code: [Select] array 'poster_id' => string '59' (length=2) When I run this into SQL in phpmyadmin I'm getting Code: [Select] 59 56 //this is what I want I need to get all of the records, not just the first one. I have even tried using replacing the function with mysql_query and mysql_fetch_array any ideas? It's a bit late, so I may just be making a simple mistake. Thanks! I'm having an issue updating a records. The insert and delete functions are working fine. The $submit variable is being passed 'Update' ~ that is working. This is some extremely old code that has migrated to a new server and is no longer working. Code: [Select] <?php $id = $_GET["id"]; $delete = $_GET["submit"]; $sortorder = $_POST["sortorder"]; $name = $_POST["name"]; $content = $_POST["content"]; $submit = $_POST["submit"]; require'../include/maindb.php'; if($submit=="Submit") { $sql="INSERT INTO biography (id, sortorder, name, content) VALUES (NULL, '".$sortorder."', '".nl2br(addslashes(trim($name)))."', '".htmlentities(addslashes(trim($content)))."')"; } if ($submit == "Update") { $sql="UPDATE biography SET sortorder='".$sortorder."', name='".nl2br(addslashes(trim($name)))."', content='".htmlentities(addslashes(trim($content)))."' WHERE id='".$id."'"; } if ($delete=="Delete") { $sql="DELETE FROM biography WHERE id='".$id."'"; } mysql_query($sql); echo(mysql_error()); header('Location: index.php'); ?> sir i m new in php i need some help i want to go next record by clicking subbmit button here is my code: <?php mysql_connect('localhost','root',''); mysql_select_db('exam'); $sql="select * from examination"; $result=mysql_query($sql); echo $result; echo"<table><tr>"; $i = 1; while ($row = mysql_fetch_assoc($result)) { if(isset($_POST['bttn'])) { // here i want to go next record echo "<td>"; echo $row['qid']; echo "</td>"; print "</tr>"; $i++; } } Please help me Hi, I have a simple mySQL database and I would like to update using a simple html form. One of the field is "accepted" and is type "boolean". I have a texbox where I will write the record id and another button which will change the "accepted" value from 0 to 1 for that record id. The problem is that I don't know how to start.... Thanks in advance Sergio How to pull just I record of a column out of the 20 columns? This is not showing the results. $SomeVar = 'pets'; $query = "SELECT field1 FROM db1 WHERE id1 = '".$SomeVar."'"; $results=mysql_query($link); echo $link; |