I've created a HTML page to enter data to a MySQL database. I've embedded PHP code within the same HTML page. Here is the code,
<html>
<head>
</head>
<body>
<?php
/* Database connection operations */
require_once("db_handler.php");
$conn = iniCon();
$db = selectDB($conn);
?>
<b id="loc_caption_1">Enter a new location</b>
<div id="loca">
<form name="locForm" action="<?php echo $PHP_SELF; ?>" method="post" >
<table width="300" border="0">
<tr>
<td>Location ID</td>
<td><input type="text" name="lid" readonly="readonly" value="<?php echo $new_id; ?>" style="text-align:right" /></td>
</tr>
<tr>
<td>Code</td>
<td><input type="text" name="code" style="text-align:right" /></td>
</tr>
<tr>
<td>Location</td>
<td><input type="text" name="loc" style="text-align:right" /></td>
</tr>
</table>
</div>
<br/>
<div id="buttons">
<input type="reset" value="Clear" /> <input type="submit" value="Save" />
</div>
</form>
<?php
/* Saving new record */
$id = $_POST["lid"];
$code = $_POST["code"];
$location = $_POST["loc"];
$query = "INSERT INTO locations(LID, Code, Location) VALUES('$id', '$code', '$location')";
if (!mysql_query($query, $conn))
{
die("Error " . mysql_error());
}
else
{
echo "<br/><br/>";
echo "1 record added successfully!";
echo "<meta http-equiv=\"refresh\" content=\"3;URL=locations.php\">";
}
mysql_close($conn);
?>
</body>
</html>
The problem I have is, the PHP code block which is written to insert the data to the database gets executed once as soon as the page loads. How can I stop this from happening? I want it to wait until I enter the data to the form and when I press the submit button, then gets executed.
Thank you.