Here is a simple program code to enter the form data to the database using php.
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Form To Db</title>
<style type="text/css">
.container{width: 80%;text-align: left;margin: 80px;padding: 60px;background-color: #999;}
input{border: none;border-radius: 4px;padding: 10px}
</style>
</head>
<body>
<div class="container">
<form method="post" action="formtodb.php">
<table align="center">
<caption>Registration Form</caption>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="40"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" size="40"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Here is simple registration form with two fields name and email that are to be entered by the user. By post method the values entered by the user will not be visible to everyone. When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "formtodb.php". The form data is sent with the HTTP POST method.
PHP Code:
<?php
include 'connectToDb.php'; // database connection file is included.
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$name = $_REQUEST['name']; // fetching the form data in the variable
$email= $_REQUEST['email'];
}
$sql="INSERT INTO registrationform(Name,Email)
VALUES('$name','$email')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
PHP $_REQUEST is used to collect data after submitting an HTML form. Insert the values in the table "registrationform" using the sql query.
0 Comment(s)