PDO 中的表单以更新数据

Mar*_*rco 0 php mysql pdo

我一直在环顾四周,甚至在网站上,但我无法在 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));
}
Run Code Online (Sandbox Code Playgroud)

和,

<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>
Run Code Online (Sandbox Code Playgroud)

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

Kuy*_*uya 5

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

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在你的表格必须呼应。

这是产生的过程

<?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();
}
?>
Run Code Online (Sandbox Code Playgroud)

这是结果形式(更容易阅读缩进)

<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>
Run Code Online (Sandbox Code Playgroud)

快乐编码!