[英]How to insert more than 1 row into foreign key table in php mysql?
$notice = "SELECT id FROM notification
WHERE noti_user ='Pro'";
$noticeqry = mysqli_query($con, $notice);
$sql = "INSERT INTO `user_notification` (`notif_id`, `user_id`)
values while($noticerow = mysqli_fetch_array($noticeqry)){("$noticerow['id']", "$univuid"}";
$query= mysqli_query($con,$sql);
不工作並出現錯誤解析錯誤:語法錯誤,意外變量“$noticerow”
在INSERT
語法是路要走,而除了這個事實存在SQL注入的問題,我相信你正在試圖做的是:
while ($noticerow = mysqli_fetch_array($noticeqry)) {
$sql = "INSERT INTO `user_notification` (`notif_id`, `user_id`)
VALUES ({$noticerow['id']}, $univuid)";
mysqli_query($con, $sql);
}
但是,我強烈建議轉為使用參數化查詢來避免 SQL 注入問題,這可以將上述處理替換為:
$sql = 'INSERT INTO `user_notification` (`notif_id`, `user_id`) VALUES (?, ?)';
$stmt = mysqli_prepare($con, $sql);
while ($noticerow = mysqli_fetch_array($noticeqry)) {
mysqli_stmt_bind_param($stmt, 'ii', $noticerow['id'], $univuid);
mysqli_stmt_execute($stmt);
}
也許您可以嘗試通過 for 循環插入,如下所示。
$notice = "SELECT id FROM notification WHERE noti_user ='Pro'";
$noticeqry = mysqli_query($con, $notice);
$row = $noticeqry->fetch_assoc();
$count_id = mysqli_num_rows($noticeqry);
for($i=0; $i < $count_id; $i++){
$noticerow = $row['id'][$i];
$sql = "INSERT INTO `user_notification` (`notif_id`, `user_id`) values ('$noticerow', '$univuid')";
$query= mysqli_query($con,$sql);
}
if($quey){echo "New record created successfully";}else{ echo "Error: " . $sql . "<br>" . mysqli_error($con);}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.