www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Search and Delete

  1. #1
    Join Date
    May 2014
    Posts
    26

    Search and Delete

    Hello web developers,,

    i have manage to made a searchbox that displays the followng

    LRN First Name Last Name Grade Section View Update Delete
    2 Jane Doe 8 A View Update Delete
    33 Jane Denice 7 C View Update Delete

    Basically my problem is i just wanted that when we click the Delete link,, it will directly delete the students info in the database. How will i do that?? This has been my codes so far which needed your comments and correction.

    PHP 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() 
            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>";
    ?>
    i would be glad if someone can correct my codes, Thank you!!!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,538
    Looks like an extraneous single quote here:
    Code:
    $query = ("DELETE FROM student_information WHERE student_id = $id'");
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2014
    Posts
    26
    hello Nogdog,,

    Probably , i am just too sleepy last night,,yeah your right, i miss that single qoute.

    $query = ("DELETE FROM student_information WHERE student_id = '$id'");

    Now it doesn't give me notifications anymore.

    BUT i still need your help although as we can see no notifications and errors but still the data is still not deleted in the database.
    What do you think is wrong with codes, Please advise.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,538
    Start with all the usual debugging stuff:

    Make sure all error reporting is on for now:
    PHP Code:
    <?php
    ini_set
    ('display_errors'true); // set to false for live site
    error_reporting(E_ALL);
    Use some combination of error_log(), echo, print_r(), etc. to check variable/array values.

    Check return values from anything that could possibly go wrong (which is basically everything ), and if an unexpected result occurs, generate some debug info.

    You can use mysql_affected_rows() to find out how many rows it thinks were deleted. If it comes up with 0, maybe output your query at that point to see what it really tried to do.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2014
    Posts
    26
    Hi Sir Nogdog,

    I used the codes you have given but there is still no error notifications but there is alert saying SUCCESFULLY DELETED.

    Whoah!! Sorry but i am still beginner. Honestly, i dont know how to convert your advices to codes.

    Use some combination of error_log(), echo, print_r(), etc. to check variable/array values.
    I hope we could do something to revised and modified my codes.

  6. #6
    Join Date
    May 2014
    Posts
    10
    Add query like this:-

    $query = ("DELETE FROM student_information WHERE student_id = '$id'");
    or
    $query = ("DELETE FROM student_information WHERE student_id = $id");

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,538
    PHP Code:
    <?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>";
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    May 2014
    Posts
    26
    Thanks for the help PHP koyal,,


    I already tried that codes but seems it doesn't work here. I can't figure it, why.

  9. #9
    Join Date
    May 2014
    Posts
    26
    Thank you very much Sir Nogdog,

    I just read it now, i'll try it after having my breakfast. I'll update you soon.

  10. #10
    Join Date
    May 2014
    Posts
    26

    Lightbulb

    Hello Sir Nogdog,

    Thank you very much for the complete effort, literally i mean "complete" detection of possible errors.
    i have gotten this URL

    http://localhost/a/A...udent_Info.php?id=hans

    ,,and the page at localhost says "SUCCESFULLY DELETED" but unfortunately the student's data of student hans is still in the database.

    I think we can't be passing in a student's first name in the URL -- a first name like hans could be shared between multiple students, so we can't be sure that we are deleting the right student. Instead,i think we should be using a numeric id that is unique per student.

    Well, i cant be sure though but if i'm right, can you teach me to code it in a particular page.

  11. #11
    Join Date
    May 2014
    Posts
    26
    Hello there Sir Nogdog,

    Your codes are doing well now,
    Ignore my last 2 comments maybe the xammp server didnt update.

    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.

  12. #12
    Join Date
    May 2014
    Posts
    26
    hello Sir Nogdog,,

    i dont know what happen,, but your codes just working now perfectly!!!

    This thread has been solved.

    Kodus to Sir Nogdog.

    God bless!!!

  13. #13
    Uhm, silly question on my part... this is 2014, not 2004 -- WHY are you still using mysql_ functions?

  14. #14
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,538
    Quote Originally Posted by deathshadow View Post
    Uhm, silly question on my part... this is 2014, not 2004 -- WHY are you still using mysql_ functions?
    Probably because 91.5%* of the freely available PHP scripts out there do, unfortunately. Personally, I use PDO for everything now -- but then I use PostgreSQL whenever I can, too.
    ___________
    * Remember that 63.7% of all statistics are made up.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  15. #15
    Quote Originally Posted by NogDog View Post
    Probably because 91.5%* of the freely available PHP scripts out there do, unfortunately.
    Which is depressing since for ~8 years we've been told to stop using them, to the point that two years ago they FINALLY added giant red warning boxes telling us not to do so...

    Gonna be real fun when PHP 6 drops and everyone runs around like chickens with their heads cut off as said functions either disappear, or just start spamming the error logs like crazy. (It's still unclear which that's going to be). Of course, you'll have the halfwits who can't be bothered to join us in this century who will try to run PHP 5.x alongside 6.x, just like they did with 4.x alongside 5.x.

    Either that or we end up with the scenario we have going on right now, where people won't even update to 5.5 because their broken decade out of date code (likely written in the past six months) throws errors and warnings about things being "deprecated".

    Quote Originally Posted by NogDog View Post
    * Remember that 63.7% of all statistics are made up.
    Laugh, nothing like pulling numbers out one's backside, right? As I often say, "Never trust a percentage".
    Last edited by deathshadow; 05-16-2014 at 03:55 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles