我无法显示从数据库到页面和获取的数据
注意:未定义的变量: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>
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>
任何人都能告诉我我在做什么错事吗?
我更新了代码并添加了$ id = -1;正如你告诉我的错误已经消失但开始出现此错误
警告:mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64\www\site\admin\post.php on line 41
这条线是 -
if ( mysqli_num_rows( $sql ) > 0 ) {
while ( $row4 = mysqli_fetch_array( $sql ) ) {
?>
你可以在代码中看到它也请让我知道为什么id正在创建问题
您在if{}
下方的所有代码都与$id
相关,if{}
位于}
内部并且将在任何情况下执行,因此将if{}
结束括号放在末尾,以便if{}
下面的代码将在$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';
}
?>
被设置时才执行
$id = -1;
if (isset( $_GET[ 'id' ] ) ) {
$id = $_GET[ 'id' ];
}
好吧,给出id的初始值。它将涵盖没有传递id的情况,这将防止未定义的变量错误
$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 } ?>
此外,您应确保仅在存在记录时才使用$行,因此请按如下方式更改其余代码
qazxswpoi