13

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Blue
  • 261
  • 1
  • 3
  • 14
  • what is mysql_select_db doing? you are using mysqli right? It does nothing. – Bere May 30 '13 at 12:15
  • and also [Mysqli update throwing Call to a member function bind_param() error](http://stackoverflow.com/a/15447204/285587) – Your Common Sense May 30 '13 at 12:19
  • Does this answer your question? [mysqli\_query expects at least 2 parameters](https://stackoverflow.com/questions/8073278/mysqli-query-expects-at-least-2-parameters) – Dharman Nov 10 '19 at 21:45
  • Change the line mysql_select_db("web_table") or die(mysql_error()); to mysqli_select_db("web_table") or die(mysqli_error()); – Syed Naeem Tariq Jan 27 '21 at 06:35

3 Answers3

32

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • the problem is still in the same line.. (mysqli_query..) and it didnt fix the problem. Thanx for advice about w3. – Blue May 30 '13 at 12:40
  • Post your full error here please. Also check my updated answer with backticks – Rikesh May 30 '13 at 12:41
  • ( ! ) Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\wamp\www\bilform\insert.php on line 14 Stil the same S*?t – Blue May 30 '13 at 12:52
  • bin ? can you be more specific please ? – Blue May 30 '13 at 13:02
  • I mean paste your code on this site http://pastebin.com/ , so that we can have a closer look to at. – Rikesh May 30 '13 at 13:03
  • @Blue - sorry I forgoted one `)` at end of the statement. Take this code http://pastebin.com/XdYU3Guw – Rikesh May 30 '13 at 13:08
  • sme problem here; mysqli_query($link,"INSERT INTO vranesic_formitem (`ID`, `formID`, `caption`, `key`, `sortorder`, `type`, `enabled`, `mandatory`, `data`) ( ! ) Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\wamp\www\bilform\insert.php on line 12 – Blue May 30 '13 at 13:11
  • I can't see backtics in your error code. Can you check the exact code I have given to you ? – Rikesh May 30 '13 at 13:15
  • I put the ; at the beginning so now its working.. and it starts like this ;mysqli_query($link,"INSERT... ; has to be at the begining, why is that ? – Blue May 30 '13 at 13:21
  • 1
    @Blue - actually you have missed `;` after the echo statement above the mysqli_query. – Rikesh May 30 '13 at 13:23
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Nov 10 '19 at 21:37
2

In mysqli_query(first parameter should be connection,your sql statement) so

$connetion_name=mysqli_connect("localhost","root","","web_table") or die(mysqli_error());
mysqli_query($connection_name,'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)');

but best practice is

$connetion_name=mysqli_connect("localhost","root","","web_table") or die(mysqli_error());
$sql_statement="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)";
mysqli_query($connection_name,$sql_statement);
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Nov 10 '19 at 21:37
-1

Okay, of course the question has been answered, but no-one seems to notice the third line of your code. It continuosly bugged me.

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

for some reason, you made a mysqli connection to server, but you are trying to make a mysql connection to database.To get going, rather use

       $link = mysqli_connect("localhost","root","","web_table");
       mysqli_select_db ($link , "web_table" ) or die.....

or for where i began

     <?php $connection = mysqli_connect("localhost","root","","web_table");       
      global $connection; // global connection to databases - kill it once you're done

or just query with a $connection parameter as the other argument like above. Get rid of that third line.