[英]How to insert more than 1 row into foreign key table in php mysql?
Table1: notification Table2: user_notification表 1:通知表2:用户通知
$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);
Not working and getting error Parse error: syntax error, unexpected variable "$noticerow"不工作并出现错误解析错误:语法错误,意外变量“$noticerow”
The INSERT
syntax is WAY off, and besides the fact there is SQL injection issues, I believe what you're trying to do is:在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);
}
However, I'd strongly advise to shift into using parameterized queries to avoid SQL injection issues, which can replace the above handling with:但是,我强烈建议转为使用参数化查询来避免 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);
}
Maybe u can try insert through for loop, like bellow..也许您可以尝试通过 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.