0

Following code extracting values from MySQL table using PHP. I want to add an update button in the last column of this table. I have done some coding, but when I am clicking on update its showing white screen. I want HTML table to become editable when the user clicks on update button and after submitting the values will be changed in MySQL table. Here is code:

<?php

$connection = mysqli_connect ('localhost', 'user', 'password', 'testdb');

if (!$connection){
    echo 'Not connected to server';
}

$select_db = mysqli_select_db($connection, 'testdb');

if (!$select_db){
    echo 'Not connected to database';
}

$sql= "SELECT * FROM students";

$query = mysqli_query($connection, $sql);

if (!$query) {
    die ('SQL Error: ' . mysqli_error($connection));
}

if(isset($_POST['update'])){
$UpdateQuery = "UPDATE `students` SET `elected_subject`='$ElectedSubject'";               
mysql_query($UpdateQuery, $con);
};
?>


    <h1><strong>Student Record</strong></h1>
    <table id = "result" class="data-table">
        <caption class="title"></caption>
        <thead>
            <tr>

                <th>Student ID</th>
                <th>Student Name</th>
                <th>Father Name</th>
                <th>Class</th>
                <th>Selected Subject</th>
                <th>View/Update</th>


            </tr>
        </thead>
        <tbody>
        <?php
        $no     = 1;
        $total  = 0;
        while ($row = mysqli_fetch_array($query))
        {
            echo "<form action=update_students.php method=post>";
            $student  = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
            echo '<tr>

                    <td>'.$row['student_id'].'</td>
                    <td>'.$row['student_name'].'</td>
                    <td>'.$row['father_name'].'</td>
                    <td>'.$row['class'].'</td>
                    <td>'.$row['elected_subject'].'</td>
                    <td>' . "<input type='submit' name='update' value='update'>" . '</td>

                </tr>';
            $total += $row['stu_id'];
            $no++;
        }?>
        </tbody>

    </table>

update_students.php is the same file which is having this code.

babar junaid
  • 99
  • 1
  • 12
Akhtar
  • 91
  • 11
  • 1
    `$ElectedSubject` is undefined. Where should that come from? You are not sending any values in the form except the submit button. – Jeff Oct 17 '18 at 10:36
  • Also add apostrophes around your values inside the `form action` and `method`. This might interfere and is a common practice. – Sanguinary Oct 17 '18 at 10:38
  • @Jeff firstly i was trying to update it from another file by post method. It was working fine, I want to display all record of mysql table where selected subject column is empty, now i want user to click on update and the corresponding row become editable. After input from user table will be updated with subject name. – Akhtar Oct 17 '18 at 10:39
  • @Akhtar Jeff had no way of knowing that. That information should be stated in the question in itself. All relevant information is necessary. – Sanguinary Oct 17 '18 at 10:42
  • @Sanguinary actually in first instance i was just testing to update table by having 1 input box named selected_subject. It was updating my input in mysql table. Now above coding is extracting all data from mysql table in an html table, its also showing update button and empty column named selected subject. i just want user to to click update and enter subject name in empty table. – Akhtar Oct 17 '18 at 11:00
  • _“but when I am clicking on update its showing white screen”_ - well then enable proper error reporting first of all! https://stackoverflow.com/questions/1475297/phps-white-screen-of-death – misorude Oct 17 '18 at 11:03
  • @misorude after adding some code for error reporting its showing 'Undefined variable: ElectedSubject Fatal error: – Akhtar Oct 18 '18 at 04:25
  • So now you confirmed, what was said in the first comment already … Also, your HTML is faulty, you can not nest a form in a table like that - the form either has to go around the whole table, or reside completely inside a single cell. – misorude Oct 18 '18 at 06:27
  • @misorude i am completely blank. Can you please amend my code. – Akhtar Oct 18 '18 at 06:43

0 Answers0