0

I have created an HTML form after submitting it below php code executes. The problem is that every time, page is refreshed or rerun, the if condition statement appears without clicking the Save button and null values are being stored in database table. Please help me how can I resolve it?

<?php 
    include 'connection.php';
    if ($_POST['submit'] == 'Save')
    {

       $student_no=$_POST['student_no'];
       $student_name=$_POST['student_name'];
       $sqlQuery= "INSERT INTO student_info (student_no, student_name) VALUES('$student_no','$student_name');";

       if(mysql_query($sqlQuery, $con))
       {
           echo "Data Successfully Saved";
       }
       else 
           die("Connection Error:". mysql_error());
   }
   mysql_close($con);
?>
tejashsoni111
  • 1,405
  • 1
  • 18
  • 34
  • http://stackoverflow.com/questions/5690541/best-way-to-avoid-the-submit-due-to-a-refresh-of-the-page – iJade Dec 23 '14 at 10:14
  • 2
    After post request you should do redirect. – Oleksandr T. Dec 23 '14 at 10:15
  • yes it happens because every time you refresh the page the code executes every time.. better write PHP validations to avoid it.. or redirect the page to any other after the data is successfully inserted... – annam priyatam Dec 23 '14 at 10:15
  • 1
    Also, you should not be using mysql_query (or any other mysql_ functions). These were depreciated in PHP 5.5 and will be removed entirely in a future version. You should consider using mysqli_ or PDO. – Amo Dec 23 '14 at 10:16

1 Answers1

0

Try isset() in if statement instead of $_POST['submit'] == 'Save'

Kamlakar
  • 36
  • 5