PHP MySQLi INSERT不起作用,没有错误 [英] PHP MySQLi INSERT not working, no errors

查看:43
本文介绍了PHP MySQLi INSERT不起作用,没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不同于此问题,但是类似,因为在向数据库添加信息时我没有收到错误.

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

插入换行符以避免侧向滚动...在网页上说mysqli_insert_id($ mysqli)为0,并且没有任何内容添加到数据库中的表中.我没有看到出现连接数据库的错误,并且我的服务器上正在运行MySQL,并且phpinfo()显示同时加载了MySQL和MySQLI扩展.这只是一台开发机器,因此不必担心安全性(即没有密码).我已尝试使用Google搜索该问题,但发现的内容却不是太多.我不了解使用->进行面向对象的PHP编程,我习惯于使用_.仍然支持此方法吗?

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样式.这导致您尝试使用像mysqli_query($mysqli)这样的函数而不是像$mysqli->query()这样的成员函数.您的$mysqli对象,而不是资源句柄.

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.

而且,您不会对查询执行任何错误检查.如果是这样,您会发现您错误地使用了单引号来分隔表名和字段名,而不是反引号.

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

我强烈建议使用手册作为参考.在使用过程式或面向对象样式的MySQLi时,如何使用这些功能已经很清楚了.

I strongly suggest using the manual as your reference. It's quite clear on how to use these functions when you're using either procedural or object-oriented style MySQLi.

这篇关于PHP MySQLi INSERT不起作用,没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆