PHP mysqli_query语法插入错误

时间:2016-04-01 21:35:20

标签: php mysql

我想使用PHP将数据插入MySQL表。

这是我的代码:

<?php
    $het = $_GET['het'];

    header('Content-Type: application/json; Charset=UTF-8');
    $connection = mysqli_connect("localhost:8889","root","root","imreigye_wp") or die("Error " . mysqli_error($connection));
    mysqli_set_charset($connection, 'utf8mb4');

    //This SQL string is really long
    $sql = "INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','" . $het . "','1','','','','','','',''); INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','" . $het . "','2','','','','','','',''); INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','" . $het . "','3','','','','','','',''); INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','" . $het . "','4','','','','','','',''); INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','" . $het . "','5','','','','','','','');";
    mysqli_query($connection, $sql) or die("ERROR: " . mysqli_error($connection));
    mysqli_close($connection);
?>

当我运行我的代码时,我收到MySQL错误:

  

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在“INSERT INTO rendelesiidokEvHetNapBattaR附近使用正确的语法,第1行BattaTHacsekR,`'   但是,如果我通过phpMyAdmin运行它,我的MySQL查询运行没有问题。   这是我的MySQL日志:

160401 23:07:22    15 Connect   root@localhost on imreigye_wp
           15 Query SET NAMES utf8mb4
           15 Query INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','14','1','','','','','','',''); INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','14','2','','','','','','',''); INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','14','3','','','','','','',''); INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','14','4','','','','','','',''); INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) VALUES ('2016','14','5','','','','','','','')
           15 Quit

注意:我已经尝试过使用简单的引号而不是特殊的引号。这没有用。

1 个答案:

答案 0 :(得分:1)

我认为mysqli_query()不支持多个查询。您想要做的事情可以写成1个查询,如下所示:

$sql = "INSERT INTO `rendelesiidok`(`Ev`, `Het`, `Nap`, `BattaR`, `BattaT`, `HacsekR`, `HacsekT`, `MolnarR`, `MolnarT`, `Pentek`) 
VALUES ('2016','" . $het . "','1','','','','','','',''),
('2016','" . $het . "','2','','','','','','',''), 
('2016','" . $het . "','3','','','','','','',''), 
('2016','" . $het . "','4','','','','','','',''), 
('2016','" . $het . "','5','','','','','','','')";

有关详细信息,请访问MySQL docs