Monday, October 3, 2016

How to create simple PHP form and save to database?

Things to know:
  • Tools Required to implement this code WAMP for Windows.
  • Save this code to .php file.
  • Place that file in WWW directory. WWW directory will in c:/wamp by default.
  • Create Database in mySQL name it as "test". after that create new table name as "temp" with fields "Fname" and "Lname".

 <!doctype html>
<meta charset="utf-8">

<br />

echo 'Error: Insert first name';
else if($_POST['lname']==""){
echo 'Error: Insert last name';
$conn = mysqli_connect('localhost','root','','test');
$str = "insert into temp (fname,lname) values('".$_POST['fname']."','".$_POST['lname']."')";
echo 'Inserted successfully';

echo '<br /><br />';


<form action="" method="POST">
//textbox for input value 1
First Name: <input type="text" name="fname" />
<br />
<br />
//textbox for input value 2
Last Name: <input type="text" name="lname" />
<br />
<br />
//submit button to save values in database 
<input type="submit" name="btn" value="Insert" />



Post a Comment