This is a very basic php-mysql tutorial and I will be trying to make it as simple as possible, since I it to be helpful for the first timers. This is the part of program when I was starting to learn Php-mysql. I was just trying to create a html form, get some data filled and then save the data in the database. And also displaying those data back. It was a messy code but I felt glad enough since it was a tangible piece of my learning process. Since then, if anyone asked me some help regarding the startup, i used to provide this code for them to play with. And I thought it will be a good idea if I can share it.
a simple html form
<form name="form1" method="post" action="target.php">
<p class="style1" align="left">
<label>Enter your details:</label>
</p>
<table width="421" bgcolor="#cccccc">
<tbody><tr>
<td width="195">Name:
<input name="cust_name" type="text"></td>
<td width="210">Address:
<input name="cust_address" type="text"></td>
</tr>
<tr>
<td>Select your Gender </td>
<td>
Male
<input name="gender" value="MALE" type="radio">
Female
<input name="gender" value="FEMALE" type="radio"></td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="Submit" value="Submit" type="submit">
</div></td>
</tr>
</tbody></table>
<p> </p>
<p> </p>
</form>
code for target.php file
// ESTABLISH CONNECTION TO THE MYSQL DATABASE
$connection=mysql_connect("localhost","root","") or die("Error in connecting to the database");
$database="store";
$result=mysql_select_db($database) or die("Error: Database cannot be selected");
//NOW GET THE VALUES FROM THE FORM
$name=$_POST['cust_name'];
//cust_name is the name given to the text field in the html form
$address=$_POST['cust_address'];
$gender=$_POST['gender'];
//Now enter the values in the database table "CUSTOMER_TAB"
$query="insert into customer_tab values('".$name."','".$address."','".$gender."')";
$result=mysql_query($query);
if(!$result){ die("query cannot be executed");}
echo"
<h2>Your information has been received and stored in the database</h2>";
//Now access information from database
$query="select * from customer_tab where name='".$name."' ";
$result=mysql_query($query);
if(!$result){ die("query cannot be executed");}
while($row=mysql_fetch_row($result))
{
echo"<b>Name:</b> $row[0] ";
echo"<b>Address:</b> $row[1] ";
echo"<b>Gender:</b> $row[2] ";
}
//now get out of the database
mysql_close($connection);
To run this, you should follow two steps given below:
A. Creating database
1. The database name is "store"
2. The table name is "customer_tab"
3. The table "customer_tab" contains three fields. they are "name","address", "gender"
First of all, create the table in the database and then proceed to step(B)
B. How to run the code:
1. Get these files in a folder named "sample", copy this folder"sample" in the web directory (i.e. www directory)
2. Start a web browser( eg mozilla firefox)
3. Type "http://localhost/sample/myform.html".
4. The form will appear, fill the form. This form submits its data to the "target.php".
a simple html form
<form name="form1" method="post" action="target.php">
<p class="style1" align="left">
<label>Enter your details:</label>
</p>
<table width="421" bgcolor="#cccccc">
<tbody><tr>
<td width="195">Name:
<input name="cust_name" type="text"></td>
<td width="210">Address:
<input name="cust_address" type="text"></td>
</tr>
<tr>
<td>Select your Gender </td>
<td>
Male
<input name="gender" value="MALE" type="radio">
Female
<input name="gender" value="FEMALE" type="radio"></td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="Submit" value="Submit" type="submit">
</div></td>
</tr>
</tbody></table>
<p> </p>
<p> </p>
</form>
code for target.php file
// ESTABLISH CONNECTION TO THE MYSQL DATABASE
$connection=mysql_connect("localhost","root","") or die("Error in connecting to the database");
$database="store";
$result=mysql_select_db($database) or die("Error: Database cannot be selected");
//NOW GET THE VALUES FROM THE FORM
$name=$_POST['cust_name'];
//cust_name is the name given to the text field in the html form
$address=$_POST['cust_address'];
$gender=$_POST['gender'];
//Now enter the values in the database table "CUSTOMER_TAB"
$query="insert into customer_tab values('".$name."','".$address."','".$gender."')";
$result=mysql_query($query);
if(!$result){ die("query cannot be executed");}
echo"
<h2>Your information has been received and stored in the database</h2>";
//Now access information from database
$query="select * from customer_tab where name='".$name."' ";
$result=mysql_query($query);
if(!$result){ die("query cannot be executed");}
while($row=mysql_fetch_row($result))
{
echo"<b>Name:</b> $row[0] ";
echo"<b>Address:</b> $row[1] ";
echo"<b>Gender:</b> $row[2] ";
}
//now get out of the database
mysql_close($connection);
To run this, you should follow two steps given below:
A. Creating database
1. The database name is "store"
2. The table name is "customer_tab"
3. The table "customer_tab" contains three fields. they are "name","address", "gender"
First of all, create the table in the database and then proceed to step(B)
B. How to run the code:
1. Get these files in a folder named "sample", copy this folder"sample" in the web directory (i.e. www directory)
2. Start a web browser( eg mozilla firefox)
3. Type "http://localhost/sample/myform.html".
4. The form will appear, fill the form. This form submits its data to the "target.php".
Comments