Javatpoint Logo

php + mysqli database insertion prob....help me sir

By: nakuls*** On: Wed Mar 15 13:03:24 IST 2017     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
When i reload the page it will automatically inserted last input data
in the database. If i reload again then it will again inserted last
input data. Also if i put new data then it inserted the current new
data. again after inserted new data if i reload the page (without
giving nothing in the input field) then it automatically inserted last
new data.

student_name student_city
DON india
DON india (same data )



code:

<html>
<head>
<title>insert data in database using mysqli</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form id="form1" method="post" action="<?php echo
htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<label>Student Name :</label>
<input type="text" name="stu_name" id="name" required="required"
placeholder="Please Enter Name"/><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" name="submit" id="submit" value="Submit
Data" /><br />
</form>

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "college";

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST['submit']))
{
//$name = $email = $city = "";
$name= ($_POST['stu_name']);
$city= ($_POST['stu_city']);
echo "$name" . "<br/>";
echo "$city" . "<br/>";

$sql = "INSERT INTO students (student_name, student_city) VALUES
('$name','$city')";
echo "$sql"; //its useless just checking


if (mysqli_query($conn,$sql))
{
echo "<br/> " . "<br/> " . "<br/> " . "New record created
successfully";


}
else
{
echo "<br/> " . "<br/> " . "<br/> " . "Not successfull";

}
//mysqli_free_result($sql);

}

$conn->close();

?>

</body>
</html>



i dont want want to redirect the page to other page.......for processing......
Up0Down