•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,476 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,244 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 603 | Replies: 8 | Solved
![]() |
•
•
Join Date: Aug 2007
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
So, this is a "next step" for me learning how to pass multiple variables from a form to a database and publish the results. I feel that I'm *holds fingers in air* this close, but the actual solution is alluding me.
Goal: To have user select from dropdown list of 4 variables and have the query show results of that query. If there is nothing selected, show all.
Knowledge: Quite used to working and modifying other peoples scripts. First time connecting raw script to a database. I've moderate knowledge of passing variables with GET and POST from form to another page. And have knowledge of some PHP scripting and syntax.
Form Script:
and Result Page/Query Page. (note I do have mysql connection page setup and that is working)
resource PHP.net mysql_query()
So - after a good attempt, I'm stuck. I'm thinking I've got my variables all mixed up - but need a tip/direction/solution on how to fix this.
Thanks
Jade
Goal: To have user select from dropdown list of 4 variables and have the query show results of that query. If there is nothing selected, show all.
Knowledge: Quite used to working and modifying other peoples scripts. First time connecting raw script to a database. I've moderate knowledge of passing variables with GET and POST from form to another page. And have knowledge of some PHP scripting and syntax.
Form Script:
<form action="houseplan_results.php" method="get" name="house plans" id="house plans">
<div class="houseplans">
Type of House Plan:
<select name="houseplans">
<option selected="selected" value="All Types">All Types</option>
<option value="Beach/Coastal">Beach/Coastal</option>
<option value="Bungalow">Bungalow</option>
<option value="Contemporary">Contemporary</option>
<option value="Country">Country</option>
<option value="Craftsman">Craftsman</option>
<option value="European">European</option>
<option value="French">French</option>
<option value="Modern">Modern</option>
<option value="Northwest">Northwest</option>
<option value="Ranch">Ranch</option>
<option value="Victorian">Victorian</option>
<option value="Commercial">Commercial</option>
</select>
</div>
<div class="listbox">
Number of Bedrooms:
<select name="bedrooms">
<option selected="selected" value="">Any</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="13">13</option>
</select>
</div>
<div class="listbox">
Number of Bathrooms:
<select name="bathrooms">
<option selected="selected" value="">Any</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="1.5">1.5</option>
<option value="2">2</option>
<option value="2.5">2.5</option>
<option value="3">3</option>
<option value="3.5">3.5</option>
<option value="4">4</option>
<option value="4.5">4.5</option>
<option value="4.5 +.5">4.5 +.5</option>
<option value="5.5">5.5</option>
<option value="7">7</option>
<option value="7.5">7.5</option>
<option value="12.5">12.5</option>
</select>
</div>
<div class="listbox">
Number of Garages:
<select name="garages">
<option selected="selected" value="">Any</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
</div>
<input name="Search" type="submit" />
</form>and Result Page/Query Page. (note I do have mysql connection page setup and that is working)
<?php require_once('Connections/plansdb.php'); ?>
<?php
//variables from submit form
$houseplans = $plantypes ;
$bedrooms = $bed ;
$bathrooms = $bath ;
$garages = $garage ;
// Query DB
$query = sprintf("SELECT PLANNO, PLANTYPES, DESC, BED, BATH, GARAGE, FLOORS, URL, IMG FROM plans WHERE PLANTYPES='%s' AND BED='%s' AND BATH='%s' AND GARAGE='%s'",
mysql_real_escape_string($plantypes),
mysql_real_escape_string($bed),
mysql_real_escape_string($bath),
mysql_real_escape_string($garage));
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
while ($row = mysql_fetch_assoc($result)) {
echo $row['PLANNO'];
echo $row['PLANTYPES'];
echo $row['DESC'];
echo $row['BED'];
echo $row['BATH'];
echo $row['GARAGE'];
echo $row['FLOORS'];
echo $row['URL'];
echo $row['IMG'];
}
mysql_free_result($result);
?> So - after a good attempt, I'm stuck. I'm thinking I've got my variables all mixed up - but need a tip/direction/solution on how to fix this.
Thanks
Jade
I would recommend changing your form method to 'post' instead of 'get' and then you just need to pull the variables in from the $_POST[] array and you should be good to go. I think you want to do this and change your sql parameters to use those variables.
If you want to stick with GET for your form method, just change the $_POST refs to $_GET.
//variables from submit form $houseplans = $_POST['houseplans']; $bedrooms = $_POST['bedrooms']; $bathrooms = $$_POST['bathrooms']; $garages = $_POST['garages'];
If you want to stick with GET for your form method, just change the $_POST refs to $_GET.
Last edited by Ezzaral : Aug 7th, 2007 at 11:46 am.
•
•
Join Date: Aug 2007
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
I'm getting this error.
I also added a check to make sure I was connected to the DB. Like I said, I've been staring at this for so long - I'm probably blind to the obvious. Once I'm shown how to do it, then I'll be golden.
New Code for the Results page
•
•
•
•
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC, BED, BATH, GARAGE, FLOORS, URL, IMG FROM plans WHERE PLANTYPES='All Types'' at line 1 Whole query: SELECT PLANNO, PLANTYPES, DESC, BED, BATH, GARAGE, FLOORS, URL, IMG FROM plans WHERE PLANTYPES='All Types' AND BED='' AND BATH='' AND GARAGE=''
I also added a check to make sure I was connected to the DB. Like I said, I've been staring at this for so long - I'm probably blind to the obvious. Once I'm shown how to do it, then I'll be golden.
New Code for the Results page
<?php require_once('Connections/plansdb.php');
$db_selected = mysql_select_db( $database_plansdb, $plansdb);
if (!$db_selected) {
die ('Can\'t use' . $database_plansdb .' : ' . mysql_error());
} ?>
<?php
//variables from submit form
$plantypes = $_POST['houseplans'];
$bed = $_POST['bedrooms'];
$bath = $_POST['bathrooms'];
$garage = $_POST['garages'];
// Query DB
$query = sprintf("SELECT PLANNO, PLANTYPES, DESC, BED, BATH, GARAGE, FLOORS, URL, IMG FROM plans WHERE PLANTYPES='%s' AND BED='%s' AND BATH='%s' AND GARAGE='%s'",
mysql_real_escape_string($plantypes),
mysql_real_escape_string($bed),
mysql_real_escape_string($bath),
mysql_real_escape_string($garage));
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
while ($row = mysql_fetch_assoc($result)) {
echo $row['PLANNO'];
echo $row['PLANTYPES'];
echo $row['DESC'];
echo $row['BED'];
echo $row['BATH'];
echo $row['GARAGE'];
echo $row['FLOORS'];
echo $row['URL'];
echo $row['IMG'];
}
mysql_free_result($result);
?> •
•
Join Date: Aug 2007
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
Changed the Database field name from "DESC" to "DESCRIPT"
Changed the = to LIKE
No errors, and nothing shows up, even on source.
So - I'm going to build a little output script for the variables that are being passed. And see if there's a drop there.
Not sure how to handle the "any" case beyond the change from = to LIKE. I tried changing the '%s' to just '%' and got an error like this.
Usually new things go "click" and "solved" in a small amount of time. This is causing my head to implode.
It's not like I'm new to scripting either, I wrote the fist skinning system for Gallery.sf.net (G1). This wee little thing is about to get a sledge hammer to the computer.
Jade
Changed the = to LIKE
No errors, and nothing shows up, even on source.
So - I'm going to build a little output script for the variables that are being passed. And see if there's a drop there.
Not sure how to handle the "any" case beyond the change from = to LIKE. I tried changing the '%s' to just '%' and got an error like this.
•
•
•
•
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ND BATH LIKE '0' AND GARAGE LIKE '0.00000e-1' at line 1 Whole query: SELECT PLANNO, PLANTYPES, DESCRIPT, BED, BATH, GARAGE, FLOORS, URL, IMG FROM plans WHERE PLANTYPES LIKE 'ND BED LIKE 'ND BATH LIKE '0' AND GARAGE LIKE '0.00000e-1
Usually new things go "click" and "solved" in a small amount of time. This is causing my head to implode.
It's not like I'm new to scripting either, I wrote the fist skinning system for Gallery.sf.net (G1). This wee little thing is about to get a sledge hammer to the computer.
Jade
You might change the default 'any' value for houseplans on the form to "" so that they are all consistent and try this . This will set the variable to its selected value if it's not null or '%' for the null case. Then change the query to
The sprintf() function still needs the '%s' for the variable substitution. From the docs, it sounds like the LIKE '%' wildcard should work with numerics, but I have not verified that myself. It it does not, you will have to build the WHERE string dynamically by concatenating those pieces that have a value other than "" (which was "any").
<option selected="selected" value="">All Types</option>
$plantypes = ($_POST['houseplans']) ? $_POST['houseplans']):'%'; $bed = ($_POST['bedrooms']) ? $_POST['bedrooms'] : '%'; $bath = ($_POST['bathrooms']) ? $_POST['bathrooms'] : '%'; $garage = ($_POST['garages']) ? $_POST['garages'] : '%';
$query = sprintf("SELECT PLANNO, PLANTYPES, DESC, BED, BATH, GARAGE, FLOORS, URL, IMG FROM plans WHERE PLANTYPES like '%s' AND BED like '%s' AND BATH like '%s' AND GARAGE like '%s'",
mysql_real_escape_string($plantypes),
mysql_real_escape_string($bed),
mysql_real_escape_string($bath),
mysql_real_escape_string($garage));The sprintf() function still needs the '%s' for the variable substitution. From the docs, it sounds like the LIKE '%' wildcard should work with numerics, but I have not verified that myself. It it does not, you will have to build the WHERE string dynamically by concatenating those pieces that have a value other than "" (which was "any").
Last edited by Ezzaral : Aug 7th, 2007 at 2:31 pm.
•
•
Join Date: Aug 2007
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
You've been really great, unfortunately it's still not outputting. Played with a few things from php.net phpbuilder and off adobe db primer.
I think I'll need to hit the book store tonight and do a little more research. honestly I thought this would be an easy script to do. The client has been waiting for it to go live for a week. I'll pop open my php book - but it's a couple years old and hesitate to use something depreciated.
I think I'll need to hit the book store tonight and do a little more research. honestly I thought this would be an easy script to do. The client has been waiting for it to go live for a week. I'll pop open my php book - but it's a couple years old and hesitate to use something depreciated.
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- PHP Quote form (PHP)
- HELP: created dreamweaver form, but how do i get results from form? (PHP)
- E-mail form data quick and easily with PHP (PHP)
- php login form (PHP)
- php mail form - need to redirect to new page (PHP)
- Showing results of a form in new window (HTML and CSS)
Other Threads in the PHP Forum
- Previous Thread: phpinfo page has array errors. What for?
- Next Thread: Next Link Help!!




Linear Mode