doutuo1939 2014-05-28 22:24
浏览 48
已采纳

PHP MySQLi Query使用有效语法返回null

Ok so,

I have been trying to make a ticket system for my wxPanel in order to provide basic support for the application. Although I am easily able to make a database record with the provided code:

PHP:

if (isset($_POST['submit'])) {
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $date = date('D M H:i');

    $subject = mysqli_real_escape_string($subject);
    $message = mysqli_real_escape_string($message);
    $ticket_id = 'TICK_'.rand(00000,99999);

    if (strlen($subject) === 0) {
        echo "Subject Invalid.";
    } elseif (strlen($message) === 0) {
        echo "Message Invalid.";
    } else {
        mysqli_query("INSERT INTO tickets VALUES(
        NULL,
        '".$ticket_id."',
        '".$_SESSION['user']."',
        '".$subject."',
        '1',
        '".$date."',
        '".$message."'
        )");
    }
    header('Location: /view-ticket?identifier='.$ticket_id);
}

Works fine... Then there is this, which is ment to fetch the ticket records and display the titles one by one:

PHP:

$query = mysqli_query("SELECT `subject`,`ticket_id` FROM tickets WHERE `username` = '".$_SESSION['user']."'");

while ($row = mysqli_fetch_assoc($query)) {
    $tickets = $row['subject'];
    $id = $row['ticket_id'];
}

foreach ($tickets as $ticket) {
    echo '
    <a href="view-ticket?identifier='.$id.'"><h2>'.$ticket.'</h2></a>
    ';
}

This always returns NULL. And also none of this works either:

if (isset($_GET['identifier']) === false || empty($_GET['identifier']) === true) {
    header('Location: /tickets');
    exit();
}

$id = mysqli_real_escape_string($_GET['identifier']);

$query = mysqli_query("SELECT `ticket_id`,`message`,`timestamp`,`status` FROM tickets WHERE `ticket_id` = '".$id."'");

while($row = mysqli_fetch_assoc($query)) {
    $ticket_id = $row['ticket_id'];
    $message = $row['message'];
    $timestamp = $row['timestamp'];
    $status = $row['status'];
}

foreach($message as $msg) {
    echo '
    <div class="ticket-message">
        <h2>'.$message.'</h2>
    </div>';
}

Thank you in advance!

p.s. Some of my code may be messy. Advice is always appreciated :)

  • 写回答

1条回答 默认 最新

  • douxiong4250 2014-05-28 23:45
    关注

    Once you get the SELECT query working you are also going to have to look at the code that processes the results.

    If ticket_id identifies a unique row

    $query = mysqli_query($con, "SELECT ticket_id,message,timestamp,status 
                                 FROM tickets WHERE ticket_id = '$id'");
    
    
    $row = mysqli_fetch_assoc($query);
    $ticket_id = $row['ticket_id'];
    $message   = $row['message'];
    $timestamp = $row['timestamp'];
    $status    = $row['status'];
    
    echo '<div class="ticket-message"><h2>'.$message.'</h2></div>';
    

    If ticket_id does not identify a unique row

    $query = mysqli_query($con, "SELECT ticket_id,message,timestamp,status 
                                 FROM tickets WHERE ticket_id = '$id'");
    
    // initialise the arrays that hold multiple row results
    $ticket_id[] = array();
    $message[]   = array();
    $timestamp[] = array();
    $status[]    = array();
    
    while($row = mysqli_fetch_assoc($query)) {
       $ticket_id[] = $row['ticket_id'];
       $message[]   = $row['message'];
       $timestamp[] = $row['timestamp'];
       $status[]    = $row['status'];
    }
    
    foreach($message as $msg) {
       echo '<div class="ticket-message"><h2>'.$msg.'</h2></div>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)