Trying to connect to mysql and enter data but without clicking submit, null values will be uploaded. The following code is solved:
<!DOCTYPE html> <html> <head><meta charset="utf-8"> <title>input</title> </head> <body> <?php $lian= mysqli_connect('localhost','root','root','anu');//连接到anu if($lian){ echo"连接数据库成功</br>"; } else{ echo"连接数据库失败</br>"; } ?> <form action="Draft2.php" method="post"> <div>姓名:<input type="text" name="name" required=""></div> <div>就业意愿:<input type="text" name="job" required=""></div> <div>电话:<input type="text" name="phone" required=""></div> <input type="submit" name="1" value="保存"><!--create--> <?php $name = $_POST['name']; $job = $_POST['job']; $phone = $_POST['phone']; $jia = "INSERT INTO draft VALUES('$name','$job','$phone')"; // if ($jia) { // echo "添加成功·</br>"; // } // else{ // echo "添加失败"; // } mysqli_query($lian,$jia); // or die(mysqli_error($lian)); mysqli_free_result($lian); mysqli_close($lian)//断开数据库连接 ?> </form> </body> </html>
if($_POST){
Accept value and add database
}
Line 22 starts data writing. Just add an if judgment and don’t upload if there is no data