php error - Undefined index: id - php

I have got an error message in php like this "undefined index id in line no:", I have using the following code
$id='$_REQUEST[id]';
$sql = "SELECT * FROM country";
$result = $con->query($sql);
$i=1;
foreach($result as $row)
{
?>
<li><?php echo $row['name'] ?> </li>
<?php
$i++;
}
?>
For listing the following code i have used
<?php
if(isset($id)) {
$queryImg = "SELECT * FROM data WHERE country='$_REQUEST[id]'";
$resultImg = mysqli_query($con,$queryImg);
$rowResult = mysqli_num_rows($resultImg);
while($rowsImg = mysqli_fetch_array($resultImg)){ ?>
I do not have enough experience in php, so can you please check this code and tell me how to solve this.

remove single quotes like $id=$_REQUEST['id'];
and here $queryImg = "SELECT * FROM data WHERE country='".$_REQUEST['id']."'";

Just remove the quote from the variable $id, like this:
$id=$_REQUEST['id'];

Related

How can I add an Sql Query in the middle of HTML code?

I want to make a login system, where the user can see all the messages he has received. In my database the insurance_company is the username and that is what I want it to show by. Here is my code. How do I fix it?
- Thanks Prem
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Claim'] . "<br />";
}
?>
PHP code can be written anywhere in HTML code. You need to just place your PHP code between tags. As you can see in the example bellow I have used php code between p tags.
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {?>
<p> <?php echo $row['Claim'] . "<br />"; ?> <p>
<?php }
?>
If you want it to show insurance_company, you must echo this value in the response array:
echo $row['insurance_company']

MULTI SEARCH PHP

Can anyone help me to figure out the error in my code? i want to display data through two input search..my code goes like these.
<table>
<tr>
<td>Studid</td>
<td>Course</td>
</tr>
<?php
include ("connect.php");
if(isset($_POST['submit']))
{
$studno=$_POST['idsearch'];
$scourse=$_POST['coursesearch'];
$sql=mysql_query("SELECT * FROM cfnr WHERE studid= ".$studno." AND course=".$scourse."");
}
?>
<?php
while($row=mysql_fetch_array($sql))
{
}
?>
<tr>
<td><?php echo $row['studid'];?></td>
<td><?php echo $row['course'];?></td>
</tr>
</table>
i got this error in my screen.
"mysql_fetch_array() expects parameter 1 to be resource, boolean given in "
thanks! :)
You should use a bit of debugging. Try this:
$sql = "SELECT * FROM cfnr WHERE studid= '".$studno."' AND course = '".$scourse."'";
$query = mysql_query($sql) or die(__LINE__."has an error: ".mysql_error()); // Gives an error if there's a syntax error
$row = mysql_fetch_array($query);
echo "<pre>";
print_r($row);
exit;
while ($row = mysql_fetch_array($query)) {
/* Other code */
}
Replace your sql with below line :
$sql=mysql_query("SELECT * FROM cfnr WHERE studid= ".$studno." AND course=".$scourse."") or die(mysql_error());
The issue is your `$sql variable is within the if condition and if your if condition fails there is no scope of $sql outside of if condition
Instead of
while($row=mysql_fetch_array($sql))
Use
while($row=#mysql_fetch_array($sql))
you will not get this error.
("SELECT * FROM cfnr WHERE studid= '$studno' AND course ='$scourse.'") or die(mysql_error());
Change the code :
while($row=mysql_fetch_array($sql))
to
while($row=mysql_fetch_assoc($sql))
and also change this code:
$sql=mysql_query("SELECT * FROM cfnr WHERE studid= ".$studno." AND course=".$scourse."");
to
$sql=mysql_query("SELECT * FROM cfnr WHERE studid= '$studno' AND course='$scourse'");
Not only that, the close brace of the isset() should be on before your (?>) last closing php tag.
Change the code :
include ("connect.php");
to
include 'connect.php';

Using PHP while loop on a dynamic page won't work

I am trying to show it all with while loop on a dynamic page, but it wont show anything at all..
if (isset($_GET['id'])) {
$genre = $_GET['id'];
$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();
}else{
$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}
<html>
<body>
<ul>
<?php while ( $row = $result->fetch_assoc() ): ?>
<li><?=$row['genre'];?></li>
<?php endwhile; ?>
</ul>
</body>
</html>
Anyone know why it wont show anything on the dynamic page, but if the page is not dynamic it works fine :)
Thanks.
If dynamic page means that your sending an id in Url an you are not getting result.
I think that the problem is that you are using $row = $result->fetch_assoc(); in if condition and also in while.
Please use fetching in one place. Better be in while loop.
Hope it helps.
I figured it out
if (isset($_GET['id'])) {
$genre = $_GET['id'];
$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}
This it how it needs to look when its a dynamic page :)
well i think it is, it works now.
please echo the result ...
<li><?php echo $row['genre'];?></li>
And in href, it is only genre OR genre.php
In the first query..
$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();// this line not required
comment this line
$row = $result->fetch_assoc();
It looks like you haven't enclosed the first part of your PHP code in <?php ?> tags

Please i need some help php SELECT FROM WHERE

My problem is when i want to display someone's information from database using his Username:
i get this error this is Notice: Undefined variable: fname in /opt/lampp/htdocs/aa/hhh.php on line 27 . .
<?php
include 'con_to_db.php';
$result = mysqli_query($dbcon, "SELECT * FROM Members WHERE Username='youba'" );
while($row = mysqli_fetch_array($result))
{
$fname = $row['Username'];
$age = $row['Age'];
}
?>
<html>
<body>
<h1> this is <?php echo $fname?> </h1>
</body>
</html>
but when i use the id i get the information from database without any problems:
<?php
include 'con_to_db.php';
$result = mysqli_query($dbcon, "SELECT * FROM Members WHERE id='70'" );
while($row = mysqli_fetch_array($result))
{
$fname = $row['Username'];
$age = $row['Age'];
}
?>
<html>
<body>
<h1> this is <?php echo $fname?> </h1>
</body>
</html>
First of all, it's not an error but a notice. However, this particular notice does hint at an error in the code, though not a syntax error, but a logical one: the body of your while loop does not get executed - i.e. you forgot to check if you get any result in the first place. (Also, if you would get more than one result, only the last one would be displayed).
Please check your database to see why the username is not there, though you obviously expect it to be.
Also, I assume (hope!) that your id field is numeric in the database, in which case you should not write quotes:
$result = mysqli_query($dbcon, "SELECT * FROM Members WHERE id=70" );

MySQL data does not appear in tooltip

I want to show MySQL data inside a tooltip.
Here is the code:
<?php
$abfrage = "SELECT * FROM tester";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis)){
$Name=$row->Name;
$Bech=$row->Beschreibung;
}
?>
Test Link
It shows me an empty tooltip with nothing inside. If I print those two variables, the MySQL data appear. Is there any mistake in the code?
Thanks.
This is in conjunction with Seth McClaine's answer.
Echo your values using:
<?php echo $Name; ?> <?php echo $Bech; ?>
Instead of <?=$Name?> <?=$Bech?>
The use of short tags is not recommended for something like this.
Reformatted code:
<?php
$abfrage = "SELECT * FROM tester";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);
$Name=$row->Name;
$Bech=$row->Beschreibung;
?>
Test Link
If you just want the first result remove the while...
<?php
$abfrage = "SELECT * FROM tester";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);
$Name=$row->Name;
$Bech=$row->Beschreibung;
?>
Test Link

Categories