使用循环插入一个数组表 [英] insert an array into table using for loop
问题描述
我试图插入一个数组到一个表中的mysql数据库的数据行。我在PHP,MySQL的初学者,有关于它知之甚少。我只是想了解更多信息。如果你可以试试这个这将是巨大的。
I am trying to insert rows of data in an array into a table in mysql database. I am a beginner in php, mysql and have very little knowledge about it. I just want to learn more. If you can give this a try It would be great.
在code,我想插入是如下:
The code which i want to insert is below:
for($x=0; $x<2; $x++)
{
$data[$x]['title'] = $titleQuery->item($x)->nodeValue;
$data[$x]['titleHrefQuery'] = $titleHrefQuery->item($x)->nodeValue;
$data[$x]['food'] = $foodQuery->item($x)->nodeValue;
$data[$x]['locality'] = $localityQuery->item($x)->nodeValue;
$data[$x]['rating'] = $ratingQuery->item($x)->nodeValue;
$data[$x]['cost'] = $costQuery->item($x)->nodeValue;
}
我特林使用下面给出的code插入:
I am tring to insert using the code given below:
$query = "INSERT INTO table (`title`, `link`, `food`, `locality`, `rating`, `cost`) VALUES
('" . $titleQuery->item($x)->nodeValue . "',
'".$titleHrefQuery->item($x)->nodeValue."',
'".$foodQuery->item($x)->nodeValue."',
'".$localityQuery->item($x)->nodeValue."',
'".$ratingQuery->item($x)->nodeValue."',
'".$costQuery->item($x)->nodeValue."')";
$result = mysql_query($query);
if($result)
{
echo ("Success");
}
else
{
echo ("Not added");
}
但每次都显示没有添加。请帮助!
But every time it shows not added. please help!!
推荐答案
修改
INSERT INTO table
到
INSERT INTO `table`
由于表是保留keyword.And如果你正在使用保留关键字作为表名或列名,那么你必须将他们背蜱(`)。而它最好不要使用任何储备金keyword.So如果你能更改名称,然后它会choice.You在这些问题检查更多最好
Because table is a reserved keyword.And if you are using reserved keywords as table name or column name then you must enclose them in back-ticks (`).And its better not to use any reserve keyword.So if you can change the name then it will be the best choice.You can check for more in these questions
-
<一个href=\"http://stackoverflow.com/questions/2889871/how-do-i-escape-reserved-words-used-as-column-names-mysql-create-table\">How我逃避作为列名的保留字?
我们能有表名作为&QUOT;选项&QUOT;在MySQL中?
<一个href=\"http://stackoverflow.com/questions/19758467/h2-database-column-name-is-reserved-word/19758863#19758863\">h2数据库列名的保留字
这篇关于使用循环插入一个数组表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!