Hello
i am not able to show data from db to page
this is my code
<?php
if(isset($_REQUEST['id'])){
$id=$_REQUEST['id'];
}
$query2="select * from video where vid='$id'";
$sql2=mysqli_query($con,$query2);
$row2=mysqli_fetch_assoc($sql2);
//print_r($row2) ;
?>
<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>
the error is
Notice: Undefined variable: id in C:\wamp64\www\
please tell me whats wrong here
thanks
What is the value of the $id
variable if $_REQUEST['id']
is not set?
I think you may not be understanding the error message.
Because it displays “id” and not “$id” it could be easy to think it’s referring to a database field “id”. But in this case the clue is “variable: id” - the “$” is implied. So the error message is not about a database field, but about the “$id” variable.
Try temporarily changing the code by addiing a few var_dump lines like so:
<?php
if(isset($_REQUEST['id'])){
$id=$_REQUEST['id'];
var_dump($id); // testing only
// }
} else {
echo "REQUEST[id] is not set";
}
var_dump($id); // end of testing lines
.....
REQUEST[id] is not set
( ! ) Notice: Undefined variable: id in C:\wamp64\www\site\admin\post.php on line 26
Call Stack
# Time Memory Function Location
1 0.0019 403016 {main}( ) …\post.php : 0
C:\wamp64\www\site\admin\post.php:26:null
( ! ) Notice: Undefined variable: id in C:\wamp64\www\site\admin\post.php on line 28
Call Stack
# Time Memory Function Location
1 0.0019 403016 {main}( ) …\post.php : 0
So the problem is that $_REQUEST['id']
isn’t making it to the file for some reason. You have a choice depending on what you want to happen when this happens. You could assign a default value to the $id
variable, not run a query and not display the <iframe><h1><p>
bit, display an error message, show the form again, etc.
Sorry, I don’t know what you’re asking. Something about what I posted you don’t understand?
Perhaps it’s because I used “assign”? Technically “define” and “assign” are different, but typically a variable is assigned a value at the same time it is defined. eg.
$var_foo = 1; // foo variable defined and assigned a value
$var_bar = $var_foo; // bar variable defined, no error
$var_bar = $var_baz; // baz variable is not defined and has no value, error
worked further on it and getting
Notice: Undefined variable: id in C:\wamp64\www\site\admin\post.php on line 36
Call Stack
# Time Memory Function Location
1 0.0025 415896 {main}( ) …\post.php : 0
( ! ) Fatal error: Uncaught Error: Call to undefined function mysql_num_rows() in C:\wamp64\www\site\admin\post.php on line 38
( ! ) Error: Call to undefined function mysql_num_rows() in C:\wamp64\www\site\admin\post.php on line 38
Call Stack
# Time Memory Function Location
1 0.0025 415896 {main}( ) …\post.php : 0
What is on lines 36 and 38 of your code? You didn’t post that many lines originally. As line 36 is the same error message you were reporting initially, it’s likely to be the same problem.
The next batch are referring to the function mysql_num_rows()
which is not part of the current version of PHP (all of the old mysql_
calls have been removed, in favour of mysqli or PDO). As you use mysqli to run the query and fetch the results, you should use the mysqli equivalent of that function to get the row count.
ok
but the main issue is with the id thing which is not being process and not able to understand what to put against it
the main thing is that i need to show the DB things in the page with the comment form bu the posted video/ content is not coming issue main id which is above mention what should i do with the id issue…
and as for the code this is the full code
<?php
if(isset($_REQUEST['id'])){
$id=$_REQUEST['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>
<?php
$query4="select * from comments where vid='$id' And Status='approved' order by id DESEC";
$sql=mysqli_query($con,$query4);
if(mysqli_num_rows($sql)>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>
The error on line 38 (the num_rows function) is caused by the fact that the query doesn’t execute, which in turn is caused by the fact that $id
doesn’t exist. The error will have changed now that you’ve changed it from mysql_num_rows()
to mysqli_num_rows()
, probably something about a Boolean.
As @mittineague said earlier, you only set a value for $id
if the $_REQUEST
variable is set. You also need to deal with the situation if it is not set, otherwise you will see errors. The code needs to exit gracefully if it is called without that variable.
How do you call this page?
yes the error changed
How do you call this page?- did not get this
How is it opened? Is it from a link in another page? What is the code for that link?
yes the page will open with the link but currently its locally
i am creating a post page where the posted video will show up and person can comment on them
When you say locally, do you mean you open it in the browser just by typing the address into the address bar? Exactly what do you type in there?
this i did not get what to do
So where in that link is the $_REQUEST
variable going to come from?
What happens if you do
http://localhost/site/admin/post.php?id=1
as you will no doubt be doing in your link?
You’ll still need to deal gracefully with it being called incorrectly, though.