12

我一直在看这段代码一段时间,我看不出问题出在哪里。我一直在阅读整个 StackOverflow,但仍然看不到我的错误在哪里。

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

?>

错误在第 13 行某处,即mysqli_query('insert.... 我试图通过http://www.w3schools.com/php/php_mysql_insert.asp来帮助自己,但这对我帮助不大。

4

3 回答 3

31

警告:切勿出于学习目的参考w3schools 。他们的教程中有很多错误。

根据mysqli_query文档,第一个参数必须是连接字符串:

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

注意:在插入查询中为列名添加反引号`,因为某些列名是保留字。

于 2013-05-30T12:15:10.293 回答
3

在 mysqli_query(第一个参数应该是连接,你的 sql 语句)所以

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

但最佳做法是

$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);
于 2014-06-21T11:55:14.790 回答
0

好的,当然问题已经得到解答,但似乎没有人注意到您的代码的第三行。它不断地困扰着我。

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

出于某种原因,您与服务器建立了 mysqli 连接,但您正试图建立与数据库的 mysql 连接。开始吧,而不是使用

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

或者我开始的地方

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

或者只是使用 $connection 参数作为上面的另一个参数进行查询。摆脱第三行。

于 2016-07-29T20:20:18.150 回答