我在我的代码中使用if($ stmt-> errorCode()== 0){有一点问题。如果我使用数据库中的票号进行搜索,它将显示它,但如果我使用不在数据库中的随机数进行搜索,则不会显示错误消息,也没有找到票证。
<?php
require("db.php");
$error_message="";
if (isset($_POST['submit'])){
if(empty($_POST['term']))
{
$error_message="Please enter a Ticket Number.";
}
else
{
$query = "SELECT department, subject, message FROM supporttickets Where ticketnumber LIKE :term";
$stmt = $db->prepare($query);
$stmt->execute(array(':term' => $_POST['term']));
if($stmt->errorCode() == 0) {
while (list($department,$subject,$message) = $stmt->fetch(PDO::FETCH_NUM)) {
echo htmlentities($department);
}
}else{
$error_message="no ticket found.";
}
}
}
?>
答案 0 :(得分:0)
你的问题是你实际上并没有echo
输出。这只是指定一个变量:
$error_message="no ticket found.";
你想要的是:
$error_message="no ticket found.";
echo $error_message;
或者只是:
echo "no ticket found.";
答案 1 :(得分:0)
此处不需要错误代码
<?php
require("db.php");
$data = array();
if(!empty($_GET['term']))
{
$query = "SELECT department, subject, message FROM supporttickets
WHERE ticketnumber = ?";
$stmt = $db->prepare($query);
$stmt->execute(array($_GET['term']));
$data = $stmt->fetchAll();
}
foreach ($data as $row)
{
echo htmlspecialchars($row['department']);
}
if (!$data)
{
echo "Please enter valid Ticket Number.";
}
一些错误也得到纠正。