Solved

PHP dynamic dropdown menu containing MYSQL data

Posted on 2011-02-21
4
346 Views
Last Modified: 2012-05-11
hi,

I am having a problem retrieiving the data from a dropdown menu:

the url for the site is: http://www.gavinbuczko.co.uk/phpnestedsql.php

the complete code I have is:

<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>


<?php

//table name - not active yet
$tableselection = $_POST["table"];
$lockvar = false;

include ("connection.php");
      //http://www.bitrepository.com/remove-empty-values-from-an-array-in-php.html
      ?>
   
   
    <FORM method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?
/////////////////////////// select table///////////////////////////
echo "Please select a Table:";
echo"<br>";
$con = mysql_connect($server,$username,$password);
mysql_select_db($databasename, $con);

$result = mysql_query("SHOW TABLES FROM $databasename");

$blank = "";
$alpha = 0;

echo "<Select name=select_table[]  size=4 multiple>";

  while ( $row = mysql_fetch_array($result) )  
    {  
       echo ("<option value=\"$row[0]\">$row[0]</option>");
    }
echo "</Select>";
echo "<input type=Submit value=pass on>";
echo "</form>";

$select_table = $_POST['select_table'];
$tablecount = count($select_table);
?>

    <FORM method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?


//print_r($select_table);


$i = 0;
while ($i < $tablecount) {

echo "<br/>$select_table[$i]$i<br/>";

$con2 = mysql_connect($server,$username,$password);
mysql_select_db($databasename, $con2);

$result2 = mysql_query("SHOW COLUMNS FROM $select_table[$i]");
//I am trying to get the data from this set of menus//
echo"<SELECT name=field_select.[$i]  size=4 multiple>
<OPTION value=''>please select</OPTION>";
while($row = mysql_fetch_array($result2, MYSQL_NUM))
{
echo"<br/>  $row[0] Table:  <br/>";
echo"<OPTION value='{$row[0]}'>$row[0]</OPTION>";

}
echo"</SELECT>";



mysql_close($con2);

    $i++;  
}
?>
<br>
<input type=Submit value=pass on>
</form>


<?

$field_select_passed = $_POST['field_select0'];

echo "the user selected $tablecount";

print_r($field_select_passed);

?>

basically what I am trying to do is to reteive the data in from the menus indicated in the code.
I it would work if the table name had its name declared explicitly, however it must be dynamic, but I have named it field_select.[$i] , so the first name would be $field_select0, second being $field_select1, etc...

the reason these need to be dynamic is because the user needs to be able to select any number of options from ther table field. as the code suggests, this is to build a MySQL string using joins, when it works.

I have done:
$field_select_passed = $_POST['field_select0'];
print_r($field_select_passed);

which should theoretically output the selections of the first menu, however it does not.
can someone please tell me where I am going wrong please.

thanks,

Gavin
0
Comment
Question by:GPB1983
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 34942561
Just glancing at your code, you need to put array variables in {} for them to be parsed in double quoted strings:

$result2 = mysql_query("SHOW COLUMNS FROM $select_table[$i]");

should be:

$result2 = mysql_query("SHOW COLUMNS FROM {$select_table[$i]}");
0
 

Author Comment

by:GPB1983
ID: 34942607
hi, thanks for the quick reply, but that has abolsutely no effect to the problem posted, I have done as suggested and it changes nothing what so ever.  as can be seen here:
http://www.gavinbuczko.co.uk/phpnestedsql.php

I had another go at it and it works now,

it passes the table names down fine. the inituial problem actually was with:
echo"<SELECT name=field_select[$i][]  size=4 multiple>, it didnt need to . inbetween the field_select abnd [$i].

regards, gavin
0
 
LVL 5

Accepted Solution

by:
wmadrid1 earned 500 total points
ID: 34945856
I thing your workflow had a bad structure.
Your FORM tag, must be just once.

You can do it in 2 ways:

1. Reloading the page, when the user select the table.
<FORM method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form1">
<select name=select_table  onchange="javascript: document.form1.submit();">

and then when the
$select_table = $_POST['select_table'];
var dont be empty, make the other select of columns of the table.


2. Working the select's with AJAX, so when the user change te table selection, the page does not reload, but the columns select is reloaded.
0
 

Author Closing Comment

by:GPB1983
ID: 34982298
great  help, straight to the point!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question