mysqli预处理语句更新查询在一个php文件中返回“对非对象调用成员函数bind_param()”错误;但是,它在一个更简单的测试页面上运行完美。
查询运行正常的页面:
<?php require 'db/connect.php'; ?>
<?php
session_start();
if(isset($_SESSION["UserID"])){
}else{
header('Location: LogIn.php');
}
?>
<?php
if (isset($_POST['Submit'])){
$User = $_SESSION["UserID"];
$updFName = $_POST['First'];
$updLName = $_POST['Last'];
$updEmail = $_POST['Email'];
$updPW = $_POST['Pass'];
$sql = "UPDATE user SET Fname = ?, Lname = ?, Email = ?, Password = ? WHERE UserID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ssssi', $updFName, $updLName, $updEmail, $updPW, $User);
$stmt->execute();
}
?>
<!DOCTYPE html>
<html>
<body>
<form name="form1" method="post" action="test.php">
<input type="text" name="First" id="First">
<input type="text" name="Last" id="Last">
<input type="email" name="Email" id="Email">
<input type="password" name="Pass" id="Pass">
<input type="submit" name="Submit" id="Submit" value="Submit">
</form>
<?php echo $sql . '<br>'; ?>
<?php echo $updFName . '<br>'; ?>
<?php echo $updLName . '<br>'; ?>
<?php echo $updEmail . '<br>'; ?>
<?php echo $updPW . '<br>'; ?>
<?php echo $User; ?>
</body>
</html>
此页面上的代码返回“对非对象调用成员函数bind_param()”错误。有什么建议为什么?感谢
<?php require 'db/connect.php'; ?>
<?php
session_start();
if(isset($_SESSION["UserID"])){
}else{
header('Location: LogIn.php');
}
?>
<?php
$User = $_SESSION["UserID"];
$sql = "SELECT UserID, Fname, LName, Email, Password FROM user WHERE UserID=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i",$User);
$stmt->execute();
$stmt->bind_result($id, $fname, $lname, $email, $pw);
if ($stmt->fetch()){
$_SESSION['FirstName'] = $fname;
$_SESSION['LastName'] = $lname;
$_SESSION['Email'] = $email;
$_SESSION['Password'] = $pw;
}
$stmt->close();
$conn->close();
?>
<?php
if(isset($_POST['Update'])){
$User = $_SESSION["UserID"];
$updFName = $_POST['First'];
$updLName = $_POST['Last'];
$updEmail = $_POST['Email'];
$updPW = $_POST['Pass'];
$sql = "UPDATE user SET Fname = ?, Lname = ?, Email = ?, Password = ? WHERE UserID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ssssi', $updFName, $updLName, $updEmail, $updPW, $User);
$stmt->execute();
//header('Location: UpdateAccount.php');
$stmt->close();
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Account</title>
<meta name="generator" content="Bluefish 2.2.5" >
<link href="css/Menu.css" rel="stylesheet" type="text/css">
<link href="css/Master.css" rel="stylesheet" type="text/css">
<meta charset="utf-8">
</head>
<body>
<div class="Container">
<div class="Header"></div>
<div class="Menu">
<div id="Menu">
<nav>
<ul class="cssmenu">
<li><a href="#">Register</a></li>
<li><a href="#">LogIn</a></li>
</ul>
</nav>
</div>
</div>
<div class="LeftBody"></div>
<div class="RightBody">
<form method="post" name="UpdateForm" id="UpdateForm" action="UpdateAccount.php">
<div class="FormElement">
<input type="text" name="First" required="required" class="TField" id="First" value="<?php echo $_SESSION["FirstName"]; ?>">
</div>
<div class="FormElement">
<input type="text" name="Last" required="required" class="TField" id="Last" value="<?php echo $_SESSION["LastName"]; ?>">
</div>
<div class="FormElement">
<input type="email" name="Email" required=required class="TField" id="Email" value="<?php echo $_SESSION['Email']; ?>">
</div>
<div class="FormElement">
<input type="password" name="Pass" required="required" class="TField" id="Pass" value="<?php echo $_SESSION["Password"]; ?>">
</div>
<div class="FormElement">
<input type="submit" name="Update" class="Button" id="Update">
<?php
echo "User: " . $User . '<br>';
echo "updFName: " . $updFName . '<br>';
echo "updLName: " . $updLName . '<br>';
echo "updEmail: " .$updEmail . '<br>';
echo "updPW:" .$updPW . '<br>';
?>
</div>
</form>
</div>
<div class="Footer"></div>
</div>
</body>
</html>