獲取數據我有一個問題:PHP不是從MySQL
我的代碼,這是一個:
$id=$_SESSION["id"];
$query="select id_period from periods where id_group=$id and end=NULL";
$result=mysqli_query($conn,$query);
if(mysqli_num_rows($result)==0){
$insert=mysqli_query($conn,"insert into periods(id_group) values($id)");
}
while($row=mysqli_fetch_row($result)){
$period=$row['id_period'];
}
$query="insert into times(id_period,id_group) values($period,$id)";
echo $query;
$insert=mysqli_query($conn,$query);
如果存在「期間」與現場「端」 = NULL此代碼應搜索。如果不是,則插入一個新的。這工作正常。但是,下一部分不起作用。插入'times'查詢需要由var $ period給出的id_period,但WHILE永遠不會運行。
有什麼想法?謝謝。
**警告**:使用'mysqli'時,您應該使用[參數化查詢](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param' ](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 **不要**將'$ _POST','$ _GET'或**任何**用戶數據直接放入查詢中,如果有人試圖利用您的錯誤,這可能會非常有害。 – tadman
[與NULL值比較]可能的重複(https://stackoverflow.com/questions/2393076/comparing-with-null-values) – CBroe