Saturday, September 24, 2011

It's a simple insert code using php, mysql and wamp

1. First you need to create user directory inside the C:\wamp64\www.

2. Second you need to create connect.php file inside the C:\wamp64\www\user directory for your connection to your database and paste this code below:

<?php
$dbServer="localhost";
$dbUser="root";
$dbUserPwd="";
$dbName="humanresource";

$dbLink =mysql_connect($dbServer,$dbUser,$dbUserPwd) or die(mysql_error());
mysql_select_db($dbName,$dbLink);
?>


3. Third you need to create index.php file inside the C:\wamp64\www\user directory for your form and insert code. Paste this code below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert record</title>
</head>
<body>
<form name="form1" method="post" action="index.php">
<table width="296" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>Name:</td>
<td><input name="name" type="text" id="name" size="20" maxlength="10"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Address</td>
<td><input name="address" type="text" id="address" size="20" maxlength="10"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><input name="submit" type="submit" id="submit" value="Save"></td>
<td>
<?php
include("connect.php");
if(isset($_POST['save']))
{
$name=$_POST['name'];
$address=$_POST['address'];
if($name=="" || $address=="")
{
echo'<font style="font-family:Tahoma;font-size:11px;color:#990000">&nbsp;&nbsp;Please input all fields</font>';
}
else
{     
$result = "INSERT INTO `tbl_test` (`name`, `address`) VALUES ('$name', '$address')" ;
$rs2 = mysql_query($result) or die("Invalid Query <br>$result");
echo'<font style="font-family:Tahoma;font-size:11px;color:#990000">&nbsp;&nbsp;Successfully inserted</font>';
}
}
?>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

4. Now you need to access http://localhost/user/ or it's depen on your wamp setting to your browse.




5. Now enter your name and address then clicksave button. Now open your Mysql Database to check if your data was saved.