If i delete recored 1 next when i want to view the table i shall see recored 1 deleted and in that place recored 2 shall be named as recored one.By record i mean id

<?php 

//var_dump($_POST); i used it to check if $_POST array contains the values or not.
if(isset($_REQUEST['page']) && $_REQUEST['page'] == 'insert'){

    /*Requesting page means the page which we came from and of course it expects insert page*/
    $reqeusting_page = $_POST['page'];
    $studentName     = $_POST['name'];
    $fatherName      = $_POST['father'];
    $schoolName      = $_POST['school'];
    $rollNumber      = $_POST['rollno'];
    $className       = $_POST['class'];

    if($studentName != NULL && 
        $fatherName != NULL && 
        $schoolName != NULL && 
        $rollNumber != NULL && 
        $className  != NULL){

            /*Now need to insert data into database*/
            include("database.php");
            $con = connect_database();
            $status = insert($con, $studentName, $fatherName, $schoolName, $rollNumber, $className);
            //close_database_connection($con);
            if($status == TRUE){
                header("Location: /student_example/?page=insert&success=Data Inserted Successfully!!");
            }else{
                header("Location: /student_example/?page=insert&error=Error: Insertion Error");
            }
    }else{
        header("Location: /student_example/?page=insert&error=Error: All fields mendatory");
    }

}else if(isset($_REQUEST['page']) && $_REQUEST['page'] == 'delete'){

    $student_id = $_REQUEST['id'];

    /*Now need to delete data from database*/
    include("database.php");
    $con = connect_database();
    $status = delete_record($con, $student_id);
    //close_database_connection($con);
    if($status == TRUE){
        header("Location: /student_example/?page=view&success=Data Deleted Successfully!!");

    }else{
        header("Location: /student_example/?page=view&error=Error: Deletion Error");
    }



}else if(isset($_REQUEST['page']) && $_REQUEST['page'] == 'edit'){

    $id              = $_POST['id'];
    $reqeusting_page = $_POST['page'];
    $studentName     = $_POST['name'];
    $fatherName      = $_POST['father'];
    $schoolName      = $_POST['school'];
    $rollNumber      = $_POST['rollno'];
    $className       = $_POST['class'];

    if($id != NULL && 
            $studentName != NULL && 
            $fatherName != NULL && 
            $schoolName != NULL && 
            $rollNumber != NULL && 
            $className  != NULL){


            /*here you need to update the record now*/
            include("database.php");
            $con = connect_database();
            $status = edit_record($con, $id, $studentName, $fatherName, $schoolName, $rollNumber, $className);
            if($status == TRUE){
                header("Location: /student_example/?page=view&success=Record id: [".$id."] Edited Successfully!!");
            }else{
                header("Location: /student_example/?page=view&error=Error: Edition Error");
            }       
    }else{
        header("Location: /student_example/?page=view&error=Error: All fields mendatory");
    }
}




?>

Recommended Answers

All 4 Replies

What I gather from your question is that you want to alter the id of record #2 to match the id of record #1 after deleting record #1.

It's a best practice in relational database design to leave intelligence and meaning out of the primary key. It is also best practice to never modify a primary key after assigning it.

From your example I'm assuming you want to re-use a student number. Re-usable student numbers would have meaning and should therefore not be the primary key. The primary key should be a reference to a row in your table, not a reference to a student for an arbitrary period in time.

That problem would easily be solved by adding a new column for a primary key. However, what I don't understand is why you would delete a student then assign his/her student number to a student who is already enrolled. In general I don't think student numbers are supposed to change during a student's enrollment. You could give that number to the next student who enrolls, but even that seems unnecessary unless you expect an insane amount of students. For instance, if you gave out numbers with 7-9 digits, even with exclusion of certain numbers you would still have plenty left for new students.

But if this is just a mental exercise, what would you do with the student number of student #2? would that go to student #3? Or perhaps I misunderstood entirely.

YEs i also thought same that id field is unique and shall not be changed .But if i delete id 1 the view looks odd.So my intention is when i delete id 1 id 2 shall be names as id1 .But since it is a primary key therefore cant be changed.student id 3 will go to student 2 if i delte id1.I think Roll no shall be made primary then

But if i delete id 1 the view looks odd

That could be solved outside of the database, just print a number indicating the student is #1, #2, #3 etc. in the table (even though in the database the student can have a primary key of 26658). Then, if you don't print the student number in the next column but one or two over, it won't be confusing or look weird.

If roll no has no meaning then you could make it primary yes. But you can also add a new column, for instance key that acts as primary key and allows you to attach meaning to roll no.

If you actually need the table on your page to reflect the order of enrollment you could add a date_added column to your database and insert the current date during insertion. Then you can retrieve the results sorted by date_added and print numbers starting at 1, 2, 3 to indicate that order.

I did this and it worked

if($status == TRUE){
        header("Location: /student_example/?page=view&success=Data Deleted Successfully!!");
        $student_id = 0;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.