-2

I'm making an application to store student records and edit and delete them but the server is issuing undefined variable error (on $name, $school_name, $roll_no, $result variable lines) although the variables are defined and i have used echo to check and all the variables seem to work fine ... kindly help me

<form action = 'edit.php?edit_form=<?php echo $edit_id; ?>' method = 'post'>
        <table align = "center" width = "500" border = "5">
            <tr>
                <td colspan = "5"> <h1 align = "center">Update Record</h1> </td>
            </tr>
            <tr>
                <td align = "right">Student Name:</td>
                <td> <input type = "text" name = "name" value = "<?php echo $name; ?>"> </td>
            </tr>
            <tr>
                <td align = "right">School Name:</td>
                <td> <input type = "text" name = "school" value = "<?php echo $school_name; ?>"> </td>
            </tr>
            <tr>
                <td align = "right">Roll No:</td>
                <td> <input type = "text" name = "roll_no" value = "<?php echo $roll_no; ?>"> </td>
            </tr>
            <tr>
                <td align = "right">Result:</td>
                <td> <input type = "text" name = "result" value = "<?php echo $result; ?>"> </td>
            </tr>
            <tr>
                <td colspan = "5" align = "center"> <input type = "submit" name = "update" value = "Update Now"> </td>
            </tr>
        </table>

    </form>
Kamran
  • 15
  • 3
  • 1
    *What* variable is undefined? What is the exact message you're getting? – tadman Jul 26 '16 at 06:46
  • Where are they defined? – jeanj Jul 26 '16 at 06:49
  • Can you provide us the piece of code with the filling of variable? – pilec Jul 26 '16 at 06:50
  • The message is Notice: Undefined variable: name in C:\xampp\htdocs\php\edit.php on line 36 and the same message on other variables names which i have mentioned in the description – Kamran Jul 26 '16 at 06:52
  • 1
    @Kamran please edit the question and put the PHP code and the notice code there. No one will even try to read such a big code sample from comments. – Ruslan Bes Jul 26 '16 at 07:21
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn Jul 26 '16 at 07:48

1 Answers1

0

That actually makes sense, because you are try to set variables for rendering form, which is before submitting, so there is no POST data inside request.

Fow making things work, you have to select this data from database, something like:

// mysql_connect is deprecated, use mysqli_connect instead
$link = mysqli_connect("localhost", "root", "password");
mysqli_select_db($link, "school");

if (!isset($_GET['edit_form']) || !is_numeric($_GET['edit_form'])) {
    // first validate user input from GET request
    die('please provide us valid student id');
}

// after successful updating showing confirmation message
if (isset($_GET['show_message'])) {
    echo "Data has been updated";
}

$edit_id = $_GET['edit_form'];

//using prepare statement for avoiding SQL injection
$statement = $link->prepare('SELECT student_name, school_name, roll_no, result FROM students WHERE id = ?');


// i because student id should be integer
$statement->bind_param('i', $edit_id);

$statement->execute();
$statement->store_result();

// fill every needy variable from query
$statement->bind_result($name, $school_name, $roll_no, $result);
$statement->fetch();

if (isset($_POST['update'])) {
    $name = $_POST['name'];
    $school_name = $_POST['school'];
    $roll_no = $_POST['roll_no'];
    $result = $_POST['result'];
    $query = "update students set student_name = ?, school_name = ?, roll_no = ?, result = ? where ID=?";

    // once again preparate query for avoiding SQL injection
    $statement = $link->prepare($query);

    // s for string variable, ssisi - 1st variable is string, 2nd variable is string, 3rd variable is integer ...
    $statement->bind_param('ssisi', $name, $school_name, $roll_no, $result, $edit_id);

    if ($statement->execute()) {
        header("location: index.php?edit_form=$edit_id&show_message=1");
    }
} ?>
pilec
  • 493
  • 4
  • 9