Building Query String off of Form Values
Posted: Thu Mar 26, 2009 3:01 pm
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hello,
I have a drop down form for a user to select different values. Based off of these values, I want to query a table that contains these values. The user may select values in 1,2, or all of the dropdowns. If they have a value in two of the dropdowns, I would query the table for any rows that have those same two values in their fields. If the user doesn't select a value in the dropdown in posts a null value and should not be used in the query. I'm having problems creating my sql query string. Right now...I can't even get it to echo to see if my if statements are working. Any help is appreciated!! Here's my code:
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hello,
I have a drop down form for a user to select different values. Based off of these values, I want to query a table that contains these values. The user may select values in 1,2, or all of the dropdowns. If they have a value in two of the dropdowns, I would query the table for any rows that have those same two values in their fields. If the user doesn't select a value in the dropdown in posts a null value and should not be used in the query. I'm having problems creating my sql query string. Right now...I can't even get it to echo to see if my if statements are working. Any help is appreciated!! Here's my code:
Code: Select all
<?php
$link = mysql_connect('localhost', 'root', 'root');
if (!$link){
die('Could not connect' . mysql_error());
}
echo 'Connected Successfully <br />';
$selectdb = mysql_select_db ('chfa', $link);
if(!$selectdb){
die('Could not select database' . mysql_error());
echo 'Did not select database';
}
echo 'Database Selected <br />';
$name = $_POST["Name"];
$name2 = "false";
$location = $_POST["Location"];
$location2 = "false";
$dept = $_POST["Department"];
$dept2 = "false";
$priority = $_POST["Priority"];
$priority2 = "false";
$status = $_POST["Status"];
$status2 = "false";
if ($name != "null") {
$name2 = "true";
}
if ($location != "null") {
$location2 = "true";
}
if ($dept != "null") {
$dept2 = "true";
}
if ($priority != "null") {
$priority2 = "true";
}
if ($status != "null") {
$status2 = "true";
}
$query = "SELECT * FROM task1 WHERE ";
if ($name2=="true") {
$query = $query . "name='$name'";
}
if ($name2=="true" && location2=="true") {
$query = $query . " AND location='$location'";
}
if ($name2=="false" && location2=="true") {
$query = $query . "location='$location'";
}
if ($name2=="true" || $location2=="true") {
if ($dept2=="true") {
$query = $query . " AND department='$dept'";
}
elseif (dept2=="true" {
$query = $query . "department='$dept'";
}
if ($name2=="true" || $location2=="true" || $dept2=="true") {
if ($priority2=="true") {
$query = $query . " AND priority='$priority'";
}
elseif ($priority2 =="true") {
$query = $query . "priority='$priority'";
}
if ($name2=="true" || $location2=="true" || $dept2=="true" || $priority2=="true") {
if ($status2=="true") {
$query = $query . " AND status='$status'";
}
elseif ($status =="true") {
$query = $query . "status='$status'";
}
$query = $query . ";";
echo $query . "/n";
echo $name;
echo $location;
echo $dept;
echo $priority;
echo $status;
/*if ($name!="null" && $location!="null" && $dept!="null" && $priority!="null" && $status!="null") {
$query = "SELECT * FROM task1 WHERE name='$name' AND location='$location' AND department='$dept' AND priority='$priority' AND openclosedpending='$status';";
}*/
?>pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: