my query is echoing the right amount of results in my selection box but theyre all blank? super confused any help appreciated
<?php
session_start();
$cupsession = $_SESSION['c_cname'];
$teamSet = $mysqli->query("SELECT cup_name FROM teams WHERE cup_name='$cupsession'");
?>
<select name="team-1" required>
<option value='Holder' disabled selected>Team 1</option> <!--Placeholder for Select-->
<?php
while($rows = $teamSet->fetch_assoc()){ //Fills select with cup entries in db
$teamName = $rows['team_name'];
echo " <option value='$teamName'>$teamName</option>";
}
?>
</select>
I presume you're trying to get the team_name from your database not cup_name so it would be:
$teamSet = $mysqli->query("SELECT team_name FROM teams WHERE cup_name='$cupsession'");
I have an query that I am trying to do search with multiple parameters to sort my data response from sql.
The query code :
$sql = "SELECT * FROM `apartments` WHERE `building_num`='{$i}' ORDER by `apartment_num` ASC";
The search form :
<form method="GET">
<input type="hidden" name="page" value="price">
<select class="styled-select2" name="type">
<option value="0">Apartment type</option>
<option value="דופלקס">Duplex</option>
<option value="דירה">Apartments</option>
<option value="דירת גן">דירת גן</option>
</select>
<select class="styled-select2" name="rooms">
<option value="0">rooms</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="5">5</option>
</select>
<select class="styled-select2" name="price">
<option value="0">Price</option>
<option value="700000-800000">700,000-800,000 ₪ </option>
<option value="800000-900000">800,000-900,000 ₪</option>
<option value="900000-1000000">900,000-1,000,000 ₪</option>
<option value="1000000-1100000">1,000,000-1,100,000 ₪</option>
<option value="1100000-1200000">1,100,000-1,200,000 ₪</option>
<option value="1200000-1300000">1,200,000-1,300,000 ₪</option>
<option value="1300000-1400000">1,300,000-1,400,000 ₪</option>
<option value="1400000">1,400,000 up</option>
</select>
<input type="hidden" name="action" value="search">
<button type="submit">Search</button>
</form>
There are three parameters but they are not required, could the customer only use 'apartment type' AND 'Price' OR only one parameter, I tried for few hours to write query with no success, Any one can help ?
// Vars on table apartments
// Rooms - > Rooms
// Price -> price
// Apartment type -> type
1st of all, there's no direct link between and query (or queries) and tge options you're giving to the customer...
If you want to then you can allow the customer to fill some of the fields and if you don't then no... it's a matter of decision, from which derives the development
There's missing example code, so I can obly answer regarding the query and add some remarks
Since you've given specific select options, then you should use it for specific searches
I would also use some validations on the $_POST information returned
Regarding the query, I need to assume that all of the options are saved in the apartments table, under that, then the query should be something like this :
$sql = "SELECT * FROM `apartments` WHERE
rooms = $rooms
OR `type` = $type
OR (price BETWEEN *$range_min* AND *$range_max*)
ORDER by `apartment_num` ASC";
If you want all parameters combined you should use AND instead of OR
and, again, you can decide you want both and use different queries for each scenario the user givea
In this case you have to build dynamic query as below :-
$sql = "SELECT * FROM `apartments` WHERE ";
$condition = "";
$condition .= ($rooms=='')?"":" or 'rooms'= $rooms ";
$condition .= ($type=='')?"":" or 'type'= $type ";
$condition .= ($price=='')?"":" or prince bewteen 'range_min' AND 'range_max'";
$sql .= $condition;
$sql .= " ORDER by `apartment_num` ASC";
In this way you can make all three filters free to select or not.
I'm trying to do price ascending or descending filter in php, but it is not working. Could some one help to fix it? Thanks
<select name='sort'>
<option value='ASC'> Price Low to High </option>
<option value='DESC'> Price High to Low </option>
</select>
<?php
$query = 'SELECT prd_price FROM products ORDER BY '.$_REQUEST['sort'];
$run_query = mysqli_query($con,$query);
$row = mysqli_fetch_array($run_query);
?>
You can't do it like this. Cause your select is not processed.
When you read the php docs about $_REQUEST you see that it only processes the values of $_POST, $_GET and $_COOKIE.
If you would like to make this working, you could do something like this:
<form method="post">
<select name="sort">
<option value="asc">Price Low to High</option>
<option value="desc">Prive High to Low</option>
</select>
<input type="submit" value="Sort">
</form>
<?php
// check if the server recieved a post:
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['sort'])) {
if ($_POST['sort'] == "asc") {
$query = "SELECT prd_price FROM products ORDER BY prd_price ASC";
} else {
$query = "SELECT prd_price FROM products ORDER BY prd_price DESC";
}
}
// execute query here.
}
?>
And try to avoid using $_REQUEST
I'm coding a feature in PHP, it's a search engine.
<form action="result" method="POST">
<select name="instrument" id="instrument">
<option value="all">All instrument</option>
<option value="1">Guitar</option>
<option value="2">Bass</option>
<option value="3">Battery</option>
<option value="4">Singer</option>
</select>
<select name="theme" id="theme">
<option value="all">All themes</option>
<option value="1">Metal</option>
<option value="2">Jazz</option>
<option value="3">Rock</option>
<option value="4">Blues</option>
</select>
<input type="submit" value="search" />
</form>
I already code this search engine like this and it works
<?php
if(isset($_POST)) {
$theme = $_POST['theme'];
$instrument = $_POST['instrument'];
$req_search = $bdd->query("SELECT * FROM user,theme,instrument WHERE theme.theme_id = user.user_theme AND instrument.instrument_id = user.user_instrument AND theme_id =".$theme." AND instrument_id =".$instrument);
if($req_search->rowCount()>0) {
while($search = $req_search->fetch()) {
?>
<p><?php echo $search['user_username']; ?></p>
<?php
}
$req_search->closeCursor();
} else {
echo "Users not found";
}
}
?>
You can find users who is matching with those options (without All instrument and All themes.
Everything is stored into a MySql database.
However when I click on All themes and All instrument. I want to update my select request. Like if I select All instrument and All themes I want to display every users. Or if I select All instrument and Metal, I want to display all users listening to metal and playing whatever instrument.
Thanks for your help !
You can construct your query to be conditional depending on whether $theme and $instrument have values:
$query = "SELECT * FROM user,theme,instrument
WHERE theme.theme_id = user.user_theme
AND instrument.instrument_id = user.user_instrument".
($theme!="" ? " AND theme_id='$theme'" : "").
($instrument!="" ? " AND instrument_id='$instrument'" : "");
$req_search = $bdd->query($query);
That said, you should look into PDO Prepared Statements and Placeholders, because all this would take is someone to fiddle around with the DOM to perform some MySQL Injection.
Hi I want to filter my searches in the sql based on the requirement the user wants so i used a select tag and depending on the value of the select tag a certain sql statement will be used but its not working as expected can you help me looked what went wrong here:
$strFilterByStatus = $_POST['byStatus'];
<select name="byStatus" id="byStatus" onChange="frmAdminWorklist.submit()">
<option value="all">All</option>
<option value="recovered">Recovered</option>
<option value="unrecovered">Unrecovered</option>
</select>
<?php
if($strFilterByStatus=='all'){
$strSQL = "SELECT * FROM tblcase";
$SQL=mysql_query($strSQL);
}
?>
appreciate the help :)
Suppose this is all in theTest.php
<?php
$strFilterByStatus = $_POST['byStatus'];
?>
<form id='testform" action="theTest.php" method="post">
<select name="byStatus" id="byStatus" onChange="frmAdminWorklist.submit()">
<option value="all">All</option>
<option value="recovered">Recovered</option>
<option value="unrecovered">Unrecovered</option>
</select>
<input type='submit' name='' value='Go test it'>
</form>
<?php
if ( $strFilterByStatus == 'all' )
{
$strSQL = "SELECT * FROM tblcase";
$SQL = mysql_query($strSQL);
}
?>