简体   繁体   English

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

[英]MySQL returning only one row (most recent one)

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). 我无法解决这个问题,基本上当你点击你打开的特定票证时,它会带你到这样的链接: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. 这段代码对我来说是不正常的。

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. 无需循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM