我有两个数组$ fuel和$ hours,数据量相同,我想在数据库中同时插入它们。
这就是我所拥有的:
$fuellength = count($fuel);
$f = 0;
for($f=0;$f<$fuellength;$f++){
if ($fuel[$f] > 0){
if (
mysql_query("INSERT INTO grafiku (vehicle_plate, fuel_level, date_hour) VALUES (".$userid.", ".$hours[$f].", '".$fuel[$f]."')");
)
echo "data has been inserted";
else
echo "data has not been inserted";
} //end if
}//end for
它什么都没有给我看!对数组使用'for'循环是否正确?或者我应该只使用'foreach'?如果是这样,我怎么能在同一时间循环抛出两个数组?
提前致谢! 最好的问候
答案 0 :(得分:0)
如果你的代码完全是这样的话,你将看不到任何东西,因为你有一个致命的php错误。
您有一个ELSE
语句,而没有起始IF
语句。
从IF
语句中删除评论,它应该会再次运行。
答案 1 :(得分:0)
$fuellength = count($fuel);
$f = 0;
for($f=0;$f<$fuellength;$f++){
if ($fuel[$f] > 0){
$qryStr = "INSERT INTO grafiku (vehicle_plate, fuel_level, date_hour) VALUES (".$userid.", ".$hours[$f].", '".$fuel[$f]."')";
echo $qryStr."/br";
}
}
检查您是否收到查询和值。
答案 2 :(得分:0)
假设您已与MySQL服务器建立连接:
$cn = mysql_connect("hostname", "username", "password");
假设您选择了数据库:
mysql_select_db("databasename", $cn);
你的逻辑是正确的。但是您的SQL插入顺序与您的字段不匹配。您正尝试将$fuel[$f]
数据插入date_hour
字段。
$f = 0;
$added = 0;
$noAdded = 0;
while($f < count($fuel))
{
if ($fuel[$f] > 0)
{
if(mysql_query("INSERT INTO grafiku
(vehicle_plate, fuel_level, date_hour)
VALUES (".$userid.", ".$fuel[$f].", '".$hours[$f]."')"
))
$added++;
else
$noAdded++;
}
$f++;
}
//Then you can display additional information after inserting is done.
echo $added . " data has been added to table.";
echo "<br />";
echo $noAdded . " data could not be added to table.";
答案 3 :(得分:0)
您的代码中存在错误, 解析错误:语法错误,&lt;中的意外T_ELSE文件名&gt;在第12行
<?php
$fuellength = count($fuel);
$f = 0;
for($f=0;$f<$fuellength;$f++){
if ($fuel[$f] > 0){
if (mysql_query("INSERT INTO grafiku (vehicle_plate, fuel_level, date_hour) VALUES (".$userid.", ".$hours[$f].", '".$fuel[$f]."')"))
echo "data has been inserted";
else
echo "data has not been inserted";
} //end if
}//end for
?>