所有的新年快樂。我需要指出的是,我試圖單獨使用PDO,並且我是使用PDO的相對菜單,所以請原諒這個問題是否顯而易見。php PDO mysql - 行爲查詢
我有一點愚蠢的時刻,因爲我似乎無法理解一些事情,爲什麼一個相對簡單的電子郵件驗證系統,我有(嘗試)寫入不是很正常工作。一切都可以,直到驗證鏈接結束時的php將電子郵件地址設置爲有效。這裏是我的代碼,其次是問題:
首先,我有一個包含數據庫登錄的包含文件。它看起來像這樣:
<?php
// DATABASE SETTINGS
$hostname = "127.0.0.1";
$username = "devProduction";
$password = "ienx3rybcisuc";
$database = "devProduction";
try {
$conn = new PDO("mysql:host=$hostname; dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
// close the database connection (removed as I do this at the end of each call)
//$conn = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
然後在實際接收到的用戶他們點擊發送到他們的電子郵件中的鏈接後,頁面:
<?php
// Grab our includes
include '../conf/Funcs.php';
include '../conf/DBconfig.php'; // (This is the file displayed above)
require_once '../conf/Mobile_Detect.php';
// Check out what device is looking at us
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
$scriptVersion = $detect->getScriptVersion();
// Check to see if we are already logged in under an already validated account
if(isset($_COOKIE['AGMARDTuid']) || isset($_COOKIE['AGMARDTtoken'])) {
logout();
header("Location: ../");
exit;
} else {
$val = base64url_decode($_GET['val']);
$val = explode(":-:", $val);
$uid = $val[0];
$add = $val[1];
$key = $val[2];
// These are the three items that are pulled out of the URL $val value. This works fine
// It's only here to check it's working ok for the moment
echo "uid: ".$uid."<br>add: ".$add."<br>key: ".$key."<br><br>";
// Kill the process if either of the three values - $uid, $add, $key - are empty
if(($uid == "") || ($uid == NULL) || ($add == "") || ($add == NULL) || ($key == "") || ($key == NULL)) {
logout();
header("Location: ../");
exit;
} else {
// Seems everything is in order for email validation, so lets validate
$yes = "yes";
$NULL = NULL;
try {
$stmt = $conn->prepare("UPDATE $database.users SET `emailValidated` = :validate, `emailValidationKey` = :newkey WHERE `uid` = :uid AND `email` = :add AND `emailValidationKey` = :key");
$stmt->bindParam(':uid', $uid);
$stmt->bindparam(':add', $add);
$stmt->bindParam(':key', $key);
$stmt->bindParam(':validate', $yes);
$stmt->bindParam(':newkey', $NULL);
$stmt->execute();
$result = "success";
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); $result = "fail"; }
$conn = null;
echo "result: ".$result." (post sql)<br><br>";
if($result == "fail") {
echo "Email did not successfully validate, there was a problem<br><br>";
echo $conn . "<br>" . $e->getMessage();
} else if($result == "success"){
echo "Email successfully validated<br><br>";
echo $conn . "<br>" . $e->getMessage();
}
echo "<br><br>We got to the end!";
}
}
?>
代碼工作,還挺。問題是,如果數據庫中沒有與URL中傳遞給腳本的所有三個值匹配的帳戶,它仍會顯示爲已更新(驗證)帳戶,即使尚未更新(驗證)該帳戶。爲什麼是這樣?
此外,對於我結合一些參數的部分,特別是這兩個:
$stmt->bindParam(':validate', $yes);
$stmt->bindParam(':newkey', $NULL);
爲什麼我似乎有分配$是=「是」; ?和「$ NULL = NULL;如事先變量我也嘗試:
$stmt->bindParam(':validate', 'yes');
$stmt->bindParam(':newkey', NULL);
和
$stmt->bindParam(':validate', yes);
$stmt->bindParam(':newkey', NULL);
和
$stmt->bindParam(':validate', 'yes');
$stmt->bindParam(':newkey', 'NULL');
均無功而返
答案和信息,並。建議總是歡迎和讚賞。謝謝!
C
您應該使用bindValue代替bindParam當你想直接在綁定傳遞的值。 關於UPDATE:使用$ nbr = $ stmt-> rowCount();獲取查詢所影響的行數。如果== 0,那麼你什麼也沒有更新;) – Spoke44
** Off topic **你不應該用'?>'結束php代碼,除非你在同一個文件中有html。 –
布吉人 - 爲什麼?假設之後沒有html,我應該怎樣終止我的代碼?通常有99%的時間。 – Cassandra