0

Hello,

I have looked over many examples of how to populate a dropdown with rows like

$sql="SELECT id, thing FROM table"; 
$result=sqlsrv_query($conn,$sql) or die("Couldn't execut query");

while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
    $id=$row["id"]; 
    $thing=$row["thing"]; 
    $options.="<option value=\"$id\">".$thing "</option>"; 
}

But i need to populate this list with tables and will need to set values for an auto-submit feature.

I know using

$sql = "SELECT NAME FROM <database>..sysobjects WHERE xtype = 'U'";

will give me a list of the tables but im not sure how to use with a while loop.

Any help would be greatly appreciated.

2
Contributors
3
Replies
7
Views
7 Years
Discussion Span
Last Post by jenyroger
0

Ok I have semi-gotten it to work useing (I was unaware "name" was an actual thing you could search for)

<?php 
$sql = "SELECT NAME FROM test1..sysobjects WHERE xtype = 'U'";
$result = sqlsrv_query($conn,$sql) or die("Couldn't execut query");

echo "<select>";
while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
	
	echo "<option>";
	echo $data['NAME']; 
	echo "</option>";
}
echo "</select></br></br>";
?>

Now im throwing ideas in my head, but i need the table to be known so on the next pages values can be inserted into it. Im completely new at both PHP and SQL Server. So does anyone know how I could accomplish this?

0

Ok i figured it out. I decided to save the selected value as a session variable. And just made a submit botton with the dropdown menu

<html>
</body>
<!-- form for tower selection -->
<form action="welcome.php" method="POST">
Please select the tower you are about to work on. </br></br>
<select name="TowerSelect"><option> Choose </option>

<?php

$sql = "SELECT NAME FROM test1..sysobjects WHERE xtype = 'U'";
$result = sqlsrv_query($conn,$sql) or die("Couldn't execut query");

while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
	
	echo "<option value=";
	echo $data['NAME'];
	echo ">";
	echo $data['NAME']; 
	echo "</option>";
}

?>
&nbsp;&nbsp;<input type="submit" value="Select Tower">
</select></br></br>
</form>
</body></html>

<?php

if(empty($_POST['TowerSelect'])){     
	$_SESSION['tower'] = ''; 
} else {  
	$_SESSION['tower'] = $_POST['TowerSelect']; 
	echo "<tr><p style='color:blue' colspan='2'>";
	echo $_SESSION['tower']; 
	echo "  selected. </p></tr>";
	
}
0

There are so much extract entries with using PHP and to create the data base table there are lots of queries to select the table, drop the table with using different syntax . There is also a drop down list box into the MY SQL data base the PHP syntax are always with $ sign .

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.