0

我在让我的代码中的 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.";
 }

 }
 }

?> 
4

2 回答 2

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.";
}

也纠正了几个错误。

于 2013-07-16T14:11:14.450 回答
0

你的问题是你实际上并没有echo输出。这只是分配一个变量:

$error_message="no ticket found.";

你想要的是:

$error_message="no ticket found.";
echo $error_message;

或者也许只是:

echo "no ticket found.";
于 2013-07-16T13:59:13.990 回答