我正在使用此代码根据FORM
的结果在数据库中轻松搜索class Data {
public $vin = null;
public function __construct( $data = array() ) {
if( isset( $data['vin'] ) ) $this->vin = stripslashes( strip_tags( $data['vin'] ) );
}
public function storeFormValues( $params ) {
$this->__construct( $params );
}
public function fetchByVin() {
$success = false;
try{
$con = new PDO( DB_HOST, DB_USER, DB_PASS );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM evidence_vin WHERE vin = :vin LIMIT 1";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
$stmt->execute();
$valid = $stmt->fetchColumn();
if( $valid ) {
$success = true;
}
echo "<table>";
echo "<th>Progress</th>";
echo "<th>Claim number</th>";
echo "<th>Make</th>";
echo "<th>Status</th>";
echo "<th>View</th>";
while ($row = $stmt->fetch()){
echo "<tr>";
echo "<td>24</td>";
echo "<td>".$row['claim_number']."</td>";
echo "<td>".$row['licence']."</td>";
echo "<td>".$row['country']."</td>";
echo "<td>".$row['vin']."</td>";
echo "<td><a href=\"detail.php?id=".$row["id"]."&action=detail\">detail</td>";
echo "</tr>";
}
}catch(PDOExeption $e){
$e->getMessage();
}
echo "</table>";
return $success;
}
我的表单如下:
<form method="post" action="">
<p class="input_description">
Search by VIN
</p>
<p class="input_field">
<input type="text" maxlength="30" required autofocus name="vin">
</p>
<input type="submit" value="Search" name="send">
</div>
</form>
<?php
$data = new Data;
$usr->storeFormValues( $_POST );
if( $usr->fetchByVin() ) {
echo "Prebehne presmerovanie....";
} else {
echo "VIN nebolo nájdene";
}
}
}
else {
echo "Prosím prihláste sa <a href='login.php'> TU </a>";
}
?>
有人可以告诉我我做错了什么以及为什么它只是显示我的空白表而没有值显示我搜索vin的事实在数据库中是真的吗?
答案 0 :(得分:0)
您在}
课程的文件末尾错过了Data
。
您应该始终在文件的开头添加:
error_reporting(E_ALL);
ini_set('display_errors','1');
显示所有错误。
修改强>
其他可能的错误:
在PDO中未选择数据库
和
$data = new Data;
$usr->storeFormValues( $_POST );
...
此处未定义 $usr
,因为对象为$data
当然这个PHP代码:
<?php
$data = new Data;
$usr->storeFormValues( $_POST );
if( $usr->fetchByVin() ) {
echo "Prebehne presmerovanie....";
} else {
echo "VIN nebolo nájdene";
}
}
}
else {
echo "Prosím prihláste sa <a href='login.php'> TU </a>";
}
?>
无效(关闭}
太多)
<强> EDIT2 强>
请查看以下工作代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
define('DB_HOST', 'mysql:host=localhost;dbname=testdb');
define('DB_USER', 'root');
define('DB_PASS', '');
class Data
{
public $vin = null;
public function __construct($data = array())
{
if (isset($data['vin'])) {
$this->vin = stripslashes(strip_tags($data['vin']));
}
}
public function storeFormValues($params)
{
$this->__construct($params);
}
public function fetchByVin()
{
$success = false;
try {
$con = new PDO(DB_HOST, DB_USER, DB_PASS);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM evidence_vin WHERE vin = :vin LIMIT 1";
$stmt = $con->prepare($sql);
$stmt->bindValue("vin", $this->vin, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch();
if ($row === false) {
return false;
}
$success = true;
echo "<table>";
echo "<th>Progress</th>";
echo "<th>Claim number</th>";
echo "<th>Make</th>";
echo "<th>Status</th>";
echo "<th>View</th>";
echo "<tr>";
echo "<td>24</td>";
echo "<td>" . $row['claim_number'] . "</td>";
echo "<td>" . $row['licence'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['vin'] . "</td>";
echo "<td><a href=\"detail.php?id=" . $row["id"] . "&action=detail\">detail</td>";
echo "</tr>";
} catch (PDOExeption $e) {
$e->getMessage();
}
echo "</table>";
return $success;
}
}
?>
<form method="post" action="">
<p class="input_description">
Search by VIN
</p>
<p class="input_field">
<input type="text" maxlength="30" required autofocus name="vin">
</p>
<input type="submit" value="Search" name="send">
</div>
</form>
<?php
if (!empty($_POST)) {
$data = new Data;
$data->storeFormValues($_POST);
if ($data->fetchByVin()) {
echo "Vin found - details above";
} else {
echo "VIN not found";
}
}
?>
它会删除所有错误以及fetchColumn
方法的使用。这段代码很好用。然而,它仍然不是按功能回显数据并返回bool的最佳方式。如果数据不同于false,您应该返回数据并检入主文件。