View Full Version : variable doesn't pass

08-01-2012, 07:47 PM
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'm looking through brackets. I'm pretty sure I don't have anything left open? Stumped.


$_SESSION['options_picked'] = array();

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

$var = "4";

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

$choicetest = $_POST['chosen'];

echo "and ".$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 "cat= ".$_POST['what_category'];
echo "var= ".$var;



// 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
// CF: Using sprintf () and templates makes things a whole lot easier to read.
$ExpandTemplate = <<<OutHTML
<div id="%1\$s" style="width:550px;padding-top:20px;">
<img style="position:relative;top:-2px;" src="images/structural/red-plus.gif" /> %1\$s <!--</a>-->
<div id="%2\$s-expander" style="float:left;padding-right:25px;" width="90">

$ExpandImageTemplate = <<<OutHTML
<div style='width:140px;padding:10px;float:left;'> %4\$s <br>
<form action="" method="post">
<button type="submit" name="chosen" id="chosen" value="%4\$s">

<img src="%3\$s" width="147" height="34" alt="image to come" title="choice" />
<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 -->

//$ExpandImageTafter = <<<OutHTML
// </div>
// </div>
// OutHTML;

$Output = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Build my rifle</title>


<div><!-- where the original image at top of page will go -->


//output section and sprintf

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

if ($category != $row['component_category']) {
$category = $row['component_category'];
if (!$firstime) {

$Output .= '</div><br><br>';

} else {
$firstime = true;

//CF: Changed output to be stored in a temp variable, as well as adding output escaping to prevent XSS etc.
$Output .= sprintf ($ExpandTemplate, htmlspecialchars ($row['comp_cat_name']), htmlspecialchars ($row['folder_path']));

//CF: Changed output to be stored in a temp variable, as well as adding output escaping to prevent XSS etc.
$Output .= sprintf ($ExpandImageTemplate, htmlspecialchars ($row['comp_cat_name']),htmlspecialchars ($row['folder_path']),htmlspecialchars ($row['image_filepath']),htmlspecialchars ($row['component_name']));

// when I get a chance, I need to figure out how to escape the /s in the image_filepath in code line above. It wasn't working when it was written: htmlspecialchars (rawurlencode($row['image_filepath'])). it just put %s




echo $Output;


<!-- now show the results of what they've picked so far -->
$results_done = $_SESSION['options_picked'];

foreach($results_done as $key => $val) {
echo "<font color=#ffffff>".$val."</font><br>";
echo $key." ".$val['buttstocks'];

// open as a 2nd form, so that when they are set and hit the view my rifle, it'll take this information to determine what they built and what rifle video to show.

// here's where I need to query the database for categories, then take the sessions array and loop through everything, printing the category title, and their choice where so done, and just the category title if there is no match. Happens on each page reload.

// close form.

function RecursiveWrite($results_done) {
foreach ($results_done as $vals) {
echo $vals['0'] . "\n";




08-01-2012, 10:55 PM

just a missnamed _post variable.

now how do I mark this solved?