-3

Ok so i want to create an update form that basically updates a record from my table but i am not able to do that. I am getting error like :Undefined index Id and all.. Here are my codes.. what is the mistake i am making

<?php

  // PHP code to update data from MySQL database table

  if (isset($_POST['update'])) {

    $hostname = "localhost";
    $username = "root";
    $password = "";
    $databaseName = "winc sports";

    $connect = mysqli_connect($hostname, $username, $password, $databaseName);

    // get values form input text and number

    $id = $_POST['Id'];
    $fname = $_POST['Fname'];
    $lname = $_POST['Lname'];
    $age = $_POST['Age'];
    $country=$_POST['Nationality'];
    $phone=$_POST['PhoneNumber'];
    $email=$_POST['Email'];

    // mysql query to Update data
    $query = "UPDATE `students` SET `Fname`='" . $fname . "',`Lname`='" . $lname . "',`Nationality`='" . $country . "',`PhoneNumber`=" . $phone . ",`Email`='".$email."',`Age`= " . $age . " WHERE `Id` = '" . $id . "'";

    $result = mysqli_query($connect, $query);

    if($result) {
       echo 'Data Updated';
    } else {
       echo 'Data Not Updated';
    }
    mysqli_close($connect);
  }
?>

<!DOCTYPE html>
<html>
  <head>
    <title>PHP INSERT DATA USING PDO</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <form action="updating.php" method="post">
      <input type="text" name="id"  placeholder="Enter new ID"><br><br>
      <input type="text" name="fname"  placeholder="Enter new First Name"><br><br>
      <input type="text" name="lname"  placeholder="Enter new Last Name"><br><br>
      <input type="number" name="age"  placeholder="Enter new age" min="13" max="90"><br><br>
      <input type="text" name="country"  placeholder="Enter new Nationality"><br><br>
      <input type="number" name="phone"  placeholder="Enter new Phone Number"><br><br>
      <input type="text" name="email"  placeholder="Enter new Email"><br><br>
      <input type="submit" name="update" value="update">
    </form>    
  </body>
</html>
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
Hamza Khan
  • 23
  • 4

2 Answers2

-1

The error is probably saying you do not have an index called 'Id' in your POST data.

Try var_dump($_POST) to see what it actually contains.

user744621
  • 147
  • 2
  • 8
-1

Your form is posting id whereas your code is looking for Id; Case matters.

ClickLabs
  • 547
  • 3
  • 5