PHP

Get simple form data and store in Data Base using PHP

HTML :  ( sample.html )

<html>

<head>

<title>Sample form </title>

</head>

<body>

<form action=”action.php” method=”post”>

First name:

<br />

<input type=”text” name=”firstname”  required  />

<br />

Last name:

<br />

<input type=”text” name=”lastname” required   />

<br />

Gender:

<input type=”radio” name=”gender” value=”male” checked  required  /> Male

<input type=”radio” name=”gender” value=”female”  required  /> Female

<input type=”radio” name=”gender” value=”other”  required  /> Other

<br />

<input type=”submit” value=”Submit”>

</form>

</body>

</html>

 

PHP :   ( action.php )

 

<?php

 

If ( $_SERVER [ ‘REQUEST_METHOD’ ] == ‘POST’ ) {

$host=”localhost”;

$user=”root”;

$pass=””;

$db=”form_data”;

$firstname=$_POST[‘firstname’];

$lastname=$_POST[‘lastname’];

$gender=$_POST[‘gender’];

 

$con = mysqli_connect ( $host , $user , $pass , $db );

$sql=”insert into user_data (first,last,gender) values ( ‘ $firstname ’ , ’ $lastname ’,’ $gender ’)”;

$result = mysqli_query( $con , $sql );

If(!$result) die (“Data insert failed”);

}

 

?>

Tagged ,

Leave a Reply