Multiple Select MySqL Query Using IN Operator? - php

I have been trying to make this webpage work for a while but keep getting problems. In this webpage, I have a series of selection boxes (some are independent and some are depended on another) to make selections and then apply filter to make a query. It is still in test-mode and working fine for single selections. But I have still not managed to make it work for multiple selections in same selection boxes. For example; When I select Europe and North America in Region Box and apply the filter it gives no result when I'd expect it to give me the results of the companies which are in Europe OR North America. You can find the test webpage here: http://gorevler.awardspace.biz/realdeal04.html
I have been trying to use "implode" and IN operator in .PHP file but don't know where I am doing wrong at. I'd appreciate it if you could you please show me the right way of doing it. You can find the coding below:
PHP
<?php
error_reporting(E_ALL); ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$DB_HOST = "*****"; $DB_USER = "*****"; $DB_PASS = "*****"; $DB_NAME = "*******";
$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); if($con->connect_errno > 0) { die('Connection failed [' . $con->connect_error . ']'); }
$bolge= "'" . implode("', '", $_POST['filtre_region']) . "'" ;
$bolge1= mysqli_real_escape_string($con,$bolge);
$ulke= "'" . implode("', '", $_POST['filtre_country']) . "'";
$ulke1= mysqli_real_escape_string($con,$ulke);
$sektor= "'" . implode("', '", $_POST['filtre_sector']) . "'";
$sektor1= mysqli_real_escape_string($con,$sektor);
$altsektor= "'" . implode("', '", $_POST['filtre_subsector']) . "'";
$altsektor1= mysqli_real_escape_string($con,$altsektor);;
$urun= "'" . implode("', '", $_POST['filtre_product']) . "'";
$urun1= mysqli_real_escape_string($con,$urun);
$sql = mysqli_query("SELECT * FROM gorevler WHERE region IN ('$bolge1') AND country IN ('$ulke1') AND sector IN ('$sektor1') AND sub_sector IN ('$altsektor1') AND product IN ('$urun1')");
echo "<table border='0'>
<tr>
<th>No</th>
<th>Company</th>
<th>Region</th>
<th>Country</th>
<th>Sector</th>
<th>Sub Sector</th>
<th>Product</th>
<th>Website</th>
</tr>";
while ($row = mysqli_fetch_array($sql)){
echo "<td>" . ''.$row['no'] . "</td>";
echo "<td>" . ''.$row['company'] . "</td>";
echo "<td>" . ''.$row['region'] . "</td>";
echo "<td>" . ''.$row['country'] . "</td>";
echo "<td>" . ''.$row['sector'] . "</td>";
echo "<td>" . ''.$row['sub_sector'] . "</td>";
echo "<td>" . ''.$row['product'] . "</td>";
echo "<td>" . ''.$row['website'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
HTML
<form id="filtersForm" action="search_company.php" method="post" target="_blank">
<fieldset id="filtersPane">
<div class="part03_line01" id="part03_line01">
<div class="filter_bolge" id="filtre_bolge"><p>Region:</p>
<select id="filter_region" name="filtre_region[]" class="select_bolge" title="Select a region" multiple="multiple" size="5">
</select>
</div>
<div class="filter_ulke" id="filtre_ulke"><p>Country:</p>
<select id="filter_country" name="filtre_country[]" class="select_ulke" title="Select a country" multiple="multiple" size="5">
</select>
</div>
<div class="filter_sektor" id="filtre_sektor"><p>Sector:</p>
<select id="filter_sector" name="filtre_sector[]" class="select_sektor" title="Select a sector" multiple="multiple" size="5">
</select>
</div>
<div class="filter_altsektor" id="filtre_altsektor"><p>Sub Sector:</p>
<select id="filter_subsector" name="filtre_subsector[]" disabled="disabled" class="select_altsektor" title="Select a sub-sector" multiple="multiple" size="5">
<option value="" data-filter-type="" selected="selected">
-- Make a Choice --</option>
</select>
</div>
<div class="filter_urun" id="filtre_urun"><p>Product:</p>
<select id="filter_product" name="filtre_product[]" disabled="disabled" class="select_urun" title="Select a product" multiple="multiple" size="5">
<option value="" data-filter-type="" selected="selected">
-- Make a Choice --</option>
</select>
</div>
</div>
<div class="part03_line03" id="part03_line03">
<div class="aramadugmesi" id="aramadugmesi"> <button type="submit" id="applyFilterButton">Apply Filters</button>
</div>
</div>
</fieldset>
</form>
JAVASCRIPT
<script>
$(document).ready(function() {
$('#filter_region')
.load('/textdata/region.txt');
$('#filter_country')
.load('/textdata/country.txt');
$('#filter_sector')
.load('/textdata/sector.txt');
$('#filter_sector').change(function() {
$('#filter_subsector').load("textdata/subsector/" + $(this).val() + ".txt",
function(){
$(this).attr('disabled',false);
}
);
});
$('#filter_subsector').change(function(){
$('#filter_product').load(
"textdata/subsector/product/" + $(this).val() + ".txt",
function(){
$(this).attr('disabled',false);
}
);
});
});
</script>
This Php coding is not working for me. It is not giving any results when I click Apply Filter. For example when I select Europe and North America in the selection box and click apply, I want all the companies which are in Europe OR North America to be fetched from database and listed. But it fetches no result. I guess it is a problem with php coding but I don't know whats wrong

You need to get single quotes inside your parentheses like this:
$bolge1 = "'" . implode("', '", $_POST['filtre_region']) . "'";
mysql needs to see something like this:
IN ('value1','value2','value3')
Your explode was just producing this :
IN (value1, value2, value3)
The code above will insert the opening and closing apostrophes and make sure there are also apostrophes between each value.

Related

How to store multiple values from dropdown in php variable

I am trying to get a table to generate based on the sql query in this page, and I have it working with one item in the dropdown menu.
However if I select two, nothing happens, probably because I am either storing the variable incorrectly in $forum or I am accessing it incorrectly in the if $row statement further down.
Please help me understand what to do.
Thanks!
<select name="forums" multiple>
<option disabled selected value> -- Select a forum -- </option>
<?php
$con = mysqli_connect("host","user","pass","db");
if (!$con) {
die('Connection failed: ' . mysqli_connect_error() . '<br>');
}
$productName='
SELECT p.name
FROM product as p
JOIN ownedproducts as o
on o.productID = p.productID
WHERE usersID = 2;
';
$result=mysqli_query($con, $productName);
while ($row = mysqli_fetch_assoc($result)) {
unset($id);
$id = $row['name'];
echo '<option value="'.$id.'">'.$id.'</option>';
}
if(isset($_GET["forums"])){
$forum=$_GET["forums"];
echo "selected forum => ".$forum;
}
?>
</select>
<input type="submit" class = "submit" value="submit" onclick="this.form.submit();" />
</form>
</div>
<hr>
<table class = "table">
<tr id=table-row>
<th id=table-header>Name</th>
<th id=table-header>Company</th>
<th id=table-header>Type</th>
</tr>
<?php
while ($row = $query1->fetch())
{
if ($row['name']== $forum) {
echo "<tr id=" . $row['productID'] . ">";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "</tr>";
}
}
?>```

space in php dropdown box variables

I am trying to retrieve data field from a database and store them in a dropdown box so that they correlate to each other which i have done but when I put them into a dropdown box they appear but with no spaces between them and I want the words separated.
Any help much appreciated.
<form name="FrmAmend" method="post" action="amenddvd2.php">
Select from the list below<br>
<select name="Film" t[enter link description here][1]itle="Select DVD">
<option value="" selected disabled>Select DVD</option>
<?php
$result = mysqli_query($con, "SELECT `DVD ID`, `Title`, `Certificate` FROM tbldvd;");
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['DVD ID'] . $row['Title'] . $row['Certificate'] . "'>" . $row['DVD ID'] . $row['Title'] . $row['Certificate'] . "</option>";
}
?>
</select><br>
<a href="menu.php"</a>Return To Main Menu<br>
<input type="submit" name="Submit" value="Select DVD">
</a>
</form>
It will be better you do it this way
<form name="FrmAmend" method="post" action="amenddvd2.php">
Select from the list below<br>
<select name="Film" t[enter link description here][1]itle="Select DVD">
<option value="" selected disabled>Select DVD</option>
<?php
$result = mysqli_query($con, "SELECT `DVD ID`, `Title`, `Certificate` FROM tbldvd;");
while ($row = mysqli_fetch_assoc($result)) {
?>
<option value=' <?php echo"$row['DVD ID'] $row['Title'] $row['Certificate']" ?> ' selected disabled>Select DVD</option>
<?php
}
?>
</select><br>
<a href="menu.php"</a>Return To Main Menu<br>
<input type="submit" name="Submit" value="Select DVD">
</a>
</form>
concat a space between the vars
echo "<option value='" . $row['DVD ID'] . $row['Title'] . $row['Certificate'] . "'>" . $row['DVD ID'] . ' ' . $row['Title'] . ' ' . $row['Certificate'] . "</option>";

Retrieve Columns from SQL Database Table Based on User Input in HTML Form

I apologize if this has been covered before. I looked at all of the suggestions and nothing seems to fit what I need here.
In any event, I am trying to develop an auto parts bank where the following information is stored in a table called "auto_parts":
Distributor
Address
City
State
Zip
Phone Number
SKU
Part
New or Used
Year
Make / Model
Price
The table looks like this:
I want customers to be able to submit a form that asks them the following questions:
City, State, Part, New or Used, Year, and Make / Model,
which would query my database and output all of the information on the table column that meets the previous criteria:
Query Output:
Distributor
Address
City
State
Zip
Phone Number
SKU
Part
New or Used
Price
Here is an abbreviated version of my form:
<form action="parts_query.php" method="get">
<h2 align="center">Find Auto Part</h2>
<strong>City:</strong>
<br />
<input type="text" name="City" value="City" required>
<br />
<strong>State:</strong>
<br />
<select name="State" size="1" required><option selected="selected">Choose One:
<option>AL
<option>AK
<option>AZ
<option>AR
<option>CA
<option>...
</select>
<br />
<strong>Select Part:</strong>
<br />
<select name="Part" size="1" required><option selected="selected">Choose One:
<option>A Pillar
<option>A/C Compressor
<option>A/C Compressor Clutch Only
<option>A/C Condenser
<option>A/C Condenser Fan
<option>A/C Control Computer
<option>A/C Evaporator
<option>A/C Evaporator Housing only
<option>A/C Heater Control (see also Radio or TV Screen)
<option>A/C Hose
<option>Accelerator Parts
<option>Adaptive Cruise Projector
<option>...
</select>
<br />
<strong>New or Used?</strong>
<br />
<select name="New_Used" size="1"><option selected="selected">Choose One:
<option>New
<option>Used
</select>
<br />
<strong>Vehicle Year:</strong>
<br />
<select name="Year" required><option selected="selected">Choose One:
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
</select>
<br />
<strong>Vehicle Make / Model:</strong><br />
<select name="Make_Model" required><option selected="selected">Choose One:
<option>AMC Ambassador
<option>AMC American
<option>AMC AMX
<option>...
</select>
<br />
<br />
<input class="btn" type="submit" value="Submit">
</form>
I have tried numerous variations of queries to search my database table for the above information submitted via my form, then output all relevant information (as outlined above) to an HTML table, but nothing seems to work. I am still pretty new to working with databases and again I apologize if my question seems redundant or overly broad.
I don't remember how parts_query.php originally looked because I've been changing it a lot, but what I have now looks like this:
<?php
//Connecting to sql db.
$connect = mysqli_connect("localhost","root","","autoparts_bank");
$query = "SELECT * FROM auto_parts WHERE City = [] AND Part = [] AND Year = [] AND Make_Model = []"
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr>
<td>" . $row['Distributor'] . "</td>
<td>" . $row['Address'] . "</td>
<td>" . $row['City'] . "</td>
<td>" . $row['State'] . "</td>
<td>" . $row['Zip'] . "</td>
<td>" . $row['Phone'] . "</td>
<td>" . $row['Part'] . "</td>
<td>" . $row['New_Used'] . "</td>
<td>" . $row['Price'] . "</td>
</tr>"; //$row['index'] the index here is a field name
}
echo "</table>";
mysql_close(); //Make sure to close out the database connection
?>
<?php
//Connecting to sql db.
$connect = mysqli_connect("localhost","root","","autoparts_bank");
//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO `auto_parts` (`Distributor`, `Address`, `City`, `State`, `Zip`, `Phone_No`, `SKU`, `Part`, `New_Used`, `Year`, `Make_Model`, `Price`)
VALUES ('$_POST[Distributor]', '$_POST[Address]', '$_POST[City]', '$_POST[State]', '$_POST[Zip]', '$_POST[Phone_No]', '$_POST[SKU]', '$_POST[Part]', '$_POST[New_Used]', '$_POST[Year]', '$_POST[Make_Model]', '$_POST[Price]')");
?>
To return submitted form data to an external page, simply retrieve the $POST or $GET form variables and then pass them as parameters in SQL query:
<?php
// RETRIEVE FORM DATA (consider using $POST array to avoid url show and character limit)
$city = $_GET["City"];
$state = $_GET["State"];
$part = $_GET["Part"];
$new_used = $_GET["New_Used"];
$year = $_GET["Year"];
$make_model = $_GET["Make_Model"];
// DATABASE CONNECTION
// (consider saving credentials below in external .php file and required in this script)
$servername = "localhost";
$username = "root";
$password = "****";
$dbname = "autoparts_bank";
$conn = new mysqli($servername, $username, $password, $dbname);
// QUERY PREPARATION AND EXECUTION
$query = "SELECT * FROM auto_parts
WHERE City = ? AND Part = ? AND Year = ? AND Make_Model = ?";
$stmt = $conn->prepare($query);
if ($stmt) {
$stmt->bind_param("ssss", $city, $part, $year, $make_model);
$stmt->execute();
$stmt->store_result();
$result = $stmt->get_result();
echo "<table>";
// LOOP THROUGH RESULTS
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>" . $row['Distributor'] . "</td>
<td>" . $row['Address'] . "</td>
<td>" . $row['City'] . "</td>
<td>" . $row['State'] . "</td>
<td>" . $row['Zip'] . "</td>
<td>" . $row['Phone'] . "</td>
<td>" . $row['Part'] . "</td>
<td>" . $row['New_Used'] . "</td>
<td>" . $row['Price'] . "</td>
</tr>";
}
echo "</table>";
$stmt->free_result();
$stmt->close();
} else {
echo "query failed: ".$conn->error;
}
// CLOSE CONNECTION
$conn->close();
?>

Produce a PHP result from a select tag

I am struggling to produce a SQL result. I am trying to display my results in a table from one of the select options.
Any help?
HTML:
<form action="" method="post" name="parkname">
<select id="combobox" name="parkname" class="park-choice">
<option hidden value="C">C</option>
<option class="hidden" value="E">E</option>
<option class="hidden" value="W">W</option>
</select>
<span style="display:inline-block; width: 200px;"></span>
Capacity:
<select id="foo" style="display:inline-block; width: 80px;" id="combobox">
<input type="submit" value="submit">
</select>
</form>
PHP code:
<?php
$selectOption = $_POST['parkname'];
$query = "SELECT * FROM `ROOMS` WHERE `Park` = '$selectOption%';";
$result = mysql_query($query);
echo $result;
if ($result == FALSE) die ("could not execute statement $query<br />");
echo "<table>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['roomCode'] . "</td><td>" . $row['Park'] . "</td><td>" . $row['Capacity'] . "</td><td>" . $row['Style'] . "</td><td>" . $row['dataProjector'] . "</td><td>" . $row['Whiteboard'] . "</td><td>" . $row['OHP'] . "</td><td>" . $row['wheelchairAccess'] . "</td><td>" . $row['lectureCapture'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
First, you need to clear your query from % sign.
$query = "SELECT * FROM `ROOMS` WHERE `Park` = '$selectOption';";
Or if you want to find all matches, you can use LIKE operator:
$query = "SELECT * FROM `ROOMS` WHERE `Park` LIKE '%$selectOption%';";
Second, for displaying html combobox with Park names (in your case), use code like:
<select name="park">
<option value="Park1">Display name</option>
<option value="Park2">Display name</option>
<option value="Park3">Display name</option>
</select>
Park1,Park2,Park3 - Values that would be sent to your php code inside $_REQUEST['park'] variable.
Display name - Just visually display name for options in select

Click a table row to display the data of that chosen row in a new php page

At the moment, I can use a drop down list to retrieve data which is presented on a table. I want to make every row of a table clickable. Once I have clicked on that particular row, I want the data of that row (id, age, state, city, healthcare, team, cause, planned date, from and AAA_diam) to be displayed in a new php page. It should look like this and I hope that I'm being clear enough.
Display of information in a new PHP page once I've clicked on a row of a table.
Id: $id (from the table row)
age: $age
city: $city
healthcare: $healthcare
team: $team
cause: $cause
planned date: $planned date
from: $from
AAA_diam: $AAA_diam
<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<form name="Select_filter3" method="POST" action="index.php">
<select id="dropdown3" name="filter3">
<option value=""></option>
<option value="1">Amount of present</option>
<option value="2">Amount of absent</option>
<option value="3">Amount of present: destination</option>
<option value="4">Antal of absent: destination</option>
<option value="5">Amount A_diam > 3cm</option>
<option value="6">Amount A_diam > 5cm</option>
</select>
<input id="search_box2" type="text" name="search_box3" value="" />
<input id="submit2" type ="submit" name ="search3" value ="Ok">
</body>
</html>
<?php
include "connection1.php";
mysql_query ('SET NAMES UTF8;');
mysql_query ('SET COLLATION_CONNECTION=utf8_general_ci;');
mysql_client_encoding($connect);// where $conn is your connection
if (isset($_POST['search3'])) {
switch($_POST['filter3']) {
Default:
$sql = " ";
break;
case 1:
$sql = "SELECT * FROM mytable WHERE age = '35' AND city != 'Los Angeles' ";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>age</th>
<th>state</th>
<th>city</th>
<th>healthcare</th>
<th>Team</th>
<th>cause</th>
<th>Planned date</th>
<th>From</th>
<th>diameter</th>
</tr>";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['state'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['healthcare'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['cause'] . "</td>";
echo "<td>" . $row[’planned date’] . "</td>";
echo "<td>" . $row['from'] . "</td>";
echo "<td>" . $row['AAA_diam'] . "</td>";
echo "</tr>";
}
echo "</table>";
break;
mysql_close($connect);
?>
you can handle the click event of row using jQuery like
$(document).ready(function(){
$("#your_table_id").delegate("tr.rows", "click", function(){
/*find the clicked row id column and redirect to user for that record
some thing like this http://www.yoursite.com/showrecord.php?id=5
using location.href='http://www.yoursite.com/showrecord.php?id=5';
*/
});
});

Categories