I need a fresh set of eyes. I'm trying to redo a page from scratch, and simplify things. I've gotten a "beginner-stumping" error. And I'm a beginner, so that makes it harder.

It's a page where the user makes a selection from a form with fields generated by a database. Then the page reloads, with the php determining which options to pull from the database, based on which category they last chose.

It loops through the first time, and works fine. But the second time, I get an error:

"You have an error in your SQL syntax; ... near 'ORDER BY tbl_components.component_category' at line 6"
I echoed the query and I see its not carrying over the $var that second time the page reloads:

"SELECT ....... AND tbl_component_categories.ID = ORDER BY tbl_components.component_category"

Considering it worked on first selection, I'm kind of stumped. I did run the queries in MySQL just to make sure they work, and they do.

Not sure where I'm getting it wrong. I feel like I'm blind. I'm looking through brackets. I'm pretty sure I don't have anything left open? Stumped.

<?php 
session_start();

if(!isset($_SESSION['options_picked'])){
$_SESSION['options_picked'] = array();
} 

if (!isset($_POST['chosen'])) {

    $var = "4";

} 

elseif(isset($_POST['chosen'])) {

    $choicetest = $_POST['chosen'];

    array_push($_SESSION['options_picked'],$choicetest);

echo "THE cat= ".$_POST['what_category'];

echo "and they chose ".$choicetest; // for testing . delete at completion.

    // below section is hard coded for the moment.
    // maybe I query the database for categories and category id, then I can loop to create the below statements
    // so it would be like: if post == 'row[x] { var =  row[y];

        if((isset($_POST['what_category'])) && ($_POST['what_category'] == 'Buttstocks')){ 
        $var = "1";
        } 
        elseif((isset($_POST['what_category'])) && ($_POST['what_category'] == 'Accessory_rail_mounts')){
        $var = "11";
        }
        elseif((isset($_POST['what_category'])) && ($_POST['what_category'] == 'Caliber')){
        $var = "2";
        }
        elseif((isset($_POST['what_category'])) && ($_POST['what_category'] == 'Barrel_length')){
        $var = "10";
        }
        elseif((isset($_POST['what_category'])) && ($_POST['what_category'] == 'Suppressors')){
        $var = "9";
        }


    echo "var= ".$var;

}


include("dbc.php");

// query here
$query  = "SELECT tbl_component_categories.ID, tbl_component_categories.folder_path, tbl_component_categories.comp_cat_name, tbl_components.component_name, tbl_components.image_filepath, tbl_components.component_category
FROM tbl_components JOIN tbl_component_categories ON tbl_components.component_category = tbl_component_categories.ID AND tbl_component_categories.ID = $var ORDER BY tbl_components.component_category";

echo $query;

$result = mysql_query($query)
or die(mysql_error());



// create templates
$ExpandTemplate = <<<OutHTML

            <img style="position:relative;top:-2px;" src="images/structural/red-plus.gif" /> %1\$s 
            <br><form action="" method="post">

OutHTML;


$ExpandImageTemplate = <<<OutHTML

                    <button type="submit" name="chosen" id="chosen" value="%4\$s">

                    <img src="%3\$s" width="147" height="34" alt="image to come" title="choice" />
                    </button>
                    <input type="hidden" name="what_category" value="%2\$s">
                    <!-- this hidden field is what I can use to determine what category, I compare it to -->


OutHTML;


$Output = '';

//output section and sprintf

while ($row = mysql_fetch_assoc ($result)) { 

$Output .= sprintf ($ExpandTemplate, htmlspecialchars ($row['comp_cat_name']), htmlspecialchars ($row['folder_path']));

$Output .= sprintf ($ExpandImageTemplate, htmlspecialchars ($row['comp_cat_name']),htmlspecialchars ($row['folder_path']),htmlspecialchars ($row['image_filepath']),htmlspecialchars ($row['component_name']));


}


?>



<?php

echo $Output;
echo "</form>";

?>




</body>
</html>

Recommended Answers

All 4 Replies

Just a suggestion..

The $var will be stored in temp memory, and, when you re-load the page, the $var value will therefore be cleared. Why not store it as a session?

ah, because I actually want it to clear, it gets reset by that set of elseif statements, depending on what information they chose so that the query will pull the appropriate data for their next choice.

Sorry, my last comment was invalid. Can you tell me what $var actually does? Is it something on the form? If so, couldn't you just have it as an hidden field and then post it across? The aother alternative might be to decide what your form does by using $_GET.

oh, good gawd!!

It was just a naming issue. one of the rows from the database is supposed to have capital letters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.