Table of Contents
Introduction
हेलो दोस्तों आज हम इस पोस्ट में एक form data को database के अंदर कैसे store करा सकते PHP script के मदद छे. तो दोस्तों form कैसे work करता है इसकी में आपको sort introduction करा देता हु मन लीजिये अगर आप किसी बी website में online form fill-up करते है तो उसमे क्या होता है की आप अपना firstname, lastname, email, photo etc. वगेरे data को fill करते है तो जब aap उस data fill करके submit करते है तो उस form से data कहा पे send होते होंगे और कहा store या save होते होंगे!. तो जब आप data को submit करते है किसी बी website के form से तो वह उस website के database में store होते है!. तो data को database के अंदर कैसे store करना है वह हम इस post में जानेगे step by step.
step – 1
=>HTML form
step – 2
=> create database and table & database configuration
step – 3
=> Get form data using php script
step – 4
=> sql Insert statement
step – 5
=> full source code
HTML form
HTML form के मदद छे हम user से उसकी persional information fill करा सकते है!.
Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action='action.php' method='post'>
<div class="row">
<h2>User Registration</h2>
<div class="form-group">
<label for="">first name:</label>
<input type="text" class="form-control" name='fname' id='txt_fname'>
</div>
<div class="form-group">
<label for="">last name:</label>
<input type="text" class="form-control" name='lname' id='txt_lname'>
</div>
<div class="form-group">
<label>Email id:</label>
<input type='text' name='email' id='email' class="form-control">
</div>
<div class="form-group">
<label>mobile no:</label>
<input type='text' name='mobile' id='mobile' class="form-control">
</div>
<div class="form-group">
<label>Gender:</label><br>
<input type='radio' name="gender" id='gender' value='male' class=""> Male<br>
<input type='radio' name="gender" id='gender' value='female' class=""> Female
</div>
<div class="form-group">
<label>Hobby:</label><br>
<input type='checkbox' name="chk_hobby" id='chk_hobby' value='read a book'> Read a book<br>
<input type='checkbox' name="chk_hobby" id='chk_hobby' value='Playing cricket'> Playing cricket
</div>
<div class="form-group">
<input type='submit' name='submit' value='Save' class="btn btn-primary">
<input type='reset' name='btn_reset' value='Reset' class="btn btn-primary">
</div>
</form>
</div>
</body>
</html>
ऊपर के form में देख सकते है की हम user से उसकी information जैसे की user का firstname, lastname, email, mobile, gender उसकी hoby वगेरे data को fill-up करवाएंगे. जब user अपनी information fill करके submit button पे click करेगा तो form tag में action attribute में जो direction set की हुवी है वह पे user के इस data को send करदेगा. और इस form tag के action attribute में action.php set की हुवी है!.
Create database and table & database connection
Create database
जब बी user अपना data form से submit करेगा तो उसे कही पे store करना होता है तो उसके लिए हमें database की जरुरत होती है!. निचे के image में देख सकते है की हमने user_registration name का database create किया है! database create करने के लिए

Create table
user का data database के particular table के अंदर save होगा तो user data के लिए हमने registration name का table create किया है और इस table में column fname, lname, email, mobile, gender, hoby name के column create किये है जिसकी user का data एक formate में store करा जा सके!.

table ke colum जैसे की आप निचे image मे देख सकते है!

database connection
अगर user डाटा को database tab ले जाना है तो उसके लिए हमें database के साथ connection स्थापित करना होता है और वह php script से कर सकते है!. database connection के लिए अलग से एक php file create कर लेनेगे config.php
config.php
<?php
// Create connection
$conn = mysqli_connect ( "localhost", "root", "", "user_registration" );
//mysqli_connect() function parameters are mysqli_connect('hostname','username','password','databasename');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
?>
SQL Insert statement
$sql = " INSERT INTO
table_name(
column1,
column2,
column3,
column4,
.
.
)
VALUES (
value1,
value2,
value3,
value4,
.
.
)";
Data ko database के table में insert करने के लिए SQL INSERT statement का इस्तेमाल किया जाता है!
Get form data using php script
User submits button पे click करेगा तो वह form tag के action attribute में set direction पे to send होगा और हमने action.php action attribute में set की है तो user का data to submit के बाद action.php file में send होंगे!. और आप action.php में देख सकते है हमने पहले confing.php include की है database ki configuration के लिए!
action.php
<?php
include('confing.php');
//for the user data getting using $_POST method
if (isset( $_POST['submit'] ) ) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$gender = $_POST['gender'];
$hobby = $_POST['chk_hobby'];
$created_at = date('Y-m-d h:i:s');
$updated_at = date('Y-m-d h:i:s');
$sql = " INSERT INTO
registration (
fname,
lname,
email,
mobile,
gender,
chk_hobby,
created_at,
updated_at
)
VALUES (
'".$fname."',
'".$lname."',
'".$email."',
'".$mobile."',
'".$gender."',
'".$hobby."',
'".$created_at."',
'".$updated_at."'
)";
//echo $sql;
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
Full code
अगर हमें HTML form और php script के लिए अलग से file create ना करके एक ही file में सारा code लिखना हो तो form tag के action attribute में किसी प्रकार का कोय बी action set nahi करेंगे!
<?php
include('confing.php');
//for the user data getting using $_POST method
if (isset( $_POST['submit'] ) ) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$gender = $_POST['gender'];
$hobby = $_POST['chk_hobby'];
$created_at = date('Y-m-d h:i:s');
$updated_at = date('Y-m-d h:i:s');
$sql = " INSERT INTO
registration (
fname,
lname,
email,
mobile,
gender,
chk_hobby,
created_at,
updated_at
)
VALUES (
'".$fname."',
'".$lname."',
'".$email."',
'".$mobile."',
'".$gender."',
'".$hobby."',
'".$created_at."',
'".$updated_at."'
)";
//echo $sql;
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action='#' method='post'>
<div class="row">
<h2>User Registration</h2>
<div class="form-group">
<label for="">first name:</label>
<input type="text" class="form-control" name='fname' id='txt_fname'>
</div>
<div class="form-group">
<label for="">last name:</label>
<input type="text" class="form-control" name='lname' id='txt_lname'>
</div>
<div class="form-group">
<label>Email id:</label>
<input type='text' name='email' id='email' class="form-control">
</div>
<div class="form-group">
<label>mobile no:</label>
<input type='text' name='mobile' id='mobile' class="form-control">
</div>
<div class="form-group">
<label>Gender:</label><br>
<input type='radio' name="gender" id='gender' value='male' class=""> Male<br>
<input type='radio' name="gender" id='gender' value='female' class=""> Female
</div>
<div class="form-group">
<label>Hobby:</label><br>
<input type='checkbox' name="chk_hobby" id='chk_hobby' value='read a book'> Read a book<br>
<input type='checkbox' name="chk_hobby" id='chk_hobby' value='Playing cricket'> Playing cricket
</div>
<div class="form-group">
<input type='submit' name='submit' value='Save' class="btn btn-primary">
<input type='reset' name='btn_reset' value='Reset' class="btn btn-primary">
</div>
</form>
</div>
</body>
</html>