Question


Himanshu

How to insert some data in to the MySQL database using PHP

Date = 2015-03-23

<html>

<body>
<form action="insert.php" method="post" >
First Name: <input type="text" name="fname" /><br><br>
Last Name: <input type="text" name="lname" /><br><br>
<input type="submit" name="submit" Value="Submit" />
</form>
</body>
</html>

Comments by experts


Rajesh Kakadiya
2015-03-23

// check this solution for you

<html>
<body>
<?php
$con
=mysql_connect("localhost","username","password"); // connection to localhost
if(!$con)
  {
  die(
'Could not connect :'.mysql_error()); // showing error if having problem with username or password
  
}
mysql_select_db("database_name",$con); // select database from localhost
 
$sql="INSERT INTO table_name(fname, lname)
VALUES('
$_POST[fname]','$_POST[lname]')";  // inserting record into table
 
if(!mysql_query($sql,$con)) 
  {
  die(
'Error in inserting record :'.mysql_error());  // if problem with tablename or any field name
  
}
echo 
"1 record added"// record added successfully
mysql_close($con// close connection to server
?>
</body>
</html>


Type your Comment below and click to send comment