在PHP页面中显示数据

在PHP页面中显示数据,php,Php,我无法将数据从数据库显示到页面 得到 注意:未定义的变量:id 这是我的密码 <?php if (isset( $_GET[ 'id' ] ) ) { $id = $_GET[ 'id' ]; } $query2 ="select * from video where vid='$id'"; $sql2 = mysqli_query( $con, $query

我无法将数据从数据库显示到页面 得到

注意:未定义的变量:id

这是我的密码

<?php

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

            $query2 ="select * from video where vid='$id'";
            $sql2 = mysqli_query( $con, $query2 );
            $row2 = mysqli_fetch_assoc( $sql2 );

            ?>

            <iframe src="https://www.youtube.com/embed/<?php echo $row2['link'];?>" height="300" width="400" scrolling="no" frameborder="0"></iframe>
            <h1>
                <?php echo $row2['title'];?>
            </h1>
            <p>
                <?php echo $row2['description'];?>
            </p>

你可以在代码中看到它,也请让我知道为什么id会产生这个问题

如果{}下面的所有代码都与
$id
相关,它在
如果{}
里面,并且在任何情况下都会被执行,所以在末尾放上
}
结束括号,这样
如果{}
下面的代码就会在
如果{}下面
并且仅当设置了
$id
时才会执行

if (isset( $_GET[ 'id' ] ) ) {
            $id = $_GET[ 'id' ];
        //removing bracket from here

        $query2 ="select * from video where vid='$id'";
        $sql2 = mysqli_query( $con, $query2 );
        $row2 = mysqli_fetch_assoc( $sql2 );

        ?>

        <iframe src="https://www.youtube.com/embed/<?php echo $row2['link'];?>" height="300" width="400" scrolling="no" frameborder="0"></iframe>
        <h1>
            <?php echo $row2['title'];?>
        </h1>
        <p>
            <?php echo $row2['description'];?>
        </p>
<?php } //adding bracket here
       else
           {
             echo 'id is not set';
           }
 ?> 
if(isset($\u GET['id'])){
$id=$_GET['id'];
//从此处卸下支架
$query2=“从视频中选择*,其中vid=”$id';
$sql2=mysqli\u查询($con,$query2);
$row2=mysqli\u fetch\u assoc($sql2);
?>
        <div class="comment">
            <p>
                <?php echo $row4['comment'];?>
            </p>
            <p>
                <?php echo "From ". $row4['uid'];?>
            </p>

        </div>
        <?php
        }

        }

        if ( isset( $_POST[ 'submit' ] ) ) {
            if ( empty( $_POST[ 'email' ] ) || empty( $_POST[ 'comment' ] ) ) {

                $erro = "<p class='text-danger'> Fill the Required Fields</p>";


            } else {

                $Commentquery = "insert into comments (vid,uid,status,comments,datetime) values ('$id','$_POST[email]', 'pending','$_POST[comment]',now())";

                if ( mysqli_query( $con, $Commentquery ) ) {

                    $sucess = "<p class='text-sucess'> Thankyou</p>";

                }
            }
        }
        ?>

        <div class="comment_form">
            <h2> Comment on post</h2>

            <?php
            if ( isset( $err ) ) {
                echo $err;
            }
            if ( isset( $sucess ) ) {
                echo $sucess;
            }

            ?>

            <form method="post" action="">
                <div class="form-group">
                    <lable> Email Address</lable>
                    <input type="email" name="email" class="form-control"/>
                </div>
                <div class="form-group">
                    <lable> Comment</lable>
                    <textarea name="comment" class="form-control" cols="40" rows="4"></textarea>
                </div>
                <div class="form-group">
                    <input type="submit" name="submit" class="btn btn-default"/>
                </div>


            </form>

        </div>


好吧,给id一个初始值。它将涵盖没有传递id的情况,这将防止未定义的变量错误

 $id = -1;
 if (isset( $_GET[ 'id' ] ) ) {
    $id = $_GET[ 'id' ];
 }
此外,您还应该确保只有在有记录时才使用$row,因此请按如下所示更改代码的其余部分

$query2 ="select * from video where vid='$id'";
        $sql2 = mysqli_query( $con, $query2 );
        if($row2 = mysqli_fetch_assoc( $sql2 )){

        ?>

        <iframe src="https://www.youtube.com/embed/<?php echo $row2['link'];?>" height="300" width="400" scrolling="no" frameborder="0"></iframe>
        <h1>
            <?php echo $row2['title'];?>
        </h1>
        <p>
            <?php echo $row2['description'];?>
        </p>

    <?php } ?>
$query2=“从视频中选择*,其中vid='$id';
$sql2=mysqli\u查询($con,$query2);
如果($row2=mysqli\u fetch\u assoc($sql2)){
?>
        <div class="comment">
            <p>
                <?php echo $row4['comment'];?>
            </p>
            <p>
                <?php echo "From ". $row4['uid'];?>
            </p>

        </div>
        <?php
        }

        }

        if ( isset( $_POST[ 'submit' ] ) ) {
            if ( empty( $_POST[ 'email' ] ) || empty( $_POST[ 'comment' ] ) ) {

                $erro = "<p class='text-danger'> Fill the Required Fields</p>";


            } else {

                $Commentquery = "insert into comments (vid,uid,status,comments,datetime) values ('$id','$_POST[email]', 'pending','$_POST[comment]',now())";

                if ( mysqli_query( $con, $Commentquery ) ) {

                    $sucess = "<p class='text-sucess'> Thankyou</p>";

                }
            }
        }
        ?>

        <div class="comment_form">
            <h2> Comment on post</h2>

            <?php
            if ( isset( $err ) ) {
                echo $err;
            }
            if ( isset( $sucess ) ) {
                echo $sucess;
            }

            ?>

            <form method="post" action="">
                <div class="form-group">
                    <lable> Email Address</lable>
                    <input type="email" name="email" class="form-control"/>
                </div>
                <div class="form-group">
                    <lable> Comment</lable>
                    <textarea name="comment" class="form-control" cols="40" rows="4"></textarea>
                </div>
                <div class="form-group">
                    <input type="submit" name="submit" class="btn btn-default"/>
                </div>


            </form>

        </div>


如何通过表单获取
$\u get['id']
$id
if else
块中。将
if else
}
的右括号放在末尾。所有代码都与
$id
相关,Bobby Tables将因您的SQL查询构造错误而以数据库所有权惩罚您。(提示:如果有人请求记录
1');删除表视频;--
?)这可能会导致更深层的错误,因为它将无法找到记录(我假设)与行关联的字段将是未定义的。我添加了$id=-1;错误消失了,但内容仍然没有像视频一样显示,其详细信息也没有显示出来。代码未满。您可以让我知道如何发布完整代码吗?我想我能够更新代码,请立即检查。您应该在浏览时通过index.php?id=1ri收到此警告:mysqli_num_rows()期望参数1为mysqli_result,布尔值在第41行的C:\wamp64\www\site\admin\post.php中给出