【问题标题】:$_GET id from url displays only first record from database PHP mySQL$_GET id from url 仅显示数据库 PHP mySQL 中的第一条记录
【发布时间】:2014-08-16 08:50:10
【问题描述】:

我正在尝试从 url "/view.php?postID=2" 获取 id,代码如下:

$post = "SELECT * FROM news WHERE postID='$postID'";
$post1 = mysql_query($post);
$postV = mysql_fetch_array($post1);
$postID = $postV['postID'];
$postImg = $postV['img'];
$postTitle = $postV['title'];
$postAuthor = $postV['author'];
$postDate = $postV['date'];
$postCategory = $postV['category'];
$postText = $postV['text'];


<?php
if (isset($_GET['postID'])) {
    $newsID = $_GET['postID'];
}

echo "<section class='view_news'>
    <img class='view_newsimg' src='$postImg'>
    <h3 class='lath'>$postTitle</h3>
    <ul class='det'>
        <li class='adc'>avtori: $postAuthor</li>
        <li class='adc'>TariRi: $postDate</li>
        <li class='adc'>kategoria: $postCategory</li>
    </ul>
    <p class='news'>
        $postText
    </p>
</section>";
?>

但它只显示 postID = 1 的数组中的数据

谁能告诉我该怎么做?谢谢:)

【问题讨论】:

标签: php mysql get


【解决方案1】:

这里有三个问题:

  1. 您需要先使用 $_GET 获取 postID 值

    $post = "SELECT * FROM news WHERE postID='{$_GET['$postID']}";

  2. 你应该使用while循环

    while($postV = mysql_fetch_array($post1)) {

    $postID = $postV['postID'];

    $postImg = $postV['img'];

    $postTitle = $postV['title'];

    $postAuthor = $postV['作者'];

    $postDate = $postV['date'];

    $postCategory = $postV['category']; $postText = $postV['text']; }

  3. 您的代码容易受到 SQL 注入的影响,请使用 mysql_real_escape_string() 来防止它。

【讨论】:

  • 这将如何解决它返回错误 ID 的记录的问题?
  • 如果postID是唯一键,他不需要循环。
【解决方案2】:

改变这个:

$post = "SELECT * FROM news WHERE postID='$postID'";

这样:

$post = "SELECT * FROM news WHERE postID='{$_GET['postID']}'";

【讨论】:

  • 或者先设置$postID = $_GET['postID']
【解决方案3】:

你在这里做错了三件事。首先您在设置$postID 之前运行mysql_query(),另一个是您没有使用While 循环。第三件事是你两次启动php&lt;?php。尝试设置 $postID 并使用 while 循环。你的代码应该是这样的

    <?php
$postID="Your Value";
            post = "SELECT * FROM news WHERE postID='$postID'";
            $post1 = mysql_query($post);
    while($postV = mysql_fetch_array($post1){

            $postID = $postV['postID'];
            $postImg = $postV['img'];
            $postTitle = $postV['title'];
            $postAuthor = $postV['author'];
            $postDate = $postV['date'];
            $postCategory = $postV['category'];
            $postText = $postV['text'];
            }


            if (isset($_GET['postID'])) {
                $newsID = $_GET['postID'];
            }

            echo "<section class='view_news'>
                <img class='view_newsimg' src='$postImg'>
                <h3 class='lath'>$postTitle</h3>
                <ul class='det'>
                    <li class='adc'>avtori: $postAuthor</li>
                    <li class='adc'>TariRi: $postDate</li>
                    <li class='adc'>kategoria: $postCategory</li>
                </ul>
                <p class='news'>
                    $postText
                </p>
            </section>";
            ?>

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2012-03-24
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    相关资源
    最近更新 更多