PHP MySQLi INSERT 不工作,没有错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6811070/
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
PHP MySQLi INSERT not working, no errors
提问by Canadian Luke
Different from this question, but similar in that I don't get an error when adding information to my database.
与此问题不同,但相似之处在于,将信息添加到我的数据库时我没有收到错误消息。
$sql = "INSERT INTO 'nlcc_ver1'.'tUsers' ('userID', 'userName', 'userPassword', 'userHash',
'user_first_name', 'user_last_name', 'user_corps', 'is_admin', 'is_trg', 'is_sup', 'is_co')
VALUES (NULL, '" . $userName . "', '" . $hash . "', '" . $salt . "', '" . $f_name . "', '" .
$l_name . "', '" . $corps . "', '" . $admin . "', '" . $trg . "', '" . $sup . "', '" . $co . "')";
$hostname_Database = "localhost";
$database_Database = "nlcc_ver1";
$username_Database = "root";
$password_Database = "";
$mysqli = new mysqli($hostname_Database, $username_Database, $password_Database, $database_Database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = $mysqli_query($mysqli, $sql);
echo "Query run. Inserted UserID " . mysqli_insert_id($mysqli) . "<br />";
Line breaks inserted to avoid sideways scrolling... It says on the web page that mysqli_insert_id($mysqli) is 0, and nothing is added to the table on my database. I do not see an error connecting to the database appearing, and MySQL is running on my server, and phpinfo() shows both the MySQL and MySQLI extension loaded. This is just a development machine, so don't worry about the security (i.e. no password). I have tried googling the problem, but am not finding too much. I don't know about object oriented PHP programming with ->, I am used to using _. Is this method still supported?
插入换行符以避免横向滚动...它在网页上说 mysqli_insert_id($mysqli) 为 0,并且没有向我的数据库的表中添加任何内容。我没有看到连接到数据库的错误出现,而且 MySQL 正在我的服务器上运行,并且 phpinfo() 显示加载了 MySQL 和 MySQLI 扩展。这只是一个开发机器,所以不要担心安全性(即没有密码)。我试过用谷歌搜索这个问题,但没有找到太多。我不知道使用->的面向对象的PHP编程,我习惯使用_。这个方法还支持吗?
回答by Lightness Races in Orbit
You've mixed procedural and object-oriented MySQLi styles. This has led to you trying to use the functions like mysqli_query($mysqli)
instead of the member functions like $mysqli->query()
. Your $mysqli
is an object, not a resource handle.
您已经混合了面向过程和面向对象的 MySQLi 风格。这导致您尝试使用像这样的函数mysqli_query($mysqli)
而不是像$mysqli->query()
. 你$mysqli
是一个对象,而不是一个资源句柄。
And, you're not performing any error checking on your query. If you were, you'd see that you have mistakenly used single quotes to delimit table and field names, not backticks.
而且,您没有对查询执行任何错误检查。如果是,您会发现您错误地使用单引号来分隔表名和字段名,而不是反引号。
$sql = "INSERT INTO `nlcc_ver1`.`tUsers`
(`userID`, `userName`, `userPassword`, `userHash`,
`user_first_name`, `user_last_name`, `user_corps`,
`is_admin`, `is_trg`, `is_sup`, `is_co`)
VALUES (NULL, '" . $userName . "', '" . $hash . "', '" . $salt . "', '" .
$f_name . "', '" . $l_name . "', '" . $corps . "', '" . $admin .
"', '" . $trg . "', '" . $sup . "', '" . $co . "')";
$hostname_Database = "localhost";
$database_Database = "nlcc_ver1";
$username_Database = "root";
$password_Database = "";
$mysqli = new mysqli($hostname_Database, $username_Database, $password_Database, $database_Database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = $mysqli->query($sql);
if (!$result) {
printf("%s\n", $mysqli->error);
exit();
}
echo "Query run. Inserted UserID " . $mysqli->insert_id . "<br />";
I strongly suggest using the manualas your reference. It's quite clear on how to use these functions when you're using either procedural or object-oriented style MySQLi.
我强烈建议使用手册作为您的参考。当您使用过程式或面向对象风格的 MySQLi 时,如何使用这些函数非常清楚。
回答by Sabeen Malik
$mysqli_query($mysqli, $sql);
should be
应该
mysqli_query($mysqli, $sql);
OR
或者
$mysqli->query($sql);
AND later on
后来
$mysqli->insert_id();
回答by Dr.Molle
Look at this:
看这个:
'nlcc_ver1'.'tUsers'
You have to use backticks here as quote character:
您必须在此处使用反引号作为引号字符:
`nlcc_ver1`.`tUsers`
But however(assuming that the $ in $mysqli_query is just a typo): You will not get errors for the query , unless you use mysqli_error() right after executing the query.
但是(假设 $mysqli_query 中的 $ 只是一个错字):您不会收到查询错误,除非您在执行查询后立即使用 mysqli_error()。
回答by HimalayanCoder
SET AutoCommit = 1 before inserting
插入前设置 AutoCommit = 1
$mysqli->query('SET AUTOCOMMIT = 1');