doulu6314 2016-07-27 02:15
浏览 45
已采纳

MySQL只返回一行(最近一行)

I've got an issue, basically I'm making a ticket system just for fun and I'm running into a few problems, most of which I've been able to fix with a quick google search or just by messing around with it for a little bit.

I'm not able to solve this issue though, basically when you click on your specific ticket you have open it brings you to a link like that looks like this: 158.xx.xxx.xxx/site/support?view=ID (35, 36, 37). When viewing the page it does display the ticket information but the same information on all three tickets.

  $stmt3 = $auth_user->runQuery("SELECT * FROM ticket"); $stmt3->execute();

  if(isset($_GET['view'])){ 
if($stmt3->rowCount()){
  while($r = $stmt3->fetch(PDO::FETCH_OBJ)) {
    $name = $r->name;
    $id3 = $r->id;
    $subject = $r->subject;
    $ticket = $r->ticket_date;
    $desc = $r->body;
    $ticid = $r->ticket_id;
  }
}

if($_GET['view'] == $id3){

echo 
'
<div class="ticket">
    <div class="ticket-date">
        '.$ticket.'
    </div>
    <div class="ticket-name">
        '.$name.'
    </div>
    <div class="ticket-desc">
        '.$desc.'
    </div>
</div>
';

$displayticket = $auth_user->runQuery("SELECT * FROM ticket_replies WHERE ticket_id=:ticid");
$displayticket->execute(array(':ticid'=>$user_id));
$ticketsrow = $displayticket->fetchAll();
$count = count($ticketsrow);
foreach($ticketsrow as $row9){
  echo 
  "
  <br />
  <div class='ticket'>
    <div class='ticket-date'>
    ".$row9['timestamp']."
    </div>
    <div class='ticket-name'>
    ".$row9['uid']."
    </div>
    <div class='ticket-desc'>
    ".$row9['text']."
    </div>
  </div> 

  <br />";
}

echo '
<form method="POST" action="support?view='.$id3.'">
  <textarea id="text" name="addsupportbody"></textarea><br/>
  <input type="submit" name="addsupportcomment" class="btn btn-dark" style="margin-top: 5px;" value="Add Comment">
  <input type="submit" name="closeticket" class="btn btn-danger" value="Close Ticket">
</form>';

if(isset($_POST['addsupportcomment'])){
    $ticketid = $id3;
    $uidc = $user_id;
    $ttext = $_POST['addsupportbody'];

    if($ttext == ""){
        echo "You must enter a comment to send.";
    }else{

      try
      {

        if($auth_user->insertTicketComment($ticketid, $uidc, $ttext)){
            echo "Your comment has been added!";
            header("url=index");
        }
      }
      catch(PDOException $e)
      {
        echo $e->getMessage();
      }

    }
}

}else{
  echo "This page does not exist.";
} }

Speak with what you will, I do not consider this code safe for publicizing or use, nor do I consider it good or organized. I'm simply trying to learn from my own mistakes and hopefully receive a little help on the way. If there is some missing code just let me know and I'll include it. This block of code is what isn't working correctly for me.

  • 写回答

1条回答 默认 最新

  • dongzhansong5785 2016-07-27 02:32
    关注

    Select just the ticket you want to display. Change

    $stmt3 = $auth_user->runQuery("SELECT * FROM ticket"); 
    

    To

    $id = isset($_GET['view'])? (int)$_GET['view']: -1;
    $stmt3 = $auth_user->runQuery("SELECT * FROM ticket WHERE id = $id"); 
    

    The problem with your code is that you're selecting all the tickets, then you're looping through them:

    while($r = $stmt3->fetch(PDO::FETCH_OBJ))
    

    In each iteration of the loop, you capture the data in the row. No matter what ticket the user wants, the variables in the loop always end up with the last row's values. Instead, you should only fetch the ticket you're interested in as I did earlier. If a match is found, that's the ticket! No need to loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动