Jump to content

Search and Delete


Shadow_Walker
Go to solution Solved by Shadow_Walker,

Recommended Posts

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());

:-)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

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>";

?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.