1

I am facing issue regarding to Bootstrap modal.

When I use a simple form then my data is fetched and also I can edit and update it too. But when I implemented it with Bootstrap modal then it showing me error like this:

"Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\KFUEIT_RYK\Student_Data.php on line 84"

Error occurs due to this:

<label>Student Roll No</label> 
<input type="text" value="<?php echo $data_select['s_Roll_No'];?>" name="Student_RollNo" class="form-control" required> 
<label>Student Name</label> –

My code:

<?php
$selected_record = $_GET['s_id'];
$query_select = mysqli_query($con, "SELECT * FROM `student_table` where s_id ='$selected_record'  ");
$data_select = mysqli_fetch_array($query_select);
?> 
<!-- Update Code -->
<?php
if (isset($_POST['Update_Record'])) 
{
    $student_rollno = $_POST['Student_RollNo'];
    $student_name=$_POST['Student_Name'];
    $student_father=$_POST['Student_Father'];
    $student_class=$_POST['Student_Class'];
    $student_email=$_POST['Student_Email'];
    $query_update = mysqli_query($con, "UPDATE `student_table` set 
        s_Roll_No = '$student_rollno',
        s_Name = '$student_name',
        s_Father = '$student_father',
        s_Class='$student_class',
        s_Email='$student_email'
        where s_id = '$selected_record'");
    header("location: Student_Data.php");
}
?>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria- labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="" method="POST">
                    <label>Student Roll No</label>
                    <input type="text" value="<?php echo $data_select['s_Roll_No'];?>" name="Student_RollNo" class="form-control" required>
                    <label>Student Name</label>
                    <input type="text" value="<?php echo $data_select['s_Name']; ?>" name="Student_Name" class="form-control" required>
                    <label>Student Father Name</label>
                    <input type="text" value="<?php echo $data_select['s_Father']; ?>" name="Student_Father" class="form-control" required>
                    <label>Student Class</label>
                    <input type="text" value="<?php echo $data_select['s_Class']; ?>" name="Student_Class" class="form-control" required>
                    <label>Student Email</label>
                    <input type="text" value="<?php echo $data_select['s_Email']; ?>" name="Student_Email" class="form-control" required>
                    <button type="submit" name="Update_Record" class="btn btn-primary">Update</button>
                </form>
            </div>
        </div>
    </div>
</div>
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • If `$_POST['Update_Record']` is set, your code does a redirect to a new page, and in that case, `$data_select` will not be set. – Don't Panic Jul 11 '20 at 04:31

1 Answers1

0

I suspect that it could be due to accessing $data_select array for the input fields (e.g. $data_select['s_Roll_No']).

if the mysql query returned a null for $data_select (which is likely the case when $_GET['s_id'] is not set or no such record in the database), then you'll be accessing array offset of a null variable.

You should first check if the array is null before accessing it.

Ginko
  • 13
  • 3
  • It's difficult for me to find out actually what's going on there. I have more then 15 records in database and when I check it with simple form that is without modal then it works fine but it is giving me error with modal. Don't know why it is giving me error – Muhammad Irfan Zafar Jul 10 '20 at 09:31