I am trying to create a simple search page as practice for later use, where i search a table "practice" with fields "name" and "married". The user should then be able to search names, and upon submitting the page should display the names alongside the marital status. My problem is this: I want the status to be a dropdown (i tried checkboxes with much difficulty, failed) with the options yes and no, and then the user may be able to change the status by selecting from the dropdown and submitting.

Right now I am having a problem with assigning names to the dropdowns. To be able to relate them to the specific record in the database I should provide them with unique names right? But I used an array and when I post it always contains only one value, the first record. Here is my code. Please tell me what I'm doing wrong.

<?php
if (isset($_POST['submitted'])){
if (isset($_POST['search'])){
	$nm=$_POST['name'];
	$st=$_POST['status'];
	
	require_once ('mysql_connect.php');
	$query="SELECT * FROM practice WHERE name LIKE '%$nm%' AND married LIKE '%$st%'";
	$result=mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: ". mysql_error());
	if(@mysql_num_rows($result) > 0) {

	echo '<h2>Search Results</h2>';

	echo '<form name="results" method="POST" action="search.php">';
	echo '<table border="1" cellspacing="0" width="320">';
	echo '<tr bgcolor="#AAAAAA"><td align="center"><b><font color="#FFFFFF">Name</font></b></td><td align="center"><b><font color="#FFFFFF">Status</b></font></td></tr>';

	$x=0;
	while($row = mysql_fetch_assoc($result)) {

	$name[]=$row["name"];
	$status[]=$row["married"];
	
	echo '<tr>';
	echo '<td> $name[$x] </td>';
	echo '<td align="center"><select name="dropdown[]">';
	echo '<option value="Yes" ';
	echo 'if ($status == "Yes"){echo "selected";}';
	echo '>Yes</option>';
	echo '<option value="No" ';
	echo 'if ($status == "No"){echo "selected";}';
	echo '>No</option></select></td>';
	echo '</tr>';

	$x++;
	}

	echo '<td colspan="2" align="center"><input type="hidden" name="submitted" value="TRUE"><input type="submit" name="save" value="Save Changes"></td>';
	echo '</table>';
	echo '<br>Do another <a href="search.php">SEARCH</a>?';
	echo '</form>';

	}else{
	echo '<h2>Search Results</h2>';
	echo 'No results found. Do another <a href="search.php">SEARCH</a>?';
	}
}//closing for search
if (isset($_POST['save'])){
$y = $_POST['dropdown'];
	foreach($y as $value){	
	echo "$value<br/>";
	}
}//closing for save
}else{
?>
<h2>Search</h2>
<form method="post" action="search.php">
	<table border="0">
  		<tr>
			<td>Name</td>
			<td> 
		        <input type="text" name="name">
			</td>
		</tr>
		<tr>
			<td>Status</td>
			<td>
			<input type="text" name="status">
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center"><input type="submit" name="search" value="Search">
			<input type="hidden" name="submitted" value="TRUE">
			</td>
		</tr>
	</table>
</form>
<?php } ?>

nevermind, i figured out what was wrong. the form was posting to a older version of the file, stupid me. lol. ill be back later when i run into more problems. xD

This article has been dead for over six months. Start a new discussion instead.