Shadow_Walker Posted May 14, 2014 Share Posted May 14, 2014 Hello PHP friends, With the help of 2 members here, namely Guru and Clarkey i manage to finalize the Search and this is part of the search.php codes. { $student_id = $row['student_id']; $searchResults .= "<tr>\n"; $searchResults .= " <td>{$row['LRN']}</td>\n"; $searchResults .= " <td>{$row['first_name']}</td>\n"; $searchResults .= " <td>{$row['last_name']}</td>\n"; $searchResults .= " <td>{$row['grade']}</td>\n"; $searchResults .= " <td>{$row['section']}</td>\n"; $searchResults .= " <td><a href='View_Profile.php?id={$student_id}'>View</a> </td>\n"; $searchResults .= " <td><a href='Admin_Edit_Student_Info.php?id={$student_id}'>Update</a></td>\n"; $searchResults .= " <td><a href='Delete.php?id={$student_id}'>Delete</a></td>\n"; $searchResults .= "</tr>\n"; } As we can see we have 3 target links here the VIEW/UPDATE/DELETE. My concern today is to create the Delete.php which if we are going to click the delete. The student selected will be deleted from the database. Thank you very much for reading. Please comment and never hesitate to give suggestions. By the way this is my present codes in connection, please comment and suggest here. //Connect to MySQL Server include 'Connect.php'; mysql_connect($host, $dbusername, $dbpassword); //Select Database mysql_select_db($dbname) or die(mysql_error()); // Escape User Input to help prevent SQL Injection $first_name = mysql_real_escape_string(trim($_GET['first_name'])); // Retrieve data from Query $query = "SELECT * FROM student_information WHERE first_name LIKE '%{$first_name}%'"; $result = mysql_query($query) or die(mysql_error()); :-) Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/ Share on other sites More sharing options...
Barand Posted May 14, 2014 Share Posted May 14, 2014 Basically, $id = intval($_GET['id']); and the query is DELETE FROM student_information WHERE student_id = $id Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479428 Share on other sites More sharing options...
Shadow_Walker Posted May 14, 2014 Author Share Posted May 14, 2014 Hello Barand, Presently this is my delete.php <?php session_start(); $session_id = $_SESSION['admin_id']; if($session_id == null){ header("location:Admin_Home.php"); die(); } include 'Connect.php'; $id = intval($_GET['id']); $query = ("DELETE FROM student_information WHERE student_id = $id'"); $res = mysql_query($query); echo "<script language='javascript' type='text/javascript'>alert('Successfully Deleted!')</script>"; echo "<script language='javascript' type='text/javascript'>window.open('Admin_Home.php','_self')</script>"; ?> All is going well but exasperately the account is still not deleted. Please anyone i need your suggestion to modify my codes Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479438 Share on other sites More sharing options...
Barand Posted May 14, 2014 Share Posted May 14, 2014 Check if any errors $res = mysql_query($query)or die (mysql_error() . "<pre>$query</pre>"); Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479440 Share on other sites More sharing options...
Shadow_Walker Posted May 14, 2014 Author Share Posted May 14, 2014 to mc_gyver: sometimes this account is hard to log in so i created the other one. Apology if i violated some rules. I acknowledge my fault. to barand: "<pre>$query</pre>"); what does it mean, can you suggest something in betwen $query please, Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479454 Share on other sites More sharing options...
Barand Posted May 14, 2014 Share Posted May 14, 2014 If there is an error when the query is submitted, the change I suggested will output the error message plus your query (in the variable $query) exactly as it was submitted. What are you expecting in between $query? Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479461 Share on other sites More sharing options...
Shadow_Walker Posted May 14, 2014 Author Share Posted May 14, 2014 Ahhh okay,,there was no error displayed during the clicking of DELETE link only a script saying Successfully Deleted. but the specific students data is not yet deleted in the database. ahhmnn sorry i dont expect in between query i just misunderstood the "<pre>$query</pre>"); Can you tell me how to do the codes to display the error message or how to delete the speciific student data in the database. Please. Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479463 Share on other sites More sharing options...
Barand Posted May 14, 2014 Share Posted May 14, 2014 Can you tell me how to do the codes to display the error message Check if any errors $res = mysql_query($query)or die (mysql_error() . "<pre>$query</pre>"); Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479467 Share on other sites More sharing options...
Shadow_Walker Posted May 15, 2014 Author Share Posted May 15, 2014 Hi Guru Barand, i have followed all your advices and this is now the code <?php session_start(); $session_id = $_SESSION['admin_id']; if($session_id == null){ header("location:Admin_Home.php"); die(); } include 'Connect.php'; $id = intval($_GET['id']); $query = ("DELETE FROM student_information WHERE student_id = '$id'"); $res = mysql_query($query)or die (mysql_error() . "<pre>$query</pre>"); echo "<script language='javascript' type='text/javascript'>alert('Successfully Deleted!')</script>"; echo "<script language='javascript' type='text/javascript'>window.open('Admin_Home.php','_self')</script>"; ?> BUT as i have notice the specific student is still not deleted in the database. What do you think is still wrong with our codes? or do you think we need some coding adjustment also in other pages? Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479510 Share on other sites More sharing options...
Barand Posted May 15, 2014 Share Posted May 15, 2014 What is the value of the student_id in the record you are trying to delete? Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479544 Share on other sites More sharing options...
Shadow_Walker Posted May 16, 2014 Author Share Posted May 16, 2014 Hi Guru Barand,, I think that is something i have missed cause honestly i don't know how to declare its value in the record. Yeah putting value of student_id is necessary to delete specific student. Pls, would you care to teach me create those codes. I think this is the missing link to solve the case. Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479646 Share on other sites More sharing options...
Jacques1 Posted May 16, 2014 Share Posted May 16, 2014 “Guru” is his user title, not his first name. So I think you can stop calling him “Guru Barand”. Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479650 Share on other sites More sharing options...
Barand Posted May 16, 2014 Share Posted May 16, 2014 I shall ask one more time only What is the value of the student_id in the record you are trying to delete? Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479668 Share on other sites More sharing options...
Shadow_Walker Posted May 16, 2014 Author Share Posted May 16, 2014 The URL shows this when i click the DELETE link http://localhost/a/Admin_delete.php?id=hans Im sorry but my problem is i think i didn't have the codes to give value to student_id in the record.Maybe this is one of the reason previous codes appear to be unsuccesful.. When i click the DELETE link. This is the notification i get Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed and an alert says :1 rows deleted. As i checked the database...yes, the particular student data is deleted, but why i am getting this notification? Please advise. Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479691 Share on other sites More sharing options...
Shadow_Walker Posted May 16, 2014 Author Share Posted May 16, 2014 to Jacques: Im sorry if im wrong but i just think GURU is a title name given to teacher or a master. And im not referring it as his first name. http://en.wikipedia.org/wiki/Guru I can choose to call him Guru, Sir, Sensei, Teacher or Master and if it is wrong to use that idea in this site, I humbly ask for apology to the admins. Sorry for this reply admin. Im just trying to be obedient and respectful here. but anyways i am more worried about my PHP thing here that the definition of GURU :-) Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479694 Share on other sites More sharing options...
Clarkey Posted May 16, 2014 Share Posted May 16, 2014 Advanced Member Shadow_Walker, You say you get the error message of... Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed This means that you are using mysql_* functions that are too old. You need to upgrade to mysqli or PDO. I find it weird that you say the record is deleted, even though you get this error message. I would also suggest in getting into the habbit of putting error checks in place, such as mysql_error() or an exception block when you are developing your scripts, as they are designed to give you insight on why your code hasn't worked. More often that not, you've simply missed a character or something silly like that. In your first post on this thread, the link to delete a record is Delete.php but in your last post, the URL shows Admin_delete.php. What have you changed? I think it's best if you repost your latest code here, with your latest findings and we can help you troubleshoot. Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479699 Share on other sites More sharing options...
Solution Shadow_Walker Posted May 16, 2014 Author Solution Share Posted May 16, 2014 Hello Clarkey, Yes i just change the filename for easy remembering.Some of the codes have changes maybe because of luck this problem i posted in this thread has been solved but in respect to you and to the others helped me in this thread and for the other that may encounter also this kind of problem in the future. I will post the solution codes for the problem. Again, Thank you for the read,,you've been also a help in my other posts and hopefully in my future posts :-) This is the code that solve the thread. <?php ini_set('display_errors', true); error_reporting(E_ALL); session_start(); // $session_id = $_SESSION['admin_id']; // redundant if(empty($_SESSION['admin_id'])) { header("location: Admin_Home.php"); die(); } // change to require() so we're sure it's loaded require 'Connect.php'; $id = intval($_GET['id']); // no parens need to define string: $query = "DELETE FROM student_information WHERE student_id = $id"; // not using return value, and add some debug info mysql_query($query) or die(mysql_error().PHP_EOL.$query); // let's see if anything actually happened... $rowsDeleted = mysql_affected_rows(); if($rowsDeleted == 0) { error_log("Nothing deleted for this query:".PHP_EOL.$query); } echo "<script language='javascript' type='text/javascript'>alert('$rowsDeleted row(s) deleted!')</script>"; echo "<script language='javascript' type='text/javascript'>window.open('Admin_Home.php','_self')</script>"; Acknowledge also the effort and help of Sir Barand and Sir NOgdog Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479700 Share on other sites More sharing options...
Clarkey Posted May 16, 2014 Share Posted May 16, 2014 Ok cool. Mark this thread as answered if your issue is resolved Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479701 Share on other sites More sharing options...
Barand Posted May 16, 2014 Share Posted May 16, 2014 Id ares are typically unique integer values. Yours is alpha so you need to escape it with real_escape_string() and not intval() as I have. Here is the code using mysqli <?php $db = new mysqli('$host', 'user', 'password', 'database'); $id = $db->real_escape_string($_GET['id']); $query = ("DELETE FROM student_information WHERE student_id = '$id'"); $res = $db->query($query)or die ($db->error . "<pre>$query</pre>"); echo "<script language='javascript' type='text/javascript'>alert('Successfully Deleted!')</script>"; echo "<script language='javascript' type='text/javascript'>window.open('Admin_Home.php','_self')</script>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/288477-search-and-delete/#findComment-1479708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.