在php中使用Insert,Select和values

时间:2018-01-21 18:31:54

标签: php mysql

我在尝试插入多行数据时遇到困难,其中userid = deviceid(将从.php网址中获取)

e.g。 url.com/test/this.php?deviceid=abc

这是我的代码:

$sql = "INSERT INTO notif (message,userid) VALUES ('hi', (select userid from devices where deviceid = '" . $deviceid . "'))";

 if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

主要目标是我希望数据库插入“hi”消息的用户数量

这是一个例子

留言|用户ID

嗨|约翰

嗨|玛丽

嗨|杰克

等。

请帮助我..我会尽力回复

感谢

1 个答案:

答案 0 :(得分:1)

INSERT INTO notif (message,userid)
  SELECT 'Hi', user_id 
  FROM devices WHERE
   deviceid = :deviceid