forum by: user: nick, Post on: 2021-08-18 18:34:58, Posts: 129
php code to insert data into mysql database from html form
php code to insert data into mysql database from html form
1. Create a Database Connection File
<?php $servername='localhost'; $username='root'; $password=''; $dbname = "my_db"; $conn=mysqli_connect($servername,$username,$password,"$dbname"); if(!$conn){ die('Could not Connect MySql Server:' .mysql_error()); } ?>
2. Create Html form
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Create Record</title> <link rel="stylesheet" href="href="https://maxcdn.bootstrapcdn.com"> <style type="text/css"> .wrapper{ width: 500px; margin: 0 auto; } </style> </head> <body> <div class="wrapper"> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <div class="page-header"> <h2>Contact Form</h2> </div> <p>Please fill this form and submit to add employee record to the database.</p> <form action="insert.php" method="post"> <div class="form-group"> <label>Name</label> <input type="text" name="name" class="form-control"> </div> <div class="form-group"> <label>Email</label> <input type="email" name="email" class="form-control"> </div> <div class="form-group"> <label>Mobile</label> <input type="mobile" name="mobile" class="form-control"> </div> <input type="submit" class="btn btn-primary" name="submit" value="Submit"> </form> </div> </div> </div> </div> </body> </html>
3. Create Insert.php file
<?php include_once 'db.php'; if(isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; $mobile = $_POST['mobile']; $sql = "INSERT INTO users (name,email,mobile) VALUES ('$name','$email','$mobile')"; if (mysqli_query($conn, $sql)) { echo "New record has been added successfully!"; } else { echo "Error: ".$sql.":-".mysqli_error($conn); } mysqli_close($conn); } ?>