4 Replies - 2476 Views - Last Post: 21 April 2011 - 06:23 AM Rate Topic: -----

#1 Codey09  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 210
  • Joined: 20-August 09

Get info from a database into a table from a dropdown list

Posted 21 April 2011 - 05:11 AM

What I have been trying to do for the past 2 days is have a dropdown list where the list data comes from the database, I got that working fine. What I am having trouble with is when the selected value is showing and when the user selects a different option that the info related to that option is displayed in a table below.

Below is my code
<form id="form1" name="holistic" method="post" action="">
  <label>Treatment choice
    <select name="select" id="select">
	<?php

$con = connectToDatabase();
	  
	
      $sql = mysql_query("SELECT * FROM holistic ORDER BY holistic_id");
	  if (!$sql)
	  {
		  die('Could not query: ' .mysql_error());
	  };
	  
	  while ( $info = mysql_fetch_assoc($sql))
	  {
		  echo "<option name=".$info[holistic_id]." value=".$info[holistic_id].">".$info[treatment]."</optgroup>";
		  
	  }
	

   echo "</select>";
  echo "</label>";
  echo "<input name='Go' type='submit' />";
echo "</form>"
?>
<?php

$selection_id = $_POST["holistic_id"];
$con = connectToDatabase();
	  
	
      $sql = mysql_query("SELECT * FROM holistic WHERE holistic_id=$selection");
	  if (!$sql)
	  {
		  die('Could not query: ' .mysql_error());
	  };
	  
	  echo "<table>";
	  echo "<tr>";
	  while ($info = mysql_fetch_assoc($sql))
	  {
		  echo "<td>".$info[description]."</td>";
		  echo "<td>".$info[cost]."</td>";
		  echo "<td>".$info[time]."</td>";
	  }
	  echo "</tr>";
	  echo "</table>";
?>


I would appreciate any help and advice on this as I am new to php.

The error I am getting is Could not query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Is This A Good Question/Topic? 0
  • +

Replies To: Get info from a database into a table from a dropdown list

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Get info from a database into a table from a dropdown list

Posted 21 April 2011 - 05:18 AM

Line 17 replace your indices with strings

echo "<option name=".$info['holistic_id']." value=".$info['holistic_id'].">".$info['treatment']."</optgroup>";


You don't need more than one connectToDatabase() call per page unless you're closing one of them.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Get info from a database into a table from a dropdown list

Posted 21 April 2011 - 05:19 AM

Have you checked out this tutorial?
Was This Post Helpful? 0
  • +
  • -

#4 Codey09  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 210
  • Joined: 20-August 09

Re: Get info from a database into a table from a dropdown list

Posted 21 April 2011 - 05:45 AM

I have got the data to fill the dropdown list.
What I need to do and cant work out how to is when the user clicks on the Submit button, I need it to store the holistic_id value into a variable.
This variable needs to be used in another sql query to get the description, cost and time data from the database.
To which i want displayed in a table.
Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Get info from a database into a table from a dropdown list

Posted 21 April 2011 - 06:23 AM

Separate your PHP from your HTML. And use quotes in your indices...it's an ASSOC array!

<?php 
if (isset($_POST['Go']))
{
	$id = $_POST['select'];
	$con = connectToDatabase();
	$results = @mysql_query("SELECT * FROM holistic WHERE holistic_id = '$id'", $con);
	if(mysql_errno) die("Error in query");
	while($row = mysql_fetch_assoc($results))
	{
		echo "<option value='{$row['holistic_id']}>{$row['holistic_id']}</option>"; //concat the rest
	}
}
else
{
?>
	<form action="<?php echo $_SERVER['PHP_SELF']?>">
		<select id="select" name="select">
			<!-- whatever here -->
		</select>
	</form>
<?php
}
?>


This post has been edited by codeprada: 21 April 2011 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1