[英]MySQL insert statement won't insert?
I have attached my PHP code below. 我在下面附加了我的PHP代码。 It calculates the cumulative value correctly and builds the SQL statement correctly (tested by echo $sql;
). 它可以正确计算累积值并正确构建SQL语句(通过echo $sql;
测试)。 However, the instert statement into MySQL is unsuccessful. 但是,插入MySQL的instert语句不成功。 Any ideas? 有任何想法吗?
<?php
ini_set('memory_limit', '500M');
set_time_limit(1800);
$dbh = new mysqli('localhost','user','password','database') or die(mysql_error());
$query = "SELECT * FROM table1 ORDER BY id, date";
$del = "DELETE FROM table2";
$dbh->query($del);
if ($dbh->multi_query($query)){
if ($result = $dbh->store_result()){
$id = 0;
while ($row = $result->fetch_row()){
$date = $row[0];
$cvalue = $row[2];
$id = $row[1];
$cumulative = 0;
// Pull most recent cumulative value
$sql_recent = "SELECT * FROM table2 WHERE id = $id ORDER BY date DESC LIMIT 1";
if ($dbh->multi_query($sql_recent)){
if ($result_recent = $dbh->store_result()){
while ($row_recent = $result_recent->fetch_row()){
if($row_recent[0] != $date){
$cumulative = $row_recent[2];
}
}
}
}
$cumulative += $cvalue;
$sql = 'INSERT INTO table2 (id, date, cumulative) VALUES (';
$sql .= "'".$row[1]."',";
$sql .= "'".$date."',";
$sql .= "'".$cumulative."',";
$sql .= ')';
$dbh->query($sql);
echo $sql;
}
}
}
$dbh->close();
?>
You have an extra comma after the last value: 最后一个值后面有一个逗号:
$sql .= "'".$cumulative."',";
Try removing that: 尝试删除它:
$sql .= "'".$cumulative."'";
$ cumulative之后的逗号将使语法错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.