gpt4 book ai didi

php - PDO 中的表单更新数据

转载 作者:太空宇宙 更新时间:2023-11-03 12:00:21 24 4
gpt4 key购买 nike

我一直在四处寻找,甚至在网站上寻找,但我无法在 PDO 中找到正确的语法来更新数据,例如用户配置文件的数据。

你能给我一个html格式的实际例子吗?我知道也许我要求那么多,但我做不到。

我附上了迄今为止能够做到但没有奏效的东西。

if(isset($_POST['submit'])) {
$email = $_POST['email'];
$location = $_POST['location'];
$id = $_SESSION['memberID'];

$stmt = $db->prepare("UPDATE `members` SET `email` = :email, `location` = :location WHERE `memberID` = :id");

$stmt->bindParam(":email", $email, PDO::PARAM_STR);
$stmt->bindParam(":location", $location, PDO::PARAM_STR);
$stmt->bindParam(":id", $_SESSION['memberID'], PDO::PARAM_STR);

$stmt->execute(array(':email' => $_POST['email'], ':location' => $_POST['location'], ':id' => $id));
}

还有,

<form role="form" method="POST" action="<?php $_PHP_SELF ?>">
<div class="form-group">
<label class="control-label">Email</label>
<input type="text" value="<?php echo $_SESSION['email'] ?>" name="email" id="email" class="form-control"/>
</div>
<div class="form-group">
<label class="control-label">Location</label>
<input type="text" value="<?php echo $_SESSION['location'] ?>" name="location" id="location" class="form-control"/>
</div>
<div class="margiv-top-10">
<input type="submit" name="submit" class="btn green" value="Update" >
<a href="profile.html" class="btn default">Annuller </a>
</div>
</form>

我想知道查询同一个页面是否安全正确,还是我应该创建一个类?你能帮忙举一个实际的例子吗,因为我什么都试过了。

最佳答案

首先,我将解释我对您的代码所做的一些更改。

  1. 不需要反引号,除非您使用保留字,所以我删除了它们

  2. 您已经将 $id 定义为 $id = $_SESSION['memberID']; 所以我更改了 $stmt->bindParam(":id ", $_SESSION['memberID'], PDO::PARAM_STR);

  3. 如果要绑定(bind)参数,则不需要使用数组执行,所以我更改了 $stmt->execute(array(':email' => $_POST['email'], ' :location' => $_POST['location'], ':id' => $id));$stmt->execute();

  4. 必须回显表单中的action

这是最终的过程

<?php
if(isset($_POST['submit'])) {

$email = $_POST['email'];
$location = $_POST['location'];
$id = $_SESSION['memberID'];
$sql = "UPDATE members SET email=:email, location=:location WHERE memberID=:id";
$stmt = $db->prepare($sql);
$stmt->bindValue(":email", $email, PDO::PARAM_STR);
$stmt->bindValue(":location", $location, PDO::PARAM_STR);
$stmt->bindValue(":id", $id, PDO::PARAM_STR);
$stmt->execute();
}
?>

这是生成的表单(缩进后更易于阅读)

<form role="form" method="POST" action="<?php echo $_PHP_SELF ?>">
<div class="form-group">
<label class="control-label">Email</label>
<input type="text" value="<?php echo $_SESSION['email'] ?>" name="email" id="email" class="form-control"/>
</div>
<div class="form-group">
<label class="control-label">Location</label>
<input type="text" value="<?php echo $_SESSION['location'] ?>" name="location" id="location" class="form-control"/>
</div>
<div class="margiv-top-10">
<input type="submit" name="submit" class="btn green" value="Update" >
<a href="profile.html" class="btn default">Annuller </a>
</div>
</form>

关于php - PDO 中的表单更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29586585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com