Introduction
हेलो दोस्तों आज हम PHP का इस्तेमाल करके Html form data को MySQL database में insert यानि save कैसे करते है ये जानेगे. User जब बी form में अपना data fillup करने के बाद form submits button पे click करता है तब क्या होता है की submitted data server-side में send किया जाता है और वह पे php या किसी दूसरे server side language के जरिये collect किया जाता है और SQL का इस्तेमाल से यह data को database के specific table में save करवा दिया जाता है. तो बस दोस्तों आज इस post में हम यही जानेगे की form data को database में कैसे store यानि save कराये.
Data को database के table में insert करने के लिए जरुरी steps
Data को database में insert करने के लिए हमें एक HTML form , mysql database के साथ connection करना जरुरी होता है. form data को server side collect करके database में insert करने के लिए SQL query का इस्तेमाल किया जाता है!.
- WAMP (web server)
- HTML form
- php
- MySQL
WAMP (web server)
WAMP एक software(application) है जो हमें web site development के लिए एक local server environment provides करता है जिसे की हम अपने local machine(laptop, computer) को एक server की तरह इस्तेमाल कर सके.
Creating the Html Form
HTML form का इस्तेमाल करके हम user की information collect कर सकते है. user की information collects करने के लिए HTML form के element जैसे की input box, radio buttons, checkbox, textarea, submit button का इस्तेमाल किया जाता है!. और user की information जैसे की user का first name, Lastname, email, mobile, gender, address, profile photos जैसे information form के जरिये हम collect करवा के database में insert करा सकते है!.
For example employee, के data को collect करने के लिए Employee form create किया गया है!. Employee information जैसे की employee FirstName, LastName, Email, Mobile, gender है!.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Employee Form</title>
</head>
<body>
<form action="datainsert.php" method="post">
<div>
<label for="firstName">First Name:</label>
<input type="text" name="f_name" id="firstName">
</div>
<div>
<label for="lastName">Last Name:</label>
<input type="text" name="l_name" id="lastName">
</div>
<div>
<label for="emailAddress">Email Address:</label>
<input type="text" name="email" id="emailAddress">
</div>
<div>
<label for="Mobile">Mobile:</label>
<input type="number" name="mobile" id="mobile">
</div>
<div>
<label for="Gender">Gender:</label>
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
output:
Employee Form
Mysql database connection before save data into databse
HTML form के realated data को database में store करने से पहले php script का इस्तेमाल से mysql database को connect करना होता है. MySQL database connects होगा तभी तो हम data को database में store करा पायेँगे इस लिए mysql database का connection जरुरी होता है!.
Example:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Employee";
// Create connection
$connection = new mysqli($servername,$username, $password, $dbname);
// Check connection
if ($connection->connect_error) {
die("Connection failed: ". $conn->connect_error);
}
$query = "INSERT INTO registration VALUES ('gamit', 'imu', 'imu@gmail.com', '9918477221', 'male')";
if ($connection->query( $query ) === TRUE) {
echo "Record inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Form data inserting into the database table
ऊपर के HTML form में देख सकते है जिसमे method post सेट किया है और action attribute में location datainsert.php है!. तो जैसे ही user form data submit करेंगे तो वह post methos से server-side datainsert.php file में send होंगे और data को $_POST, $_GET, $_REQUEST super global variable के जरिये data को connect किया जाता है!.
datainsert.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Employee";
// Create connection
$connection = new mysqli($servername,$username, $password, $dbname);
// Check connection
if ($connection->connect_error) {
die("Connection failed: ". $conn->connect_error);
}
//collect HTML form data
$firstname = $_POST['f_name'];
$lastname = $_POST['l_name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$gender = $_POST['gender'];
$sql = "INSERT INTO registration VALUES ('$firstname','$lastname','$email','$mobile','$gender')";
$result = mysqli_query( $connection,$sql );
if( $result ){
echo "Data stored in database successfully!";
}else{
echo "Data NOT stored in database successfully!";
}
?>






