將我所有的PHP轉換爲PDO。有一個發送contractor_id和passwd的表單。然後搜索數據庫,如果發現它顯示該記錄中的選定字段。應該是非常簡單的,但無論是我錯過了什麼,或者我有一個類型 - 我不能看到。預先感謝您的幫助。PDO SELECT * if id and password then DISPLAY record
<html >
<body>
<form action="display.php" method="POST" >
<input name="contractor_id" type="text" />
<input name="passwd" type="text" />
<input name="submit" type="submit" value="Submit" />
</form>
</body>
</html>
<?php
$servername = "localhost";
$username = "searchdb";
$password = "passwd";
$dbname = "dbn";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT contractor_id, firstname, lastname, address, city, state, zip, email, areacode, phonenumber, ssnumber,
rate, mykey, passwd FROM contractor_keys");
$stmt->execute();
$stmt = $conn->prepare("SELECT * FROM contractor_keys WHERE contractor_id=:contractor_id and passwd=:passwd");
$stmt->bindValue(1, $contractor_id, PDO::PARAM_INT);
$stmt->bindValue(2, $firstname, PDO::PARAM_STR);
$stmt->bindValue(3, $lastname, PDO::PARAM_STR);
$stmt->bindValue(4, $address, PDO::PARAM_STR);
$stmt->bindValue(5, $city, PDO::PARAM_STR);
$stmt->bindValue(6, $state, PDO::PARAM_STR);
$stmt->bindValue(7, $zip, PDO::PARAM_STR);
$stmt->bindValue(8, $email, PDO::PARAM_STR);
$stmt->bindValue(9, $areacode, PDO::PARAM_STR);
$stmt->bindValue(10, $phonenumber, PDO::PARAM_STR);
$stmt->bindValue(11, $ssnumber, PDO::PARAM_STR);
$stmt->bindValue(12, $rate, PDO::PARAM_STR);
$stmt->bindValue(13, $mykey, PDO::PARAM_STR);
$stmt->bindValue(14, $passwd, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo $row['firstname'].' '.$row['lastname'].' '.$row['address'] .' '.$row['city'].' '.$row['state']
.' '.$row['zip'].' '.$row['areacode'].' '.$row['phonenumber'].' '.$row['ssnumber'].' '.$row['rate']
.' '.$row['mykey'].' '.$row['passwd'];
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
你的問題是什麼? –
你有兩個佔位符,':contractor_id'和':passwd' - 但是你綁定了14個值?我不認爲你明白['bindValue();'](http://php.net/manual/en/pdostatement.bindvalue.php)是什麼? – Qirel
對不起,我把那部分拿出來了。當我運行它時,我只是得到一個沒有結果的白色屏幕,沒有錯誤 –