0

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.

6
  • 1
    put each sql query into seperate variables to segment your code better and to make it easier to read. Also it could be due to the ` symbole being input on the names of your table and fields Commented Apr 1, 2016 at 21:41
  • Back ticks are perfectly acceptable in MySQL (and other SQL environments) @Juakali92 Commented Apr 1, 2016 at 21:43
  • @JayBlanchard fair enough, didn't realise you could Commented Apr 1, 2016 at 21:43
  • Actually you should use them all of the time if you wanted to prevent problems with possible keywords used as table/column names, etc. Using any single or double quote around table/column names will cause the query to fail. Commented Apr 1, 2016 at 21:45
  • 2
    You should be using mysqli_multi_query or use multiple value sets in one query. Commented Apr 1, 2016 at 21:46

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.