我使用PDO使用以下方法将我的值更新为表
形式
<form role="form" method="post" action="submitupdate.php" autocomplete="off">
<input class="update" type="text" name="fullname"></li>
<input class="update" type="text" name="dob"></li>
<textarea style="width:740px;height:220px;" class="update" type="text" name="intrested"></textarea></li>
<textarea style="width:740px;height:220px;" class="update" type="text" name="description"></textarea></li>
</form>
处理
<?php
require('includes/config.php');
//if not logged in redirect to login page
if (!$user->is_logged_in()) {
header('Location: login.php');
}
//define page title
$title = 'Members Page';
//include header template
?>
<?php
try {
$username = $_SESSION['username'];
$fullname = $_POST['fullname'];
$dob = $_POST['dob'];
$intrested = $_POST['intrested'];
$description = $_POST['description'];
$sql = "UPDATE `members`
SET `fullname` = :firstname,
`dob` = :dob,
`intrested` = :intrested,
`description` = :description
WHERE `username` = :username";
$statement = $db->prepare($sql);
$statement->bindValue(":username", $username);
$statement->bindValue(":fullname", $fullname);
$statement->bindValue(":dob", $dob);
$statement->bindValue(":intrested", $intrested);
$statement->bindValue(":description", $description);
$count = $statement->execute();
$db = null; // Disconnect
} catch (PDOException $e) {
echo $e->getMessage();
}
使用ID
使用用户名而非更新列我使用username
进行更新,并使用会话调用用户名
我收到以下错误
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
有人可以帮助我
答案 0 :(得分:1)
在设置值之前,您正在执行查询。在执行查询时,您尝试使用的值尚未设置,因此没有任何数据可以替换占位符。
这应该有效:
<?php
require('includes/config.php');
//if not logged in redirect to login page
if (!$user->is_logged_in()) {
header('Location: login.php');
}
//define page title
$title = 'Members Page';
//include header template
try {
$username = $_SESSION['username'];
$fullname = $_POST['fullname'];
$dob = $_POST['dob'];
$intrested = $_POST['intrested'];
$description = $_POST['description'];
$sql = "UPDATE `members`
SET `fullname` = :fullname,
`dob` = :dob,
`inserted` = :inserted,
`description` = :description
WHERE `username` = :username";
$statement = $conn->prepare($sql);
$params = array(
":username" => $username,
":fullname" => $fullname,
":dob" => $dob,
":inserted" => $intrested,
":description" => $description
);
$count = $statement->execute($params);
$conn = null; // Disconnect
} catch (PDOException $e) {
echo $e->getMessage();
}
答案 1 :(得分:1)
代码是否已完成?
我看到$ row但你在哪里定义$ row?
顺便说一句,在设置要使用的值之前,您正在执行查询。
用以下代码替换您的代码:
<?php
require('includes/config.php');
//if not logged in redirect to login page
if (!$user->is_logged_in()) {
header('Location: login.php');
}
//define page title
$title = 'Members Page';
//include header template
try {
$sql = "UPDATE `members`
SET `fullname` = :firstname,
`dob` = :dob,
`inserted` = :inserted,
`description` = :description
WHERE `username` = :username";
$username = $_SESSION['username'];
$fullname = $row['fullname'];
$dob = $row['dob'];
$intrested = $row['intrested'];
$description = $row['description'];
$statement = $conn->prepare($sql);
$statement->bindValue(":username", $username);
$statement->bindValue(":fullname", $fullname);
$statement->bindValue(":dob", $dob);
$statement->bindValue(":inserted", $intrested);
$statement->bindValue(":description", $description);
$count = $statement->execute();
$conn = null; // Disconnect
} catch (PDOException $e) {
echo $e->getMessage();
}