Reputation: 35
DataBase connect but not insert the data into table
DataBase Name School Table Name form
Data successfully insert into mysql sql Consol by using this insert Query $sql="insert into form(name,gender) Values('jany','female')";
and not showing data added Massage also
<?php
$con=mysqli_connect("localhost","root","","school");
if(mysqli_connect_errno()){
echo "failed".mysqli_connect_error();
}
if(isset($_POST['submit'])){
$name=$_POST['name'];
$gender=$_POST['gender'];
$sql="insert into form(name,gender) Values('".$name."','".$gender."')";
if(!mysqli_query($con,$sql)){
die('error'.mysqli_error($con));
}
else echo "data added";
}
mysqli_close($con);
?>
<html><head></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="">
Name : <input type=text name=name ><br>
Last Name : <input type=text name=gender ><br>
<input type=submit name=submit value=add ><br>
</body>
</html>
Upvotes: 0
Views: 1520
Reputation: 91734
You really should switch to prepared statements to get rid of the sql injection problem you have now, but your current problem is caused by the fact that your are not doing a POST
request, so if(isset($_POST['submit'])){
will always return false
.
The default method for posting a form is GET
so change it to:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Upvotes: 1