php - I want to select a table from a database and show in a drop down list -
php - I want to select a table from a database and show in a drop down list -
i wanted select table specific database , list of tables appear in alternative tag under select tag.
thanks lot.
current code:
<?php include 'database/connectdatabase.php'; if(isset($_post['select_db'])) { $select_db = $_post['select_db']; $selectdb_query = 'show tables $select_db'; $query_select = mysql_query($selectdb_query,$connectdatabase); if(!$query_select) { echo 'no table selected!'; } ?> <form method="post" action="selecttable.php" autocomplete="off"> <select name="select_db"> <option selected="selected">select database</option> <option>section_masterfile</option> </select> </form> <form method="post" action="#" autocomplete="off"> <?php while ($row = mysql_fetch_row($query_select)) { $num_row = mysql_num_rows($row);?> <select name="select_table"> <option selected="selected">select table</option> <?php for($i=0;$i>=$num_row;i++){?> <option><?php echo $row[0];?></option> <?php}?> </select> <?php}?> </form>
the problem you're fetching rows yet haven't submitted form yet.
i suggest restructure logic way:
$con = new mysqli('localhost', 'username', 'password', 'database'); $tables = array(); if(isset($_post['select_db'])) { // if submitted $select_db = $con->real_escape_string($_post['select_db']); // escape string $query = $con->query("show tables $select_db"); while($row = $query->fetch_assoc()) { $tables[] = $row['tables_in_' . $select_db]; // utilize associative instead } } ?> <form method="post" autocomplete="off"> <select name="select_db" onchange="this.form.submit();"> <option disabled selected>select database</option> <option>test</option> </select> <br/><br/> <select name="select_table"> <?php foreach($tables $table): ?> <option value="<?php echo $table; ?>"><?php echo $table; ?></option> <?php endforeach; ?> </select> </form>
sidenote: if have turned on error reporting, should have given reddish lite doing wrong. kindly turn on.
error_reporting(e_all); ini_set('display_errors', '1');
php html mysql
Comments
Post a Comment