Php Insert Form Data Into Mysql Database Using MySQLI Code.
________________________________________________________
In this Php Tutorial we will Learn How To Insert Data Into MySQL Database Table From Form Inputs In Php using MySQLI .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .
<?php
// php code to Insert data into mysql database from input text
if(isset($_POST['insert']))
{
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "test_db";
// get values form input text and number
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
// connect to mysql database using mysqli
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
// mysql query to insert data
$query = "INSERT INTO `users`(`fname`, `lname`, `age`) VALUES ('$fname','$lname','$age')";
$result = mysqli_query($connect,$query);
// check if mysql query successful
if($result)
{
echo 'Data Inserted';
}
else{
echo 'Data Not Inserted';
}
mysqli_free_result($result);
mysqli_close($connect);
}
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP INSERT DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="php_insert_data_in_mysql_database.php" method="post">
<input type="text" name="fname" required placeholder="First Name"><br><br>
<input type="text" name="lname" required placeholder="Last Name"><br><br>
<input type="number" name="age" required placeholder="Age" min="10" max="100"><br><br>
<input type="submit" name="insert" value="Add Data To Database">
</form>
</body>
</html>
In this Php Tutorial we will Learn How To Insert Data Into MySQL Database Table From Form Inputs In Php using MySQLI .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .
Php Source Code:
<?php
// php code to Insert data into mysql database from input text
if(isset($_POST['insert']))
{
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "test_db";
// get values form input text and number
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
// connect to mysql database using mysqli
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
// mysql query to insert data
$query = "INSERT INTO `users`(`fname`, `lname`, `age`) VALUES ('$fname','$lname','$age')";
$result = mysqli_query($connect,$query);
// check if mysql query successful
if($result)
{
echo 'Data Inserted';
}
else{
echo 'Data Not Inserted';
}
mysqli_free_result($result);
mysqli_close($connect);
}
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP INSERT DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="php_insert_data_in_mysql_database.php" method="post">
<input type="text" name="fname" required placeholder="First Name"><br><br>
<input type="text" name="lname" required placeholder="Last Name"><br><br>
<input type="number" name="age" required placeholder="Age" min="10" max="100"><br><br>
<input type="submit" name="insert" value="Add Data To Database">
</form>
</body>
</html>
///////////////OUTPUT:
Bagikan
PHP MySQLI Code : How To Insert Data Into MySQL Database From Form Inputs Text Using Php
4/
5
Oleh
insurance