Insert Data in Database
×
Home Courses Community Support
Insert Data in Database

To insert data into any table of database we use simple query,

$sql = "INSERT INTO TableName (ColumnName1, ColumnName2, ColumnName3, ColumnName4)
VALUES ('Value1', 'Value', 'Value3', 'Value4')";


For your easy understanding we have created a simple webform with no validation. When you will submit the form it will pass the values to new page and will show the message along with storing the data to database. You have learned to create a web form in Designing Enquiry Form. Just add form action like below-

<form method="post" action="response.php">


Now create response.php using below code-

<!docty html>
<html>
<body>
<h2>Thanks for contacting us. We will contact you sortly.</h2>
<h3>Your contact details are as below-</h3>
<b>Name:</b> <?php echo "& ".$_POST["userName"];?><br>
<b>Phone:</b> <?php echo "& ".$_POST["userPhone"];?><br>
<b>Email:</b> <?php echo "&nbsp;".$_POST["userEmail"];?><br>
<b>Message:</b> <?php echo "&nbsp;".$_POST["userMsg"];?><br>
<?php
$con = mysqli_connect("localhost","root","","myweb");
if (mysqli_connect_errno()){
echo"Failed to connect to MySQL: ".mysqli_connect_error();
die;}
$sql = "INSERT INTO form_data (Name, Phone, Email, Message)
VALUES ('".$_POST["userName"]."', '".$_POST["userPhone"]."', '".$_POST["userEmail"]."', '".$_POST["userMsg"]."')";
if ($con->query($sql) === TRUE) {
    echo "<br><b>Note:</b> We have saved the details to contact you. Please re-submit the form if the contact details changed.";}
	else {
    echo "Error: " . $sql . "<br>" . $con->error;}
mysqli_close($con);
?>
</body>
</html>

 

You will get the values on response.php as shown in below image.
 


 

Your updated database table will be like below image-

 

enlightenedWhile working on SQL queries we need to take care of following-

  • The SQL query must be quoted in PHP.
  • String values inside the SQL query must be quoted.
  • Numeric values and the word NULL must not be quoted.

 

← Preview

Connection to Database

Next →

Update Record in the Database

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App