嘗試連接到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判斷就好了,沒有資料的時候不上傳