Working with HTML select and PHP

I want to use the html select to search for info from the database but no matter how I tried, it doesn’t work. So, i thought maybe since there is no way I was able to detect which element or data was selected(maybe that is why it dosen’t pull anything from the database).

My question now is how am I able to detect which select statement was selected(I populate the select field dynamically from the database). This is the code I write myself.

<form action="bred.php?go" method="post" id="submit">
	<fieldset>
	<?php $query = "SELECT mtext FROM allwords";
	$stmt = $conn->prepare($query);
	$stmt->execute();
	$word = $stmt->fetch();?>

	<select id="" name="select_type">
            <?php while($word != null){
		echo '<option value="/'.$word['mtext'].'" selected="selected">'.$word['mtext'].'</option>';
		
                $word = $stmt->fetch();
            }
            $stmt->closeCursor();?>
	</select>

You posted only some of the code, but I see some potential issues / problems.

  • the action has a GET and the method is POST - but maybe you know that already
  • fetch defaults to “both”, column names and numbers - but the while loop handles only name indexes
  • the while loop is outputting select=“selected” for every option - but maybe you want that
  • you have closeCursor - but maybe you need to execute the statement again

This is the whole code.

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(!empty($_POST['select_type']) ){
			$select_type = trim(filter_input(INPUT_POST, 'select_type', FILTER_SANITIZE_STRING));
            $select = $select_type."%";
				
				$display_search = "SELECT sno, bname, fm, sm, pname FROM book WHERE fm LIKE :select_type OR sm LIKE :select_type";
									//" OR sm LIKE :select_type";
				//SELECT pname, sm, fm, sno FROM book WHERE sm LIKE 'آئي%' OR fm LIKE 'آئي%'
				//show display (sd)
				$sd = $conn->prepare($display_search);
				$sd->bindParam(':select_type', $select, PDO::PARAM_STR);
				$sd->execute();
				$num = $sd->rowCount();
				$sd->closeCursor();
				
				if($num > 0){
					'<table>
						<tr>
							<th>echo 1 </th>
							<td>echo 2 </th>
							<td>echo 3 </th>
						</tr>';
						//display searched results with displaynow(dn)
						while($dn = $sd->fetch(PDO::FETCH_ASSOC)){
							'<tr>
								<td>'. print $dn['sno'].'</td>
								<td>'. print $dn['bname'].'</td> 
								<td>'. print $dn['pname'].'</td>
								<td>'. print $dn['fm'].'</td>
								<td>'. print $dn['sm'].'</td>
							</tr>';
						}
					'</table>';
				}
		}
}?>

<form action="bred.php?go" method="post" id="submit">
	<fieldset>
	<?php $query = "SELECT mtext FROM allwords";
	$stmt = $conn->prepare($query);
	$stmt->execute();
	$word = $stmt->fetch();?>

	<select id="" name="select_type">
            <?php while($word != null){
			echo '<option value="/'.$word['mtext'].'" selected="selected">'.$word['mtext'].'</option>';

                $word = $stmt->fetch();
            }
            $stmt->closeCursor();?>
	</select>
<input type="submit" id="submit" name="submit" value="Search">
	</fieldset>	
</form>

Also, the data requested from the table has one one field

I don’t think you can do what you’re hoping to do by specifying the same parameter name twice in the query. I believe you have to name them separately, then pass the same value into both.

1 Like

Try adding this temporarily

<?php 
/* DEVELOPMENT TROUBLESHOOTING ONLY !!! */ 
error_reporting(-1); 
ini_set('display_errors', 1);
/* END OF TROUBLESHOOTING LINES */
if($_SERVER['REQUEST_METHOD'] == 'POST'){

You didn’t give the <select> “multiple” which would allow more than one <option> to be selected and be an array not a string. AFAIK, without multiple, only one option can be selected. Your code is currently giving every option selected. I don’t know if any given browser would take one and disregard the others or pass an array of values. As you want only one option, for now try removing the selected="selected" bit.

You also call closeCursor() before you fetch the results - won’t this give you no results? Surely the closeCursor() comes after you’ve retrieved the results?

Good catch. You can assign the same value to each, but they should have different names.

This bit’s weird as well:

if($num > 0){
  '<table>
  <tr>
  <th>echo 1 </th>
  <td>echo 2 </th>
  <td>echo 3 </th>
  </tr>';
  //display searched results with displaynow(dn)
  while($dn = $sd->fetch(PDO::FETCH_ASSOC)){
    '<tr>
    <td>'. print $dn['sno'].'</td>
    <td>'. print $dn['bname'].'</td> 
    <td>'. print $dn['pname'].'</td>
    <td>'. print $dn['fm'].'</td>
    <td>'. print $dn['sm'].'</td>
    </tr>';
    }
  '</table>';
  }
}

Yes. The strings missing echo’s?

Yes, that or switching in and out of PHP.

Can you please help me rewrite it?

Maybe the best place to start would be fixing errors that PHP is reporting.

What do you see when you run the file(s) after adding the troubleshooting lines?

This is the error it shows for using display_errors()

Fatal error: Uncaught Error: Call to undefined function display_errors() in C:\xampp\htdocs\pakistani\bred.php:3 Stack trace: #0 {main} thrown in C:\xampp\htdocs\pakistani\bred.php on line 3

D’oh! that’s what I get for copying-pasting and not paying attention (or thinking) sorry :blush:

display errors is an ini setting so that should be

ini_set('display_errors', 1); 

* I’ve edited the error in my previous post

It dosen’t display anything

Show us the code as it is now. Have you fixed all the errors pointed out above, such as those where you just put quoted strings in place without echo statements?

Where does your database connection come from? I see you using $conn, but I don’t see where you either define it, or include a file that defines it for you.

Yes, I have done all those ones. The $conn is the connection parameters I created in db_connect.php

Where you show the “whole code” above, though, there is no reference to including that file, as far as I can see.

It would still be handy to see the current version of the code, in case there are any typos or that kind of thing.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.