2015-04-12 56 views
0

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

而且,

<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> 

我想知道,如果它是安全的,正确的查询相同的页面,或者我应该创建一个类?你能帮助一个实际的例子,因为我已经尝试了一切。

+0

什么不起作用? – Rasclatt

+0

如果你有' - > bindParam',' - > execute()'应该有空参数,你不需要同时拥有这两个参数。或者删除所有' - > bindParam'语句,并使用' - > execute'以及数组参数 – Ghost

+0

我只是想说... double bind是不行的。 – Rasclatt

回答

1

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

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(); 
} 
?> 

这是生成的表单(更容易与压痕读取)

<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> 

编码快乐!

+0

@Marco - 你需要更多帮助吗?如果这个答案对你有帮助并回答你的问题,请不要忘记[接受那个答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) 。 另请参阅[回答“接受”时的含义是什么?](http://meta.stackexchange.com/help/accepted-answer)和[为什么投票重要?](http://meta.stackexchange .COM /帮助/为什么票)。 – Kuya