从数据库中选择数据的问题

时间:2018-02-13 11:03:26

标签: php mysql mysqli

我正在使用此代码在编辑页面上显示用户已经评论它的文本,我想让他们编辑它们,如果他们想要但我有一个问题是使用mysqli_num_rows检查它们是否结果我没有得到结果来自数据库。为什么会发生这种情况我是从错误的方式选择数据库

    $editsql = "SELECT topics.topic_id, topics.topic_subject, topics.topic_by, posts.post_id, posts.post_topic,
posts.post_content
FROM topics, posts WHERE posts.post_topic = ".mysqli_real_escape_string($conn,$_GET['id'])." ";
$editresult = mysqli_query($conn,$editsql);
if(mysqli_num_rows($editresult) == 0)
  {
      echo 'something went wrong please try again later';
  }
      while ($editrow= mysqli_fetch_assoc($editresult)) {
  $topic_by = $editrow['topic_by'];
  if ($_SESSION['id'] = $topic_by) {
  echo '<form method="post" action="'.edit_function($conn).'" class="textareacon bottom">
      <textarea name="edit-content">'.$editrow['post_content'].'</textarea>
      <input type="submit" value="Submit reply" name="edit-post" class="topicsubmit"/>
  </form>';
}else {
  echo "sorry look like you've been redirected to the wrong topic please head back and try again";
}



}

2 个答案:

答案 0 :(得分:0)

当没有结果时,你试图得到你的结果。请更改您的代码,然后尝试。我还在=

中将==更改为$_SESSION['id'] = $topic_by
$editsql = "SELECT topics.topic_id, topics.topic_subject, topics.topic_by, posts.post_id, posts.post_topic, posts.post_content FROM topics, posts WHERE posts.post_topic = ".mysqli_real_escape_string($conn,$_GET['id'])." ";

$editresult = mysqli_query($conn,$editsql);

if(mysqli_num_rows($editresult) == 0) {
    echo 'something went wrong please try again later';
} else {
      while ($editrow= mysqli_fetch_assoc($editresult)) {
          $topic_by = $editrow['topic_by'];
          if ($_SESSION['id'] == $topic_by) {
              echo '<form method="post" action="'.edit_function($conn).'" class="textareacon bottom"><textarea name="edit-content">'.$editrow['post_content'].'</textarea><input type="submit" value="Submit reply" name="edit-post" class="topicsubmit"/></form>';
          } else {
              echo "sorry look like you've been redirected to the wrong topic please head back and try again";
          }
      }
}
  

一个个人注释:请尝试使用PDO和/或prepared statements

答案 1 :(得分:0)

我发现问题看起来像是在我的主题页面

  while($row = mysqli_fetch_assoc($result))
  {
  if ($_SESSION['id'] = $post_by) {
      echo '<br /><a class="edit_topic" href="edit-topic.php?id='.$row['id'].' ">Edit</a><br /><br />';
  }
}

我在数据库中使用了一行,我应该使用来自url

的get id
  if ($_SESSION['id'] = $post_by) {
      $get_id = mysqli_real_escape_string($conn,$_GET['id']);
      echo '<br /><a class="edit_topic" href="edit-topic.php?id='.$get_id.' ">Edit</a><br /><br />';
  }

感谢您的帮助解决了我的问题