-1

I am not able to show the data from DB to page and getting

Notice: Undefined variable: id

This is my code

<?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>
0 ) { while ( $row4 = mysqli_fetch_array( $sql ) ) { ?>
        <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>

Can any one tell me what wrong thing I am doing?

i have updated the code and added the $id = -1; as you told me the error is gone but started getting this error

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64\www\site\admin\post.php on line 41

the line which is -

if ( mysqli_num_rows( $sql ) > 0 ) {
                while ( $row4 = mysqli_fetch_array( $sql ) ) {
                    ?>

you can see it in the code also please let me know why the id is creating the issue

s singh
  • 7
  • 1

2 Answers2

5

Your all code below if{} is related to $id which is inside if{} and will be executed in any case so put } closing bracket at the end so that the code below if{} will be under if{} and only be executed if your $id is set

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';
           }
 ?> 
Zain Farooq
  • 2,846
  • 3
  • 17
  • 35
1

Well, give an initial value for the id. It will cover the scenario where no id is passed which will prevent from undefined variable error

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

Also you should make sure the $row is used only when there is a record so change the rest of the code as follows

$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 } ?>
Rinsad Ahmed
  • 1,777
  • 1
  • 4
  • 19