PDO中的表格以更新数据 [英] Form in PDO to update data
问题描述
我一直在环顾四周,甚至在该站点上,但我在PDO中找不到正确的语法来更新数据,例如用户个人资料的数据.
I've been looking around and even here on the site, but I can not find the correct syntax in PDO to update data, such as the data of a user profile.
您可以用html表单给我一个实际的例子吗? 我知道也许我提出了很多要求,但是我无法使它生效.
You could give me a practical example with html form? I know that maybe I ask so much, but I can not make it work.
我附上到现在为止可以执行的操作,但是不起作用.
I enclose what until now have been able to do, but not work.
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>
我想知道查询同一页面是否安全,正确还是应该创建一个类?因为我已经尝试了一切,所以您能提供一个实际的例子吗?
I was wondering if it was safe and correct to query the same page or should I create a class? Can you help with a practical example because I have tried everything.
推荐答案
首先,我将解释我对您的代码所做的一些更改.
First I'll explain some of the changes I made to your code.
1)除非您使用保留字,否则不需要反引号,因此我将其删除
2)您已经将$id
定义为$id = $_SESSION['memberID'];
,所以我更改了$stmt->bindParam(":id", $_SESSION['memberID'], PDO::PARAM_STR);
3)如果要绑定参数,则不需要使用数组执行,因此我将$stmt->execute(array(':email' => $_POST['email'], ':location' => $_POST['location'], ':id' => $id));
更改为$stmt->execute();
4)必须回显您表单中的action
.
1) Back-ticks are not required unless you are using a reserved word, so I removed them
2) You are already defining $id
as $id = $_SESSION['memberID'];
so I changed $stmt->bindParam(":id", $_SESSION['memberID'], PDO::PARAM_STR);
3) If you are binding your parameters, you don't need to execute with an array, so I changed $stmt->execute(array(':email' => $_POST['email'], ':location' => $_POST['location'], ':id' => $id));
to $stmt->execute();
4) The action
in your form must be echoed.
这是结果过程
<?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();
}
?>
这是生成的表格(更易于阅读缩进)
This is the resulting form (easier to read with indentations)
<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>
快乐编码!
这篇关于PDO中的表格以更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!