尝试连接到mysql并输入数据但是没有点击提交也会将空值上传,求解下面为代码:
<!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){
接受值和添加数据库
}
22行开始执行数据写入了。加个if判断就好了,没有数据的时候不上传