article

Saturday, October 19, 2019

PHP MySQLi Insert Data Into Database

PHP MySQLi Insert Data Into Database
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PHP MySQLi Insert Data Into Database</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testingdb";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST["submit"])){
 $sql = "INSERT INTO students (student_name, student_email, student_city)
 VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";

 if ($conn->query($sql) === TRUE) {
 echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
 } else {
 echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
 }
 $conn->close();
}
?>
<div id="main">
<center>
<h1>Insert data into database using mysqli</h1>
<div id="registration">
<h2>Student's Form</h2>
<hr/>
<form action="" method="post">
<label>Student Name :</label>
<input type="text" name="stu_name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
<label>Student Email :</label>
<input type="email" name="stu_email" id="email" required="required" placeholder="john123@gmail.com"/><br/><br />
<label>Student City :</label>
<input type="text" name="stu_city" id="city" required="required" placeholder="Please Enter Your City"/><br/><br />
<input type="submit" value=" Submit " name="submit"/><br />
</form>
</div>
</center>
</div>
<style>
@import url(http://fonts.googleapis.com/css?family=Raleway);
#main{
 width:100%;
 font-family: 'Raleway', sans-serif;
}
h2{
 background-color: #C4F980;
 text-align:center;
 border-radius: 10px 10px 0 0;
 margin: -10px -40px;
 padding: 15px;
}
hr{
 border:0;
 border-bottom:1px solid #ccc;
 margin: 10px -40px;
 margin-bottom: 30px;
}
#registration{
 width:300px;text-align:left;
 border-radius: 10px;
 font-family:raleway;
 border: 2px solid #ccc;
 padding: 10px 40px 25px;
 margin-top: 70px;
}
input[type=text],input[type=email]{
 width:99.5%;
 padding: 10px;
 margin-top: 8px;
 border: 1px solid #ccc;
 padding-left: 5px;
 font-size: 16px;
 font-family:raleway;
}
input[type=submit]{
 width: 100%;
 background-color:#7ECF16;
 color: white;
 border: 2px solid #6BB30F;
 padding: 10px;
 font-size:20px;
 cursor:pointer;
 border-radius: 5px;
 margin-bottom: -12px;
}
</style>
</body>
</html>       

Related Post