基于previous查询结果的PHP / MySQL插入多条记录 [英] PHP / MySQL Insert Multiple Records Based On Previous Query Results
问题描述
我试图制定通知到我的网站的某些功能进行更改的premium成员PHP脚本,但我不知道如何将多个记录添加到通知表。我拥有的是:
I'm trying to develop a PHP script that notifies premium members of changes made to certain features of my website but I'm not sure how to add the multiple records to the notifications table. What I have is:
$get_pro_sql = "SELECT id FROM members WHERE pro = '1'";
$get_pro_res = mysqli_query($con, $get_pro_sql);
while($row = mysqli_fetch_assoc($get_pro_res){
$memberid = $row["id"];
}
$message = "My New Message";
foreach($row){
$insert_note_sql = "INSERT INTO notes(id, recipient, message, ..., ...) VALUES('', 'Member_Id__Returned_From_Previous_Query', '$message')";
$insert_note_res = mysqli_query($con, $insert_note_sql);
// Inserts 5 Records
如何可以做到这一点任何想法?
Any ideas on how this can be achieved?
推荐答案
您可以通过这个使用子查询来插入多行。
You could this by using a sub query to insert multiple rows.
其实你可以做到这样的:
Actually you can achieve it like this:
INSERT INTO Results (People, names )
SELECT d.id, 'Henry'
FROM Names f
JOIN People d ON d.id = f.id
在子查询中,您可以选择要插入的实际值。
In the subquery you can select the actual values to be inserted.
<一个href=\"http://stackoverflow.com/questions/9422529/mysql-how-do-you-insert-into-a-table-with-a-select-subquery-returning-multiple-r\">More关于这一主题这里
这篇关于基于previous查询结果的PHP / MySQL插入多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!