Jump to content

Trying to make data from database table display on webpage, when user chooses appropriate value through dropdown list


Recommended Posts

Hi people, i am basically trying to have the functionality of a dropdown menu that when chosen a specific value from it, the resulting display will be that values data from my databse table, this information gets displayed on a html format table.

 

I am keep getting the error:

 

Notice: Undefined variable: q in E:\website\htdocs\restricted\test\getproduct.php on line 17

 

Please have in mind i am a completely novice with PHP, and doing this just to get better, please if someone can help me get my head around this, it will be much appreciated. Thank You.

 

 

HTML CODE is:

 

<html>
<head>
<script>
function showproduct(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("GET","getproduct.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="laptop_products" onchange="showproduct(this.value)">
<option value="">Select a person:</option>
<option value="1">SAMSUNG</option>
<option value="2">HP</option>
<option value="3">ASUS</option>
<option value="4">LENOVO</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

 

The PHP CODE is:

 

<?php

if (isset($_GET['q']))  {
echo $_GET['q'];

}
$dbhandle = mysql_connect('localhost','root','','store');
if (!$dbhandle)
  {
  die("Could not connect: " . mysql_error());
  }

mysql_select_db("store")
      or die ("Could not connect: " . mysql_error());


$sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'";


$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>product_id</th>
<th>product_name</th>
<th>weight</th>
<th>price</th>
<th>description</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['product_id'] . "</td>";
  echo "<td>" . $row['product_name'] . "</td>";
  echo "<td>" . $row['weight'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
  echo "<td>" . $row['description'] . "</td>";
  echo "</tr>";
  }
echo "</table>";



mysql_close($dbhandle);
?>

You haven't declared $q.  You stopped at echo'ing $_GET['q'], but since registered_globals are turned off (as they should be), the $_GET array isn't extracted. 

 

if (isset($_GET['q']))  {
echo $_GET['q'];
$q = (int)$_GET['q'];

}

 

You haven't declared $q.  You stopped at echo'ing $_GET['q'], but since registered_globals are turned off (as they should be), the $_GET array isn't extracted. 

if (isset($_GET['q']))  {
echo $_GET['q'];
$q = (int)$_GET['q'];

}

Hey, i added the extra line of code but i am still for some reason still receiving the same error message :S

 

I am sorry, but is there anything else another fresh pair of eyes my see what i am doing wrong?

You could start by showing the new message and also the line that it points to

Yes that is true sorry,

 

well i am receiving the same error message 

 

Notice: Undefined variable: q in E:\website\htdocs\restricted\test\getproduct.php on line 23

 

after using the advice that was offered above.

 

The error is refering to line 23 which is:

 

$sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'";

 

from the above code in the php page

 

any hints?

Try this:

$sql="SELECT * FROM laptop_products WHERE product_id = = '{$q}'";

Jon

Aspiring Web Developer

@JonEdney1

no seems not to work tried that before too, however, receive the following:

 

Notice: Undefined variable: q in E:\website\htdocs\restricted\test\getproduct.php on line 24

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in E:\website\htdocs\restricted\test\getproduct.php on line 37

Change line 23 back to this:

$sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'";

Them, where you have the variable q set, set it this way:

$q = isset($_GET['q'])?$_GET['q']:null;

Let me know if that gets things moving.

 

Jon
Aspiring Web Developer
@JonEdney1

Change line 23 back to this:

$sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'";

Them, where you have the variable q set, set it this way:

$q = isset($_GET['q'])?$_GET['q']:null;

Let me know if that gets things moving.

 

Jon

Aspiring Web Developer

@JonEdney1

Jon, yeah, that worked, thank you,really appreciated, thanks for the time.

 

However, now after resolving this problem, the main aim of this code which is to select a value from the drop down list and in return display the table related to that value from the database is not happening. 

 

Life story to PHP, never ending problems ; )

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.