Page 1 of 1

Query Select where = form input

Posted: Sat Aug 02, 2008 6:34 pm
by ardan16
Hi all,
Frustration setting in

Code: Select all

<form>
<select name="find"  value="<?php if (isset($_POST['find'])) echo $_POST['find']; ?>" >
<label>bank</label>
<option value ="Commonwealth Bank">Commonwealth Bank</option>
<option value ="Westpac">WestPac</option>
<option value ="City Bank">City Bank</option>
<option value ="St George">St George</option>
<option value ="Bendigo Bank">Bendigo Bank</option>
</select>
<input type="submit" name="submit" value="Post Comment" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
 
<?php
require_once ('mysql_connect.php'); // Connect to the db.
 
$find = $_POST['find']; 
 
// Make the query.
$query = "SELECT(name) AS name, (comment) AS com, (date_reg) AS date,(sub_by) AS user FROM banks WHERE name = '$find'"; 
$result = @mysql_query ($query); // Run the query.
$num = mysql_num_rows($result);
Can someone please tell me what is wrong with the above code.
How do I get the forms input to be what the search equals?

Cheers

Re: Query Select where = form input

Posted: Sat Aug 02, 2008 7:14 pm
by jaoudestudios
Hope this helps...

I started the idea you should be able to follow it and continue it for as many options as you require. I am assuming when you said 'input' you meant 'select'? I modified your sql query as you had some code in there that was not required.

Code: Select all

 
   <?php
   if ($_POST['find'] == 'Commonwealth Bank') { $sel_1 = " selected='selected'"; }
   if ($_POST['find'] == 'Westpac') { $sel_2 = " selected='selected'"; }
   if ($_POST['find'] == 'City Bank') { $sel_3 = " selected='selected'"; }
   // and so on...
   ?>
   <form action='#' method='post'>
        <fieldset title='something'>
             <label>bank</label>
             <select name='find'>              
             <option value ='Commonwealth Bank'<?php echo $sel_1; ?>>Commonwealth Bank</option>
             <option value ='Westpac'<?php echo $sel_2; ?>>WestPac</option>
             <option value ='City Bank'<?php echo $sel_3; ?>>City Bank</option>
             <option value ='St George'>St George</option>
             <option value ='Bendigo Bank'>Bendigo Bank</option>
   </select>
   <input type="submit" name="submit" value="Post Comment" />
   <input type="hidden" name="submitted" value="TRUE" />
   </form>
  13.  
  14. <?php
  15. require_once ('mysql_connect.php'); // Connect to the db.
  16.  
  17. $find = $_POST['find'];
  18.  
  19. // Make the query.
  20. $query = "SELECT name, comment AS com, date_reg AS date, sub_by AS user FROM banks WHERE name = '".$find."'";
  21. $result = @mysql_query($query); // Run the query.
  22. $num = mysql_num_rows($result);
 
Let us know how you got on :)

PS. It would be worth reading on some html basics as your form would not valid and would only work as it would use default settings. I have added in 'action' and 'method' and 'fieldset'.

Re: Query Select where = form input

Posted: Sat Aug 02, 2008 9:17 pm
by ardan16
Thanks jaoudestudios,
It worked like a dream.

Cheers