Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting data to table (mysqli insert) [duplicate]

I've been looking at this code for a while now and I can't see where the problem is. I have been reading the whole of StackOverflow and still can't see where my error is.

<?php

mysqli_connect("localhost","root","","web_table");
mysql_select_db("web_table") or die(mysql_error());

if (mysqli_connect_errno()) {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

}
echo "<p> Connection Successful!"

mysqli_query('INSERT INTO web_formitem (ID, formID, caption, key, sortorder, type, enabled, mandatory, data) VALUES (105, 7, Tip izdelka (6), producttype_6, 42, 5, 1, 0, 0)');


echo "<p>Insert successfull";

?>

The error is somewhere in line 13, thats mysqli_query('insert.... I tried to help myself with http://www.w3schools.com/php/php_mysql_insert.asp but it's not helping me much.

like image 388
Blue Avatar asked May 30 '13 12:05

Blue


1 Answers

Warning: Never ever refer to w3schools for learning purposes. They have so many mistakes in their tutorials.

According to the mysqli_query documentation, the first parameter must be a connection string:

$link = mysqli_connect("localhost","root","","web_table");

mysqli_query($link,"INSERT INTO web_formitem (`ID`, `formID`, `caption`, `key`, `sortorder`, `type`, `enabled`, `mandatory`, `data`)
VALUES (105, 7, 'Tip izdelka (6)', 'producttype_6', 42, 5, 1, 0, 0)");

Note: Add backticks ` for column names in your insert query as some of your column names are reserved words.

like image 158
Rikesh Avatar answered Oct 27 '22 19:10

Rikesh