简体   繁体   中英

Data not insert into mysql DataBase in php

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>

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">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM