简体   繁体   English

来自MySql的ID值在while循环中丢失

[英]ID value from MySql is lost in while loop

When I post comments using the form field from the loop, the specific ID of the blog is not carried over to the php script. 当我从循环中使用表单字段发布评论时,博客的特定ID不会传递到php脚本中。 So the comment is not assigned to the blog where it is meant to go. 因此,该评论未分配给打算去的博客。

<?php
$conn = mysql_connect("localhost", "ooze", "");
mysql_select_db ("ooze");
$result = mysql_query ("select * from blog") or die(mysql_error());
$result2 = mysql_query ("select * from blog, blogcomment where blog.ID = blogcomment.blogID") or die(mysql_error());
$i = 1;  
while($row = mysql_fetch_array($result))
{
echo "<h1>$row[title]</h1>";
echo "<p class ='second'>$row[blog_content]</p> "; 
echo "<p class='meta'>Posted by .... &nbsp;&bull;&nbsp; $row[date] &nbsp;&bull;&nbsp; <a href='#' onclick=\"toggle_visibility('something$i'); return false\">Comments</a><div id='something$i' style='display: none;'>";    
$i++;
while($row = mysql_fetch_array($result2))
{
echo "<p class='third' >$row[commentdate] &nbsp;&bull;&nbsp; $row[username]</p><p>said:</p> <p>$row[comment]</p>";
}
if ( isset ($_SESSION["gatekeeper"])) {
echo '<form method="post" name="result_'.$row["ID"].'" action="postcomment.php"><input name="ID" type = "hidden" value = "'.$row["ID"].'" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form>';
} 
else { 
echo '<p class="third"><a href="register.html">Signup </a>to post a comment</p>';
}
echo "</div>";
}
mysql_close($conn);
?>

It's because you're overwriting the $row variable with the inner while() loop. 这是因为您正在使用内部while()循环覆盖$ row变量。 Change the inner variable name from $row to $sub and you'll find the behavior working like expected. 将内部变量名称从$ row更改为$ sub,您将发现行为正常。

dont use the same variable ( $row ) for both loops... 不要在两个循环中使用相同的变量( $row )...

change your second loop to look as: 将您的第二个循环更改为:

 while($row2 = mysql_fetch_array($result2)){...}

and change all the values from the second result to $row2["COLUMN_NAME"] 并将所有值从第二个结果更改为$row2["COLUMN_NAME"]

also - don't forget to surround the array index with quotation marks... it works but creates a notice, and makes the server work a little bit harder 还有-不要忘记用引号将数组索引括起来...它可以工作,但会发出通知,并使服务器工作起来更加困难

ie: $row[username] should be $row["username"] 即: $row[username]应该是$row["username"]

this is a try to make a little bit order in your code: 这是尝试在您的代码中进行一些排序:

<?php
$conn = mysql_connect("localhost", "ooze", "");
mysql_select_db ("ooze");
$result = mysql_query ("select * from blog") or die(mysql_error());
$result2 = mysql_query ("select * from blog, blogcomment where blog.ID = blogcomment.blogID") or die(mysql_error());
$i = 1;  
while($row = mysql_fetch_array($result)){
    echo <<<str
    <h1>{$row["title"]}</h1>
    <p class ='second'>{$row["blog_content"]}</p> 
    <p class='meta'>Posted by .... &nbsp;&bull;&nbsp; {$row["date"]} &nbsp;&bull;&nbsp; <a href='#' onclick=\"toggle_visibility('something{$i}'); return false\">Comments</a><div id='something{$i}' style='display: none;'>
str;

    $i++;
    while($comment = mysql_fetch_array($result2)){
         echo "<p class='third' >{$comment["commentdate"]} &nbsp;&bull;&nbsp; {$comment["username"]}</p><p>said:</p> <p>{$comment["comment"]}</p>";
    }
    if ( isset ($_SESSION["gatekeeper"])) {
        echo '<form method="post" name="result_'.$row["ID"].'" action="postcomment.php"><input name="ID" type = "hidden" value = "'.$row["ID"].'" /><input name="comment" id="comment" type="text" style="margin-left:20px;"/><input type="submit" value="Add comment" /></form>';
    } 
    else { 
        echo '<p class="third"><a href="register.html">Signup </a>to post a comment</p>';
    }
    echo "</div>";
}
mysql_close($conn);

after all if i understood right what you were trying to achieve - than your script is not good, the second query should be inside the first loop and contain a WHERE blogID={$row["ID"]} in the end 毕竟,如果我正确理解了您要实现的目标-比您的脚本不好,则第二个查询应该在第一个循环中,并且最后包含WHERE blogID={$row["ID"]}

if you prefer getting all the comments in one query - it is good, but you need to arrange them and then show only the relevant for each "blog", when you fetch them in a nested loop - the second query will only give you rows ones (because in the second time - it will already be empty) 如果您希望在一个查询中获取所有评论-很好,但是当您在嵌套循环中获取它们时,您需要排列它们,然后仅显示每个“博客”的相关内容-第二个查询将只为您提供行一个(因为第二次-它已经为空)

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

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