插入WHILE循环不插入数据库

时间:2014-04-08 11:44:07

标签: mysql while-loop sql-insert

我有一个SELECT语句,WHILE语句和一个INSERT:

$result = mysqli_query($con,"SELECT winner, time, course, market, twitter_pubstatus
FROM combo 
WHERE twitter_pubstatus = 0 AND market = '$win' GROUP BY winner");
while($row = mysqli_fetch_array($result))
{
$winner = $row['winner'];
$time = $row['time'];
$course = $row['course'];

$message = "$winner won the $time at $course.  You are a winner! #GetIn";

$query = "INSERT INTO messageTable (MESSAGE) VALUES($message)or die(mysql_error())";

}

它没有错误地运行。应该有12行插入到数据库中。我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试更改$query = "INSERT INTO messageTable (MESSAGE) VALUES($message)or die(mysql_error())"; 至  $query = "INSERT INTO messageTable (MESSAGE) VALUES('$message')or die(mysql_error())";

请注意'$message'

中的单引号

$ query只是一个字符串,所以执行查询
$result=mysqli_query($query)

然后通过执行此操作检查是否执行了查询 if(!$result) die(mysqli_error());