php 向表插入数据(mysqli insert)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16835753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting data to table (mysqli insert)
提问by Blue
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.
我已经看这段代码一段时间了,但我看不出问题出在哪里。我一直在阅读整个 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";
?>
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.aspbut it's not helping me much.
错误在第 13 行的某处,即mysqli_query('insert...
. 我试图用http://www.w3schools.com/php/php_mysql_insert.asp帮助自己,但它对我帮助不大。
回答by Rikesh
Warning:Never ever refer to w3schoolsfor learning purposes. They have so many mistakes in their tutorials.
警告:永远不要为了学习目的而参考w3schools。他们的教程中有很多错误。
According to the mysqli_querydocumentation, the first parameter must be a connection string:
根据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)")
or die(mysqli_error($link));
Note:Add backticks ` for column names in your insert query as some of your column names are reserved words.
注意:在插入查询中为列名添加反引号 `,因为您的某些列名是保留字。
回答by Love Shankar Shrestha
In mysqli_query(first parameter should be connection,your sql statement) so
在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)');
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);
回答by Tapiwanashe Augustine
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
出于某种原因,你建立了一个到服务器的 mysqli 连接,但你正试图建立一个到数据库的 mysql 连接。要开始,而是使用
$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.
或者只是使用 $connection 参数作为上面的另一个参数进行查询。去掉第三行。
回答by Vikash Mishra
While using php mysqli functions, keep in mind that the connection details are mentioned first then the query is passed in the function
在使用 php mysqli 函数时,请记住首先提到连接详细信息,然后在函数中传递查询
$cid=mysqli_connect("server", "username", "password", "database_name") or die (mysqli_error($cid));
mysqli_query($cid, $query) or die (mysqli_error($cid));
$result=mysqli_affected_rows($cid);
if($result===TRUE)
echo"The query ran successfully";
else
echo"The query did not run";
mysqli_close($cid);
Keep in mind the $cid is the variable which holds the connection data in the above codes.
请记住, $cid 是保存上述代码中连接数据的变量。
回答by Vlad Bereschenko
What about this?
那这个呢?
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')");
回答by Amir
if one of your columns like ID
are autoincrement, you should not assign value for it. just put NULL
for its value.
如果您的其中一列ID
是自动增量,则不应为其分配值。只是NULL
为了它的价值。
回答by Matheno
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')');