Building Query String off of Form Values

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
srefer
Forum Newbie
Posts: 6
Joined: Thu Mar 12, 2009 12:56 pm

Building Query String off of Form Values

Post by srefer »

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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Building Query String off of Form Values

Post by andyhoneycutt »

Please remember to use code tags around your code so it's formatted nicely :) So when you echo $query, nothing prints to screen? Also, if you could post the html form that goes with this, I'll re-create it on my end and see if I can fix it.

-Andy
max529
Forum Commoner
Posts: 50
Joined: Sat May 19, 2007 4:10 am

Re: Building Query String off of Form Values

Post by max529 »

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: :arrow: Posting Code in the Forums to learn how to do it too.

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';";
}*/
 
?>
there in line 58 , you have missed a closing ')' bracket for elseif ,Dont you have got errorreporting enabled?


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: :arrow: Posting Code in the Forums to learn how to do it too.
Post Reply