0

I'm trying to do a select from a table based on the post value of an HTML select box. I'm getting no results at all, I'm echoing out the post value no problem. The statement works on it's own but won't when I use the select form to populate it. This is just my test I will be adding other options to the dropdown box.

<?php
if(isset($_POST['value'])) {
if($_POST['value'] == 'Militaria') {
    $query = "SELECT * FROM listings WHERE category1=Militaria";  
}
else {  
    // query to get all records  
    $query = "SELECT * FROM listings";  
}  
}
$sql = mysql_query($query);  
while ($row = mysql_fetch_array($query)){ 
    echo 'Description:' . $row['description'];
}
mysql_close($con);    
?>

Here is the html form I'm using, can anyone tell me where I'm going wrong, should I do it a different way etc, I'm new to php? Thanks!!

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' > 
<select name="value"> 
    <option value="all">All</option> 
    <option value="Militaria">Militaria</option>
</select> 
<br /> 
<input type='submit' value = 'Filter'> 
</form>
2
  • try var_dump($_POST) and see if the there are values inside $_POST Commented May 16, 2015 at 11:51
  • Add single quote near ...category1= 'Militaria' Commented May 16, 2015 at 11:57

3 Answers 3

2

mysql_fetch_array() should receive resorce as a parameter. Try mysql_fetch_array($sql).

Sign up to request clarification or add additional context in comments.

Comments

0

Quote around 'Militaria' and mysql_fetch_array($sql)

<?php

        if(isset($_POST['value'])) {
            if($_POST['value'] == 'Militaria') {
                $query = "SELECT * FROM listings WHERE category1='Militaria'";  
            }
            else {  
                // query to get all records  
                $query = "SELECT * FROM listings";  
            }  

            $sql = mysql_query($sql);  
            while ($row = mysql_fetch_array($sql)){ 
                echo 'Description:' . $row['description'];
            }
            mysql_close($con); 
        }
    ?>

    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' > 
    <select name="value"> 
        <option value="all">All</option> 
        <option value="Militaria">Militaria</option>
    </select> 
    <br /> 
    <input type='submit' value = 'Filter'> 
    </form>

Comments

0

You have two mistakes in your php code.

1st : quote around Militaria. The query should be, $query = "SELECT * FROM listings WHERE category1='Militaria'";

2nd : mysql_fetch_array accepts executed query's result as parameter. It should be, $row = mysql_fetch_array($sql)

Final code:

<?php
if(isset($_POST['value'])) {
if($_POST['value'] == 'Militaria') {
    $query = "SELECT * FROM listings WHERE category1 = 'Militaria'";  
}
else {  
    // query to get all records  
    $query = "SELECT * FROM listings";  
}  
}
$sql = mysql_query($query);  
while ($row = mysql_fetch_array($sql)){ 
    echo 'Description:' . $row['description'];
}
mysql_close($con);    
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.