Home » Php » Why is my gap using both insert and update?

Why is my gap using both insert and update?

Posted by: admin June 30, 2018 Leave a comment

Questions:

I want to update and insert using an if-else statement, but the query always goes “Insert” data. No “update” anyway. My code is below:

<form method="post" action="">
    <label>ID: </label> <input type="text" name="id">
    <label>Subject: </label> <input type="text" name="subject">
    <input type="submit" name="submit">
</form>

<?php 
$conn = new mysqli("localhost", "root", "", "zidm"); 

$id=$_POST['id'];
$subject=$_POST['subject'];


if (isset($_POST['submit'])){
      $sql = "UPDATE exam  SET $subject = '$marks'  WHERE id = '$id'";    
    mysqli_query($conn,$sql);
    echo "Data Updated";
  }
  else {
 $sql="INSERT INTO exam ($subject, x_sess, x_class, x_exam, x_roll) VALUES ('$marks', '$ex_sess', '$class', '$exam', '$roll')";
    mysqli_query($conn,$sql);
    echo "Data Inserted";
  }

  ?>
Answers: