Page 1 of 1

passing variables via a html form, no results showing

Posted: Wed Mar 18, 2009 12:16 pm
by dragon_agility
Hi!

I've got a php project connected to a local database where I've created a form where users can search for a book based on it's subject. When the user presses search, the information should be posted to another php page within my project and the filtered information shown within a table.

However, although there are no syntax errors, the variables are not being passed. The page is blank.

I was wondering if anyone could look over my code and help me with any suggestions? I'm fairly new to php (6 months) and it's frustrating my greatly.

thanks.


index 1st page.php

Code: Select all

<?php
$db = new mysqli ('localhost','root','','library');
$results = $db->query('select distinct subject from catalogue order by subject');
?>
<form action="view.php?library" method="POST">
<select name="library">
<option>All Subjects</option>
<?php while ($row = $results->fetch_assoc()){?>
<option><?php echo $row['subject']?></option>
<?php }?>
<input name="library" input type="submit" value="search" />
</select>
</form>
<?php
$db->close();
?>


view page.php

Code: Select all

 
<?php
$library = $_POST['library'];
$db = new mysqli ('localhost','root','','library');
$sql = "select author, title, description from catalogue where subject = '{$_POST['library']}' order by title";
$results = $db->query($sql);
?>
<table border="3" cellpadding="8">
<?php while($row = $results->fetch_array(MYSQLI_ASSOC)){ ?>
<tr>
<td width="300px" border="1"><?php echo $row['author'] ?></td>
<td width="300px" border="1"><?php echo $row['title'] ?></td>
</tr>
<tr><td width="500px" border="1"><?php echo $row['description'] ?></td></tr>
<?php } ?>
</table>
 
<?php $db->close(); ?>
 

Thanks again!

Re: passing variables via a html form, no results showing

Posted: Wed Mar 18, 2009 1:30 pm
by php_east
may i know which db library you are using, because this part

Code: Select all

 
<?php while ($row = $results->fetch_assoc()){?>
 
does not look right to me. it may have to be something like
$row = $db->fetch_assoc($results);

because $results would not carry the db library itself, only result id i presume, hence $results->fetch_assoc() does not look right at all.

but i am guessing without more info, it would be higly dependent on your db library, if you could share that. thanks.