Inserting records in database with input box
Not only we can insert the data by directly in with the php INSERT command but also we can insert the data using a form. That is, we can read data from the user in the form of an input box inwhich the user type the data to be inserted in the input box and when they click on the submit button, the data are passed to the database. The following program will show how to insert data to the database with a form.
First we need to create a php file named insform.php
Then we need to create another file named save.php
Output
First we need to create a php file named insform.php
<div>
<form action=save.php method=post>
Num:<input type=text name=numm>
<br>
Name:<input type=text name=namee><br><br>
<INPUT type=submit name=submit>
</form>
</div>
Then we need to create another file named save.php
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="mydb";
//connecting...
$conn=new mysqli($servername,$username,$password,$dbname);
//checking...
if($conn->connect_error)
{
die("connection failed:".$conn->connect_error);
}
//inserting into ting table
if(isset($_POST["submit"]))
{
$num=$_POST['numm'];
$name=$_POST['namee'];
$sql="INSERT INTO firsttable (num,user) VALUES('$num','$name')";
if(mysqli_query($conn,$sql))
{
echo "added sucessfully";
}
else{
echo "error:".$sql."".msqli_error($conn);
}
mysqli_close($conn);
}
?>
Output
⇐Prev
Next⇒
Comments
Post a Comment