Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the equivalent of ispostback(.NET) in PHP?

I just start coding in PHP, i wrote my first php + mysql program for inserting data through web form. it works fine, but whenever i refresh the page, it automatically saves null record to my database. i know the solution in .NET is ispostback, but not in php? can somebody give me some idea in PHP.

Code is here:

<body>
<form action="mySQLTest.php" method="post">
First Name :<input type="text" name ="txtFirstName"/> <br/>
Last Name: <input type="text" name ="txtLastName"/>   <br/>
Age : <input type="text" name= "txtAge" /> <br/>
<input type="submit"/>
</form>
<?php
   $con = mysql_connect("localhost","root","");
   if(!$con)
   {
    die('Could not Connect:' .mysql_error());
   }

   mysql_select_db("test", $con);

   if($_REQUEST[])
   {

   $sql1 = "Insert into info(FirstName, LastName, Age) Values('$_POST[txtFirstName]','$_POST[txtLastName]','$_POST[txtAge]')";
   }
   if(!mysql_query($sql1, $con))
   {
    die('Error: '.mysql_error());
   }
   else                                
   {
   echo "1 Record Added";
   }

   mysql_close($con)
?>
</body>
like image 660
subeer haldar Avatar asked Nov 25 '25 23:11

subeer haldar


1 Answers

Have you tried if ($_SERVER['REQUEST_METHOD']=='POST')

updated answer:

if ($_SERVER['REQUEST_METHOD']=='POST') {
[insert...]
header('Location: added_record.php');
}
like image 91
David Aleu Avatar answered Nov 27 '25 13:11

David Aleu