Hi All,

I am very new to PHP and still learning lots... :)
I am using the below code to populate a drop down list from my database.
But I am not sure how to display/send the results back to the same page.

The fields in my Database are:
Base, Owner, Cluster, Sector, Coordinates, Defenses, Notes

So based on the code below - when someone selects their Cluster I would like to display all records from database that are from that cluster. Any help is greatly appreciated.
Here is code I have:

<?php

//Populates drop down choice from database field

//Database connection string is here.

$query = "select DISTINCT Cluster FROM Starbase";
$results = mysql_query($query, $link) or die("Error performing query");

if(mysql_num_rows($results) > 0){
echo("<select name=\"selectItem\">");
echo '<option value="">Please Select..</option>';
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->record_id\">$row->Cluster</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>

Dani AI

Generated

The parse error you saw is almost always caused by an unterminated string when PHP tries to interpolate an array index inside a double-quoted string. In this thread was interpolating several $row[...] items in one long double‑quoted string, which is fragile. was right to point out the select/value mismatch: either make each option value the cluster name (and query by that), or include a record id in the dropdown and query by id — but be consistent.

A simple, robust fix is to avoid complex interpolation and build strings with concatenation while escaping output. Example (uses mysqli fetchassoc here to avoid deprecated mysql* functions):

while ($row = $result->fetch_assoc()) {
    $output .= '<li>'
        . htmlspecialchars($row['Base'], ENT_QUOTES, 'UTF-8')
        . ' - ' . htmlspecialchars($row['Owner'], ENT_QUOTES, 'UTF-8')
        . ' - ' . htmlspecialchars($row['Cluster'], ENT_QUOTES, 'UTF-8')
        . '</li>';
}

echo '<option value="' . htmlspecialchars($row['Cluster'], ENT_QUOTES, 'UTF-8') . '">'
     . htmlspecialchars($row['Cluster'], ENT_QUOTES, 'UTF-8') . '</option>';

For security and forward-compatibility switch to prepared statements (mysqli or PDO). Example outline:

$stmt = $mysqli->prepare(
  "SELECT Base,Owner,Cluster,Sector,Coordinates,Defenses,Notes FROM Starbase WHERE Cluster = ?"
);
$stmt->bind_param('s', $selectedCluster);
$stmt->execute();
$res = $stmt->get_result();

Quick checklist: make sure the <select name="..."> matches your POST key, use the same value type in the option and the WHERE clause, escape output with htmlspecialchars, and var_dump($POST) when debugging. The old mysql* extension is removed in modern PHP — migrate to mysqli/PDO and use prepared statements (mysqli prepared statements, ).

Recommended Answers

All 2 Replies

Member Avatar for Member #120589
<?php
$selectedItem = 0; //default
$select = '<select name="selectItem"><option value="">Please Select..</option>';
$output = "";

if(isset($_POST['submit']) && isset($_POST['selectItem'])){
	$selectedItem = intval($_POST['selectItem']);
	$results = mysql_query("SELECT ...fields you need... FROM `Starbase` WHERE `Cluster`=''")or die("Error performing query");
	if(mysql_num_rows($results) > 0){
		$output .= "<ul>";
		while($row = mysql_fetch_array($results)){
			$output .= "<li>{$row['field1']} - {$row['field1']}</li>";	
		}
		$output .= "</ul>";
	}else{
		$output = "<p>No records for this Cluster, which is very strange as it was chosen from the Database</p>";	
	}
}

//I'll keep the fetch_object here
$query = "SELECT `record_id`, `Cluster` FROM `Starbase` ORDER BY Cluster";
$results = mysql_query($query, $link) or die("Error performing query");
if(mysql_num_rows($results) > 0){
	while($row = mysql_fetch_object($results)){
		$sel = ($selectedItem == $row->record_id) ? ' selected="selected"' : '';
		$select .= "<option value=\"{$row->record_id}\"$sel>{$row->Cluster}</option>";
	}
}
$select .= "</select>";
?>

<form method="post">
	<?php echo $select;?>
	<input type="submit" name="submit" value="Submit Me" />
</form>

<?php echo $output;?>

Why are you using SELECT DISTINCT on the Cluster? I hope you haven't been inputting this field individually every time. If so, use a relational model to maintain the data easily. OK, this means using JOINS in your SQL, but that's a lot better than duplicating data.

Thank you for the help - I must still be doing something wrong as I am receiving the following error:

Parse error: syntax error, unexpected T_STRING, expecting ']' in /home/a5063282/public_html/test/test.php on line 20

Would you mind reviewing for my mistake?

<?php
//Database connection string is here

   $selectedItem = 0; //default
    $select = '<select name="selectItem"><option value="">Please Select..</option>';
    $output = "";
     
    if(isset($_POST['submit']) && isset($_POST['selectItem'])){
    $selectedItem = intval($_POST['selectItem']);
    $results = mysql_query("SELECT * FROM `Starbase` WHERE `Cluster`=''")or die("Error performing query");
    if(mysql_num_rows($results) > 0){
    $output .= "<ul>";
    while($row = mysql_fetch_array($results)){
    $output .= "<li>{$row['Base']} - {$row['Owner']} - {$row['Cluster]} - {$row['Sector']} - {$row['Coordinates']} - {$row['Defenses']} - {$row['Notes']}</li>";
    }
    $output .= "</ul>";
    }else{
    $output = "<p>No records for this Cluster, which is very strange as it was chosen from the Database</p>";
    }
    }
     
    //I'll keep the fetch_object here
    // I am NOT SURE whether to leave this or take out?

    $query = "SELECT `record_id`, `Cluster` FROM `Starbase` ORDER BY Cluster";
    $results = mysql_query($query, $link) or die("Error performing query");
    if(mysql_num_rows($results) > 0){
    while($row = mysql_fetch_object($results)){
    $sel = ($selectedItem == $row->record_id) ? ' selected="selected"' : '';
    $select .= "<option value=\"{$row->record_id}\"$sel>{$row->Cluster}</option>";
    }
    }
    $select .= "</select>";
    ?>
     
    <form method="post">
    <?php echo $select;?>
    <input type="submit" name="submit" value="Submit Me" />
    </form>
     
    <?php echo $output;?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.