Displaying data from mysql, with a twist

Hi,
I am very new to php so feel free to have a giggle at my code. I am trying to populate a table with data after an option has been selected form a drop down menu in a form. My problem comes from the fact that the drop down options are generated from the data, or I’m just being really stupid.

here is a snippet

[code]<?php

include 'includes/connection.php';

$query = ("SELECT * FROM COSHH") or die ('Error: '.mysql_error ());

$result = mysql_query($query);

while($substance = mysql_fetch_array($result, MYSQLI_ASSOC)) {

echo "<form action'result.php' name='Substance_choice' method='post'>Please choose a Substance<br/><br/>
		<select name='substance_dropdown'>
		<option value='" . $substance['substance_name'] . "'>" . $substance['substance_name'] . 
		"</option>";

}
echo “”
?>

Results

<?php if (isset($_POST['submitted'])) {

include(‘includes/connection.php’);

$subname = $_POST[’$substance’];
$substance_dropdown = $_POST[‘substance_dropdown’];

$query = “SELECT * FROM COSHH WHERE substance_dropdown=’$subname’”;
$result = mysqli_query($dbcon, $query) or die(‘error getting data’);

echo “

”;
echo “”;
while($substance = mysql_fetch_array($result, MYSQLI_ASSOC)) {

echo "<tr><td>";
echo $substance['id'];
echo "</td><td>";
echo $substance['substance_name'];
echo "</td><td>";
echo $substance['manufacturer'];
echo "</td><td>";
echo $substance['assessment_date'];
echo "</td><td>";
echo $substance['reassessment_date'];[/code]

Thanks in advance for any help on this!

ID Name Age

Hi retrobot,

I don’t think if this helps but try to replace this particular line of code:
[php]echo "<form action’result.php’ name=‘Substance_choice’ method=‘post’>Please choose a Substance

[/php]
with
[php]echo "Please choose a Substance


[/php]

Also, this particular line
[php]$subname = $_POST[’$substance’];[/php]
will cause an error; there’s no control found in your form named as ‘$substance’. It must be
[php]$subname = $_POST[‘substance_name’];[/php]

Also, there’s no need for you to redeclare your connection include(‘includes/connection.php’);" next to
if (isset($_POST[‘submitted’])) {.

Hi codeguru thanks for the help,

the form did originally post to a different page “results.php” which is something I had been trying instead of posting to self, I hadn’t tried this method though. In regards to the control, the item I’m trying to pass to the second query is a user option from results of the first query. If that makes any sense.

Sponsor our Newsletter | Privacy Policy | Terms of Service