【发布时间】:2016-01-24 21:02:21
【问题描述】:
我正在为我的一个朋友做一个项目,我们想在 mysqli 中编写我们的数据库调用。我是新手,我只使用了我知道此时已过时的 mysql 命令。我不断收到Call to a member function query() on a non-object on line 30,这是我的if ($mysqli->query($sql)) { 命令。谁能指出我正确的方向?我已经尝试在 W3 学校中查找它。这是我的完整代码:
// If the form is submitted, INSERT into table.
if (isset($_POST["submit"])) {
// Define $username and $password.
$username = $_POST['user_username'];
$password = $_POST['user_password'];
// Protect them from MySQL injection.
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
// Run some queries.
if ($_FILES["user_image"]["error"] > 0) {
//Bad Output for form results red text
echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";
} else {
move_uploaded_file($_FILES["user_image"]["tmp_name"],"uploads/" . $_FILES["user_image"]);
$file="uploads/".$_FILES["user_image"];
$image_title = addslashes($_REQUEST['user_image']);
$sql="INSERT INTO users (user_fname, user_lname, user_image, user_phone, user_cell, user_email, user_username, user_password) VALUES ('$_POST[user_fname]', '$_POST[user_lname]', '$_POST[user_image]', '$_POST[user_phone]', '$_POST[user_cell]', '$_POST[user_email]', '$username', '$password')";
if ($mysqli->query($sql)) {
die('Error: ' . $mysqli->error);
}
//Good Output for form results green text
echo '
<form enctype="multipart/form-data" action="insert_image.php" method="post" name="changer">
<div style="padding:10px;">
<h2 style="font-size: 28px;">Success!</h2>
<p style="font-size: 18px;">Your file has been successfully uploaded!</p>
</div>
</form>';
}
}
谢谢!
【问题讨论】:
-
$mysqli未在此代码中的任何位置定义 -
尝试使用$mysqli->real_query($query);
-
请不要使用 MD5 散列密码。 MD5 对于密码来说是快速且不安全的。请使用密码哈希 API。如果您没有 PHP 5.5,可以使用 PHP 5.3+ 的兼容性脚本。
-
您需要向我们展示您在此处连接的位置/方式。到目前为止,以下所有答案都是错误的。
-
@Fred-ii- 好吧,他确实使用转义并将
$db作为 mysqli 实例传递,所以他们没有错。他们处于一种状态,他们的答案要么是错的,要么是对的,或者两者都是。