2

I'm getting this SQL error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO `tsv` (`Id`,`Date`,`Quantity`) VALUES( `1`,`2009-07-01`,`174`, `1`,`' at line 9

I'm not really experienced in SQL queries and i really can´t see whats wrong here... I tried replacing "`" with "'". And also removing "ENGINE=MyISAM" as I'm not sure thats the engine I'm using.

CREATE TABLE IF NOT EXISTS `tsv` 
( `_id` int(11) NOT NULL AUTO_INCREMENT, 
`Id` text NOT NULL, `Date` text NOT NULL, 
`Quantity` text NOT NULL, PRIMARY KEY (`_id`) ) 
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=68 ; 
INSERT INTO `tsv` (`Id`,`Date`,`Quantity`) 
VALUES( `1`,`2009-07-01`,`174`, `1`,`2009-07-02`,`96`, 
`1`,`2009-07-03`,`271`, `1`,`2009-07-04`,`335`, `1`,
`2009-07-06`,`72`, `1`,`2009-07-07`,`246`, `1`,`2009-07-08`,
`93`, `1`,`2009-07-09`,`191`, `1`,`2009-07-10`,`136`, `1`,
`2009-07-11`,`200`, `1`,`2009-07-13`,`151`, `1`,`2009-07-15`,`99`);

And the PHP:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
if ($conn->query($sqlContent) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sqlContent . "<br/>" . $conn->error;
}

$conn->close();
Jonas Borneland
  • 383
  • 1
  • 6
  • 19

3 Answers3

2
  • Every row must be enclosed in parantheses separately.
  • You need to use single quotes ('), not backticks (`) for values.
  • Also, you cannot have separate queries called inside mysqli_query. Use mysqli_multi_query for the same. From documentation:

Executes one or multiple queries which are concatenated by a semicolon.

Do the following:

CREATE TABLE IF NOT EXISTS `tsv` 
( `_id` int(11) NOT NULL AUTO_INCREMENT, 
`Id` text NOT NULL, `Date` text NOT NULL, 
`Quantity` text NOT NULL, PRIMARY KEY (`_id`) ) 
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=68 ; 

INSERT INTO `tsv` (`Id`,`Date`,`Quantity`) 
VALUES('1','2009-07-01','174'), 
      ('1','2009-07-02','96'),
      ('1','2009-07-03','271'), ... and so on
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
1

You do not need to provide id. Id will auto increment while inserting data.

INSERT INTO tsv ('Date','Quantity') 
VALUES( '2009-07-01','174'), ('2009-07-02','96'), 
('2009-07-03','271'), ('2009-07-04','335'), 
('2009-07-06','72'), ('2009-07-07','246'), ('2009-07-08',
'93'), (,'2009-07-09','191'), ('2009-07-10','136'), 
('2009-07-11','200'), ('2009-07-13','151'), ('2009-07-15','99');
Chayan
  • 604
  • 5
  • 12
1

You cannot have several queries be part of the same mysqli::query call.

You need to separate the two queries or use mysqli::multi_query.

More on multiple statements.

Jeto
  • 14,596
  • 2
  • 32
  • 46