Databases| SQL| MySQL| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
 
 


 

PHP SQL Insert

                         

PHP SQL Insert is used to insert the record from HTML page to Mysql  database using PHP.

Understand with Example

The Tutorial illustrate an example from PHP SQL Insert . To understand the example, firstly we need to create a table 'stu' with required fieldnames and datatypes respectively. The table 'stu' has a Primary Key 'id'.

 

 

 

Create Table Stu:

CREATE TABLE `stu` (                     
          `id` int(11) NOT NULL auto_increment,  
          `name` varbinary(10) default NULL,     
          `class` int(11) default '12',          
          PRIMARY KEY  (`id`)                    
        )

Insert.php:

We create an HTML form that is used to add the new records to table 'stu' in Mysql database. When a user click on a submit button of html page,the form records is sent to the "insert.php". The insert.php is connected to the database and submit the records from HTML page with PHP post variable. The Mysql insert query in PHP execute the query and add the records to the table 'Stu'.

<html>
<body ">
<form method="post" action="insert.php"
	style="border: 1px solid #000000;
	width :230px; margin-top: 
	50px;margin-left: 70px;
	padding:20px 20px 20px 20px; 
	background-color: #F5F5FF;">
	<table cellpadding="5">
	<tr >
		<td>Name</td>
		<td>&nbsp;</td>
		<td><input type="text" name="name"></td>
	</tr>
	<tr>
		<td>Class</td>
		<td>&nbsp;</td>
		<td><input type="text" name="class"></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td><input type="submit" name="submit" value="Submit"></td>
	</tr>
	</table>

</form>
<div style="border: 1px solid #000000; 
	width :230px; margin-top: 
	50px;margin-left: 70px;
	padding:20px 20px 20px 20px ; 
	background-color: #F5F5FF;">
<?php
	$host = "localhost";
	$user = "root";
	$password = "root";
	$database = "komal";
	$connection = mysql_connect($host,$user,$password) 
		or die("Could not connect: ".mysql_error());
	$connection1 = mysql_connect($host,$user,$password) 
		or die("Could not connect: ".mysql_error());
	mysql_select_db($database,$connection) 
		or die("Error in selecting the database:".mysql_error());
	if (isset($_POST['name'])) {
		$name=$_POST["name"];
		$class=$_POST["class"];
		$sql="insert into stu(name,class) 
			values('".$name."',".$class.")";
		mysql_query($sql,$connection) 
			or exit("Sql Error".mysql_error());
		mysql_close($connection);
	}
	$sql="Select * from stu";
	$sql_result=mysql_query($sql,$connection1) 
		or exit("Sql Error".mysql_error());
	$sql_num=mysql_num_rows($sql_result);
	echo "<table width=\"100%\">";
	echo "<tr>";
	echo "<td ><b>Id</b></td><td><b>Name</b></td> 
		<td><b>Class</b></td>";
	echo "</tr>";
	while($sql_row=mysql_fetch_array($sql_result))
	{
		$id=$sql_row["id"];
		$name=$sql_row["name"];
		$class=$sql_row["class"];
		echo "<tr><td>".$id."</td>";
		echo "<td>".$name."</td>";
		echo "<td>".$class."</td></tr>";
	}	
	echo "</table>";
	mysql_close($connection1);
?>
</div>
</body>
</html>

Ourtput:

Name  
Class  
   
IdName Class
1Ajay12
2Bhanu12
3Komal12
4Rakesh12
5Santosh12
6Tanuj12
7kk12
8ss12
9ss12
10komal11
11girish12

                         

» View all related tutorials
Related Tags: php sql mysql c table data ui insert datatypes types type tutorial field name using ria create tab ie example

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.