php - 使用 for 循环将数组插入表中

标签 php mysql arrays database

我正在尝试将数组中的数据行插入到 mysql 数据库中的表中。我是php、mysql的初学者,对它的了解很少。我只是想了解更多。如果你能试一试,那就太好了。

我要插入的代码如下:

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;
}

我尝试使用下面给出的代码插入:

$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");
}

但是每次都显示未添加。请帮忙!!

最佳答案

改变

INSERT INTO table

INSERT INTO `table`

因为表是保留关键字。如果你使用保留关键字作为表名或列名,那么你必须将它们括在反引号(`)中。最好不要使用任何保留关键字。所以如果你可以改名字是最好的选择,你可以在这些问题中查看更多

  1. How do I escape reserved words used as column names? MySQL/Create Table

  2. Can we have the table name as "option" in MySQL?

  3. H2 database column name "GROUP" is a reserved word

关于php - 使用 for 循环将数组插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24951632/

相关文章:

javascript - 递归地将所有数组转换为多维Javascript对象中的对象

PHP 和 MYSQL 空白结果。脚本突然停止。无错误日志

php - 发布前预览音频文件

mysql - 当我们连接到 MS SQL/MySQL 数据库时,如何确保我和客户端的 PC 始终具有相同的 IP 地址?

MySQL 持久运行总计

python - 如何解析 's' 路径中的 numpy 数组?

PHP - 计算字符串的 "physical"(显示)宽度

javascript - WordPress 下拉导航的 Z-Index 问题

php - Android 使用 PHP 连接 MYSQL

Java集合列表内存结构