2015-04-12 51 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