HTML/Javascript

SES by business degree online promotion team.

Philippine Airlines International Flight schedules

Philippine Airlines International Flight schedules
Philippine Airlines International Flight schedules

Search This Blog

Thursday, September 9, 2010

Sample php code to save data to mysql database from html form

Save data from html form to MySql Database.
Again, here is a sample html form for data entry and php script.
Note: You can combine javascript validation and php code on saving data.

code to paste:

form_entry.html

<html>
<body>
<table border="1" align="center">

<form action="save.php" method="post">
<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" /></td>
</tr>

<tr>
<td>Firsntname:</td>
<td><input type="text" name="firstname" /></td>
</tr>

<tr>
<td>Age:</td>
<td><input type="text" name="age" /></td>
</tr>

<tr>
<td><input type="submit" /></td>
<td>&nbsp;</td>
</tr>

</form>
</table>

</body>
</html>


save.php
<html>
<body>
<?php
$con = mysql_connect("localhost","root","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sample", $con);

$sql="INSERT INTO table1 (lastname, firstname, age)
VALUES
('$_POST[lastname]','$_POST[firstname]','$_POST[age]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>
</body>
</html>

No comments:

Post a Comment