Soma Zambelly
Soma Zambelly

Reputation: 197

PHP mysqli_query Syntax Error in insert

I want to insert data into a MySQL table using PHP.

Here is my code:

<?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);
?>

When I run my code I get a MySQL error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO rendelesiidok(Ev, Het, Nap, BattaR, BattaT, HacsekR, `' at line 1 However, my MySQL query runs with no problem if I run it trough phpMyAdmin. Here is my MySQL log:

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

NOTE: I already tried using simple quotes instead of the special ones. That didn't help.

Upvotes: 0

Views: 88

Answers (1)

lp1051
lp1051

Reputation: 501

I don't think mysqli_query() supports multiple queries. What you want to do can be written in 1 query like this:

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

For more info visit MySQL docs

Upvotes: 1

Related Questions