Multiple options for query from select menu - mysql

I have a search area which contains 3 field, one is an input field and the other two are drop down menus. Either someone enters something into the input field or they select from BOTH of the drop downs (state and city).
Input search works fine. Select menus do not.
Here is the form.
<form name="ffl_finder" method="get" action="index.php">
Enter a Zip Code to find a Firearms Dealer near you: <br />
<input name="q" type="text" id="q" size="40">
Select a State AND a City<br />
<select name="qoption2">
<option value="" selected="selected">Select . . .</option>
<?php
$sql2 = "SELECT DISTINCT state from ffl_list order by state asc";
$rs_results2 = mysql_query($sql2) or die(mysql_error());
while ($rrows2 = mysql_fetch_array($rs_results2)) {
?>
<option value="<?php echo $rrows2[state]; ?>"><?php echo $rrows2[state]; ?></option>
<?php
}
?>
</select>
<select name="qoption3">
<option value="" selected="selected">Select . . .</option>
<?php
$sql3 = "SELECT DISTINCT city from ffl_list order by city asc";
$rs_results3 = mysql_query($sql3) or die(mysql_error());
while ($rrows3 = mysql_fetch_array($rs_results3)) {
?>
<option value="<?php echo $rrows3[city]; ?>"><?php echo $rrows3[city]; ?></option>
<?php
}
?>
</select>
<input name="doSearch" type="submit" id="doSearch" value="Search">
</form>
Here is the query:
<?php if ($get['doSearch'] == 'Search') {
//$cond2 = '';
if($get['qoption2'] != '') {
$cond2 = "`state` = 'qoption2'";
}
//$cond3 = '';
if($get['qoption3'] != '') {
$cond3 = "`city` = 'qoption3'";
}
if($get['q'] == '') {
$sql = "select * from ffl_list where $cond2 and $cond3 order by state asc, city asc";
}
else {
$sql = "select * from ffl_list where `zip` = '$_REQUEST[q]' ORDER BY `id` asc";
}
$rs_results = mysql_query($sql) or die(mysql_error());
} ?>
I need the query to search both city and state and I cannot seem to get the conditions right. I know this is a lot of code but if someone could help that would be great.
Thanks,
Clint

<?php
while ($rrows2 = mysql_fetch_array($rs_results2))
{
?>
<option value="<?php echo $rrows2['state']; ?>"><?php echo $rrows2['state']; ?></option>
<?php
}
?>
state and citymay be cols' names of the ffl_list table, you have to use a string a key of the returned array from mysql_Fetch_array. You did wrong for $rrows2 and $rrows3
When you create your filter for the query, you set a hard string, not GETted data :
$cond2 = '';
if($_GET['qoption2'] != '') {
$cond2 = "`state` = '".$_GET['qoption2']."'";
}

Related

I'm creating a sign up form for an image upload but when I put the information in it says you have an error am I missing some code?

I created a signup form for an upload image, but when I enter information
in there it says you have an error, but I examined everything and I don't see a coding error. Could it be that I'm missing a good chunk of code? or I hace a coding error I'm missing a semicolon or parentheses because I don't see anything.
<?php
include_once "dbh.php";
$first = $_POST['first'];
$last = $_POST['last'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "INSERT INTO user (first, last, username, password)
VALUES ('$first', '$last', '$uid', '$pwd')";
mysqli_query($conn, $sql);
$sql = "SELECT * FROM user WHERE username='$uid' AND first='$first'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$userid = $row['id'];
$sql = "INSERT INTO profileimg (userid, status)
VALUES ('$userid', 1)";
mysqli_query($conn, $sql);
header("Location: index.php");
}
} else {
echo "You have an error!";
}
?>
Here's The html:
if(isset($_SESSION['id'])) {
if ($_SESSION['id'] == 1) {
echo "You are logged in as user #1";
}
echo "<form action='upload.php' method='POST' enctype='multipart/form-data'>
<input type='file' name='file'>
<button type='submit' name='submit'>UPLOAD</button>
</form>";
} else {
echo "You are not logged in!";
echo "<form action='signup.php' method='POST'>
<input type='text' name='first' placeholder='First name'>
<input type='text' name='last' placeholder='Last name'>
<input type='text' name='uid' placeholder='Username'>
<input type='password' name='pwd' placeholder='Password'>
<button type='submit' name='submitSignup'>Signup</button>
</form>";
}
?>
<p>Login as user!</p>
<form action="login.php" method="POST">
<button type="submit" name="submitLogin">Login</button>
</form>
<p>Logout as user!</p>
<form action="logout.php" method="POST">
<button type="submit" name="submitLogout">Logout</button>
</form>
Screenshot of table structure
You have missed a semicolon at line
$sql = "INSERT INTO user (first, last, username, password)
and also you have put a ` in line
echo "You have an error!";
}`
I have fixed those syntax errors and if you still get some error please provide the html code from which you are taking input and make sure database connection is made properly.
Edited code::
<?php
include_once "dbh.php";
$first = $_POST['first'];
$last = $_POST['last'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "INSERT INTO user (first, last, username, password);
VALUES ('$first', '$last', '$uid', '$pwd')";
mysqli_query($conn, $sql);
$sql = "SELECT * FROM user WHERE username='$uid' AND first='$first'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$userid = $row['id'];
$sql = "INSERT INTO profileimg (userid, status)
VALUES ('$userid', 1)";
mysqli_query($conn, $sql);
header("Location: index.php");
}
} else {
echo "You have an error!";
}
?>

how to keep selected value in select box after submit or page reload were values and option are different which retrieved from db?

I want to keep selected particular value which was selected before the form submission. But after reloading or submisssion i did not get the previous selected value.help me!
<select name="select" class="dropbtn" onchange="this.form.submit()">
<?php
include 'connect.php';
$query="SELECT * FROM `user` WHERE id =1";
$result=mysqli_query($link_id,$query);
while($data = mysqli_fetch_assoc($result))
{ ?>
<option value="<?php echo $data['user_id']; ?>"> <?php echo
$data['address']; ?> </option>
<?php } ?>
</select>
<?php
$id = null;
if(isset($_POST['select']))
{
$id = $_POST['select'];
echo $id;
}
?>

MySql Dropdown filter select with NULL

I'm trying to build up a drop down filter system for a custom table. I have the following code but I can't seem to get it to work such that if one of the drops is not selected it still returns results based on the the selection of the second drop down:
<form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<select name="bt_nbs_intervention">
<option value=''>NbS intervention type</option>
<option value="3">Protection</option>
<option value="143">Restoration</option>
</select>
<select name="climate_change_impacts">
<option value=''>Climate change impacts</option>
<option value="15">Drought</option>
<option value="12">Flood</option>
<option value="25">Mangrove</option>
</select>
<input type='submit' value='Search'>
</form>
<?php
if ($_POST['bt_nbs_intervention'] === '') {
$_POST['bt_nbs_intervention'] = null;
}
if ($_POST['climate_change_impacts'] === '') {
$_POST['climate_change_impacts'] = null;
}
$bt_nbs_intervention = (int)$_POST['bt_nbs_intervention'];
$cc_impact = (int)$_POST['climate_change_impacts'];
$interventions = $wpdb->get_results($wpdb->prepare("
SELECT ID, intervention_post_title
FROM wp_cpt_combined
WHERE (bt_nbs_intervention IS NULL OR bt_nbs_intervention = %d) AND
(climate_change_impacts IS NULL OR climate_change_impacts = %d)",
$bt_nbs_intervention, $cc_impact));
if($interventions)
foreach ( $interventions as $intervention )
{
echo $intervention->intervention_post_title;
}
else {
echo "no results";
}
Essentially what I'm trying to do allow the user to select one or more of the select boxes to produce results. Such that if they only select Intervention type they get results or only Climate change impacts OR if they select both.
Hope that makes sense.
Thanks
D
Can you check the following please:
<form method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<select name="bt_nbs_intervention">
<option value=''>NbS intervention type</option>
<option value="3">Protection</option>
<option value="143">Restoration</option>
</select>
<select name="climate_change_impacts">
<option value=''>Climate change impacts</option>
<option value="15">Drought</option>
<option value="12">Flood</option>
<option value="25">Mangrove</option>
</select>
<input type='submit' value='Search'>
</form>
<?php
$where = [];
$placeholders = [];
if(!empty($_POST['bt_nbs_intervention'])){
$where[] = 'bt_nbs_intervention = %d';
$placeholders[] = (int)$_POST['bt_nbs_intervention'];
}
if(!empty($_POST['climate_change_impacts'])){
$where[] = 'climate_change_impacts = %d';
$placeholders[] = (int)$_POST['climate_change_impacts'];
}
$sql = "SELECT ID, intervention_post_title FROM wp_cpt_combined";
if(!empty($where)){
$sql .= ' WHERE ';
$i = 0;
foreach($where as $clause){
$sql .= $i == 0 ? $clause : ' AND ' . $clause;
$i++;
}
$sql = $wpdb->prepare($sql, $placeholders);
}
$interventions = $wpdb->get_results($sql);

Two action forms and two submit buttons, how to keep the values?

I have 2 action forms in the page and 2 submit buttons.
1 is zoom-level
1 is refresh rate in seconds
When I hit submit of zoom, the refresh rate is reset
When I hit submit of refresh rate, the zoom level is reset
What to do to keep the value of the other when one dropdown is set to a certain value?
I think this is a common problem where noobs struggle with, so it would be nice to have some good explanation from experts with nice guidance here...
code is:
<?php
$submittedValue = "";
$value0 = 1;
$value1 = 1.2;
$value2 = 1.5;
$value3 = 2;
if (isset($_POST["FruitList"])) {
$submittedValue = $_POST["FruitList"];
}
?>
<form action="example3b2vandaag.php" name="fruits" method="post">
<select project="FruitList" id="FruitList" name="FruitList">
<option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo "off"; ?></option>
<option value = "<?php echo $value1; ?>"<?php echo ($value1 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
<option value = "<?php echo $value2; ?>"<?php echo ($value2 == $submittedValue)?" SELECTED":""?>><?php echo $value2; ?></option>
<option value = "<?php echo $value3; ?>"<?php echo ($value3 == $submittedValue)?" SELECTED":""?>><?php echo $value3; ?></option>
</select>
<input type="submit" name="submit" id="submit" value="Set zoom level" />
</form>
<?php
$submittedValue = "";
$value0 = 1000;
$value1 = 20000;
$value2 = 30000;
$value3 = 90000;
if (isset($_POST["FruitList2"])) {
$submittedValue = $_POST["FruitList2"];
}
?>
<form action="example3b2vandaag.php" name="fruits2" method="post">
<select project="FruitList2" id="FruitList2" name="FruitList2">
<option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value0; ?></option>
<option value = "<?php echo $value1; ?>"<?php echo ($value1 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
<option value = "<?php echo $value2; ?>"<?php echo ($value2 == $submittedValue)?" SELECTED":""?>><?php echo $value2; ?></option>
<option value = "<?php echo $value3; ?>"<?php echo ($value3 == $submittedValue)?" SELECTED":""?>><?php echo $value3; ?></option>
</select>
<input type="submit" name="submit" id="submit" value="Set refresh milliseconds" />
</form>
I don't see any reason why you would want two forms, you can put the two select boxes into one form.
And please don't use unintelligent variable names such as $value0, $submittedValue and $Fruitlist, because this isn't about fruits.
Here are the two select boxes into one form:
<?php
$zoom = "";
$zoom0 = 1;
$zoom1 = 1.2;
$zoom2 = 1.5;
$zoom3 = 2;
$refresh = "";
$refresh0 = 1000;
$refresh1 = 20000;
$refresh2 = 30000;
$refresh3 = 90000;
if (isset($_POST["zoom"])) {
$zoom = $_POST["zoom"];
}
if (isset($_POST["refresh"])) {
$refresh = $_POST["refresh"];
}
?>
<form action="testForm.php" name="controlpanel" method="post">
ZoomLevel:<br />
<select project="ControlPanel" id="ControlPanel" name="zoom">
<option value = "<?php echo $zoom0; ?>"<?php echo ($zoom0 == $zoom)?" SELECTED":""?>><?php echo "off"; ?></option>
<option value = "<?php echo $zoom1; ?>"<?php echo ($zoom1 == $zoom)?" SELECTED":""?>><?php echo $zoom1; ?></option>
<option value = "<?php echo $zoom2; ?>"<?php echo ($zoom2 == $zoom)?" SELECTED":""?>><?php echo $zoom2; ?></option>
<option value = "<?php echo $zoom3; ?>"<?php echo ($zoom3 == $zoom)?" SELECTED":""?>><?php echo $zoom3; ?></option>
</select>
<br />
Refresh rate:<br />
<select project="ControlPanel" id="ControlPanel" name="refresh">
<option value = "<?php echo $refresh0; ?>"<?php echo ($refresh0 == $refresh)?" SELECTED":""?>><?php echo $refresh0; ?></option>
<option value = "<?php echo $refresh1; ?>"<?php echo ($refresh1 == $refresh)?" SELECTED":""?>><?php echo $refresh1; ?></option>
<option value = "<?php echo $refresh2; ?>"<?php echo ($refresh2 == $refresh)?" SELECTED":""?>><?php echo $refresh2; ?></option>
<option value = "<?php echo $refresh3; ?>"<?php echo ($refresh3 == $refresh)?" SELECTED":""?>><?php echo $refresh3; ?></option>
</select><br />
<input type="submit" name="submit" id="submit" value="Set configurations" />
</form>
To use the data you get from the form you can use $_POST["zoom"] and $_POST["refresh"], because that's what the name properties of the select boxes are.
See http://www.w3schools.com/php/php_form_complete.asp for more information on html forms and how to use the $_POST data.

Option Value placed outside of quotes

While I am sure I am doing something stupid, after staring at the code for much too long, I am now stumped. I have created a select box and populate it with posts from a certain post type in Wordpress. This works fine except when viewing the source, the option value is being placed outside of the actual value attribute and all boxes are being marked as selected.
It has to be a simple mistake somewhere but if anyone can spot this, it would be greatly appreciated.
<label for="meta-select-providers" class="package-row-title"><?php _e( 'Provider', 'package_textdomain' )?></label>
<select name="meta-select-providers" id="meta-select-providers">
<?
if( $providers->have_posts() )
{
while( $providers->have_posts() )
{
$providers->the_post();
$provider_name = get_the_title();
$provider_id = the_ID();
?>
<option value="<? echo $provider_id; ?>" <?php if (isset ($package_stored_meta['meta-select-providers'])) selected( $package_stored_meta['meta-select-providers'][0], $provider_id ); ?>>
<?php _e( $provider_name, 'package_textdomain' )?></option>
<?
}
}
?>
</select>
Update
<option value="<? the_ID(); ?>" <?php if (isset ($package_stored_meta['meta-select-providers'])) selected( $package_stored_meta['meta-select-providers'][0], the_ID() ); ?>>
<?php _e( $provider_name, 'package_textdomain' )?></option>
Source:
1647
<option value="" selected>
Post 1</option>
1645
<option value="" selected>
Post 2</option>
1643
<option value="" selected>
Post 3</option>
the_ID() displays (echoes) the value. From the docs:
Displays the numeric ID of the current post. This tag must be within The Loop.
Note: This function displays the ID of the post, to return the ID use get_the_ID().
This code will work as expected:
<label for="meta-select-providers" class="package-row-title"><?php _e( 'Provider', 'package_textdomain' )?></label>
<select name="meta-select-providers" id="meta-select-providers">
<?
if( $providers->have_posts() )
{
while( $providers->have_posts() )
{
$providers->the_post();
$provider_name = get_the_title();
$provider_id = get_the_ID();
?>
<option value="<? echo $provider_id; ?>" <?php if (isset ($package_stored_meta['meta-select-providers'])) selected( $package_stored_meta['meta-select-providers'][0], $provider_id ); ?>>
<?php _e( $provider_name, 'package_textdomain' )?></option>
<?
}
}
?>
</select>