select two values in one option html form php mysql - php

how i can select two values in one form select html?
how explode exactly?
this is my table
id name divition
1 john IT
2 blair ACCOUNTING
3 Fira Finance
i have form like this
<select name="select" id="divi">
<option value="it">IT</option>
<option value="ACCOUNTING.FINANCE">Accounting Finance</option>
and the php process will be like this
if (isset($_POST['submit'])) {
$divi = $_POST['divi'];
$divi = explode(".",$divi);
$sql = "SELECT * FROM table WHERE divition = '$divi'";
i need show data when user select Acc & Finance it will show all name with divition Accounting and finance.
Thank you so much

An option is to loop through the array $divi
like:
if (isset($_POST['submit'])) {
$divi = $_POST['divi'];
$divi = explode(".",$divi);
$sql = "";
foreach($divi as $field) {
$sql = "SELECT * FROM table WHERE divition = '$field'";
}

How can you do that -
Accounting & Finance are in different row.
Instead of that use multiple select option Tag values.

Related

How to retrieve data using select box with PHP?

I have more than 100 rows in an database and they are from different subjects, like Database lessons or Network lessons. I want that the user can choose what questions from those subjects he want to display. So I was thinking in using one select box, but I don't know how to handle the selected box with the database query. How could I do that ?
My code currently :
<div class = "container-fluid">
<div class = "container">
<?php
$query = "SELECT def_conteudo FROM conteudo WHERE nro_conteudo BETWEEN 1 AND 100";
$result = mysqli_query($con,$query);
while($fetch = mysqli_fetch_row($result)){
echo "<li>" . $fetch[0] . "</li>";
}
?>
</div>
</div>
The problem is that he just shows all the lessons from different subjects here, but I want to give the user the choice to choose what questions he want to display.
Example :
If the user want to display only Database lessons he would choose through the select box and would appear at the screen all questions from this particular subject, not all the subjects questions.
The HTML <select> element takes a list of <option> elements:
<select>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
That snippet will produce a drop-down list to pick from the options Option 1 and Option 2 whose values are 1 and 2 respectively: https://jsfiddle.net/ux245gz3/
For your case, every <li> element you're creating should be an <option> inside of a <select> block:
<div class="container-fluid">
<div class="container">
<select id="subject_selection">
<?php
$query = "SELECT def_conteudo FROM conteudo WHERE nro_conteudo BETWEEN 1 AND 100";
$result = mysqli_query($con,$query);
while($fetch = mysqli_fetch_row($result)){
echo "<option value=\"$fetch[0]\">" . $fetch[0] . "</option>";
}
?>
</select>
</div>
</div>
This will produce a list of each of the names of the subjects with the value of the selection set to the displayed name. The MDN article on <select> will have more information about this.
Update to answer the full question:
You'll want to update the query to select a display value and the ID for each subject (assuming the primary key of the 'conteudo' table is called 'id':
SELECT id, def_conteudo FROM conteudo WHERE nro_conteudo BETWEEN 1 AND 100;
Then you can use the id as the value for each option:
<?php
$query = "SELECT id, def_conteudo FROM conteudo WHERE nro_conteudo BETWEEN 1 AND 100";
$result = mysqli_query($con, $query);
while ($fetch = mysqli_fetch_row($result)) {
echo "<option value=\"$fetch[0]\">$fetch[1]</option>";
}
?>
Then, once the user selects an option from the dropdown you can perform a GET to the page and query against the questions or lessons that have the proper relationship to the selected subject via the subject's id submitted by the form.
SELECT * FROM <licao/pergunta> WHERE contuedo_id = $_GET["id"];
As a friendly aside, at this point you should look into using something like PHP's PDO library to automatically handle sanitizing your dynamically generated SQL statements.

SQL query issues: Not all records retrieved from multiple tables in a mysql datatbase

So i have a 3 tables. A match table with records of the two teams playing against each other.
mchID mchTeama mchTeamb mchScorea mchScoreb
1 Cathedral Holy Trinity 3 0
2 St. Andrew's Immanuel
Church 2 0
I also have teams table with records of all teams participating
tmID tmName
1 Cathedral
2 Holy Trinity Lekki
and the players table
plyID plyName plyPosition teamID
1 Michael Defense 2
2 Peter Forward 1
3 Chukwudi Forward 1
4 Johnson Midfield 2
5 John Forward 2
6 Samuel keeper 1
On my html page, i have a form with multiple drop downs that gets populated from the players table based on a series of queries to all three tables based on the match ID passed to the page.
$colname_matchrf = "-1";
if (isset($_GET['mchrf'])) {
$colname_matchrf = $_GET['mchrf'];
}
$query_match = "SELECT * FROM matches WHERE mchID = $colname_matchrf";
$result_match = mysqli_query($connBiscup, $query_match);
$row_match = mysqli_fetch_assoc($result_match);
$totalRows_match = mysqli_num_rows($result_match);
$matchTeamA = $row_match['mchTeama'];
$matchTeamB = $row_match['mchTeamb'];
$query_lineupteama = "SELECT * FROM teams WHERE tmName = '".$matchTeamA. "'";
$result_lineupteama = mysqli_query($connBiscup, $query_lineupteama);
$row_lineupteama = mysqli_fetch_assoc($result_lineupteama);
$totalRows_lineupteama = mysqli_num_rows($result_lineupteama);
$matchTeamaID = $row_lineupteama['tmID'];
$query_playersa = "SELECT * FROM players WHERE teamID = $matchTeamaID AND plyPosition != 'Coach' AND plyPosition != 'Assistant Coach'";
$result_playersa = mysqli_query($connBiscup, $query_playersa);
$row_playersa = mysqli_fetch_assoc($result_playersa);
$totalRows_playersa = mysqli_num_rows($result_playersa);
$query_lineupteamb = "SELECT * FROM teams WHERE tmName = '".$matchTeamB. "'";
$result_lineupteamb = mysqli_query($connBiscup, $query_lineupteamb);
$row_lineupteamb = mysqli_fetch_assoc($result_lineupteamb);
$totalRows_lineupteamb = mysqli_num_rows($result_lineupteamb);
$matchTeambID = $row_lineupteamb['tmID'];
$query_playersb = "SELECT * FROM players WHERE teamID = $matchTeambID AND plyPosition != 'Coach' AND plyPosition != 'Assistant Coach'";
$result_playersb = mysqli_query($connBiscup, $query_playersb);
$row_playersb = mysqli_fetch_assoc($result_playersb);
$totalRows_playersb = mysqli_num_rows($result_playersb);
This is the form drop down sample
<?php
while ($row_playersa = mysqli_fetch_assoc($result_playersa)) {
$playersa[] = $row_playersa;
}
?>
<div class="std_textbox2">
<select name="captaina" class="input-field-login3" id="captaina" tabindex="1">
<option selected="Selected">--Select Player--</option>
<?php foreach ($playersa as $playera): ?>
<option value="<?php echo $playera['plyName']; ?>"><?php echo $playera['plyName']; ?></option>
<?php endforeach ?>
</select>
</div>
So the issue is this, the forms drop down gets populated but not with all the records I expect to be there. For instance the drop down for the team A should have all players from the players table who have their teamID as 1. But what I get is just some of the records not all.
Any help as to whats wrong would be highly appreciated.
You want to use AND in your where statement not &&, && does something else.
$query_playersa = "SELECT * FROM players WHERE teamID = $matchTeamaID and plyPosition != 'Coach' and plyPosition != 'Assistant Coach'";
also change your code to not be vulnerable to injection attacks. Your site will be powned in hours.
Thanks everyone for your help. I have found the issue. In the sql query, I have already called this method $row_playersa = mysqli_fetch_assoc($result_playersa); this has already taken the first row. So calling up that method again in the loop while ($row_playersa = mysqli_fetch_assoc($result_playersa)) {
$playersa[] = $row_playersa;
} takes up the second record and so on. So to solve the issue, i took out the method in the query itself.

Insert selected value from select tag into database

I have a database table with the names and locations of schools. Looks like this
id name location
1 Federal University of tech California
2. Massachusetts Inst of tech Boston
I'm populating a select tag with the names of these schools using this table. And it works fine. Each user is supposed to select his/her school when creating his/her profile. However when the form is submitted, only the first word in the school name gets inserted. e.g for Federal University of tech; only Federal is inserted into the database and for Massachusetts Inst of tech only Massachusetts is inserted - and so on - as opposed to the full name of the school. How do i get the full names of the selected school inserted? The code(page.php):
<?php
//Getting names of schools to populate select tag
$stmt = $db->query("SELECT name from schools order by name ASC");
if($_SERVER['REQUEST_METHOD'] == "POST"){
$major = $_POST['major'];
$university = $_POST['university'];
$email = $_POST['email'];
$stmts = $db->prepare("INSERT INTO profile (university, major email)") VALUES (:university, :major, :email);
$stmts->execute(array(':university' => $university, ':major' => $major, ':email' => $email));
}
?>
//THE HTML FORM
<form action = "page.php" method = "post">
<label>University:
<?php
echo '<select title = "Select one" name= "university"></select>';
echo '<option value="">--please select--</option>';
while ($rows = $stmt->fetch[PDO::FETCH_ASSOC])
echo '<option value = '.$rows['name'].'>'.$rows['name'].'</option>';
echo '</select>';
?>
//other form elements
</label>
</form>
I think the problem lies in the the way you have written value attribute for the option tag.
The suggestion is to put values-with-spaces in double quotes.
Like:
echo '<option value = "Massachusetts Inst of tech">Massachusetts Inst of tech</option>';
But what is getting passed is this because double quotes have been neglected:
echo '<option value = "Massachusetts">Massachusetts Inst of tech</option>';
You might want to check out this line:
echo '<option value = '.$rows['name'].'>'.$rows['name'].'</option>';
and correct with this one:
echo '<option value = "'.$rows['name'].'">'.$rows['name'].'</option>';

How to display specific value from SQL in a dropdown depending on another condition

I need some help for a problem that I have been stuck with for a while.
I have a list with ads showing vehicles of different categories - cars, motorcycles, trucks, boats etc. I have a dropdown that should work as a filter. Depending on what vehicles are in the list, the dropdown values should be equal to the category of the vehicle.
For example, if in the list there are 3 cars: Audi, BMW and Porsche and 2 boats: Cranchi and Azimut, the dropdown should show 5 values: Audi, BMW, Porsche, Cranchi, Azimut.
However, for the example above i only manage to show in the dropdown all car and boat makes from the DB, but not only the relevant to the list entries.
Here is my code, any ideas will be appreciated:
<option value="">Please Select</option>
<?php
if($all=='ALL')
{
foreach($arrVehicleType as $arrVehicleTypes){
$qry_makes_all = "SELECT * FROM ".$tblprefix."makes WHERE status ='1' AND type = '".$arrVehicleTypes."' ORDER BY title ASC";
$rs_makes_all = $db->Execute($qry_makes_all);
while(!$rs_makes_all->EOF){
$sql_query_all_all = "SELECT type FROM ".$tblprefix."vehicles WHERE user_dealer_id = '".$DealeId."' AND make='".$rs_makes_all->fields['id']."' AND user_dealer_type = 2 AND completed_status = 1 AND status = 1 ";
$rs_results_all_all = $db -> Execute($sql_query_all_all);
$TYPE = $rs_results_all_all -> fields['type'];
?>
<option value="<?php echo $rs_makes_all->fields['id'];?>,<?php echo $rs_makes_all->fields['title']." ($vehicletext)";?>
</option>
<?php $rs_makes_all->MoveNext(); } }} else {
while(!$rs_makes->EOF){?>
<option value="<?php echo $rs_makes->fields['id'];?>,,<?php echo $arrVehicleType[0];?>">
<?php echo $rs_makes->fields['title'];?>
</option>
<?php
$rs_makes->MoveNext(); } } ?>
</select>
Use DISTINCT in Mysql.
$qry_makes_all = "SELECT DISTINCT /* list column names */ FROM ".$tblprefix."makes WHERE status ='1' AND type = '".$arrVehicleTypes."' ORDER BY title ASC";
This will select all the distinct values from your table.

MYSQL insert form- insert 1 of 3 possible options from dependent select boxes

I have been searching for an answer to this for about an hour and can't find what I am looking for...
I have 3 dependent select boxes on a INSERT form.(drop_1, drop_2, drop_3) Each of their values (int) represent the same field in the database called category.
I am looking for a way to choose only the highest value of the three as the value that gets inserted into the category field.
Here is what I have been trying (obviously doesn't work):
$drop_1 = ($_POST['drop_1']);
$drop_2 = ($_POST['drop_2']);
$drop_3 = ($_POST['drop_3']);
$category = max($drop_1, $drop_2, $drop_3);
This works only if I select an option from each of the 3 boxes, but if I select an option from only 1 or 2 of the boxes the form gets submitted to the DB with blank values.
your php seems fine:
<?php
$_POST['drop_1'] = 100;
$_POST['drop_2'] = null;
$drop_1 = ($_POST['drop_1']);
$drop_2 = ($_POST['drop_2']);
$drop_3 = ($_POST['drop_3']);
$category = max($drop_1, $drop_2, $drop_3);
echo $category;
would output:
100
so, problem is with the form.
$drop_1 = mysql_real_escape_string($_POST['drop_1']);
$drop_2 = mysql_real_escape_string($_POST['drop_2']);
$drop_3 = mysql_real_escape_string($_POST['drop_3']);
$query = "INSERT INTO table1 (category)
SELECT GREATEST('$drop_1', '$drop_2', '$drop_3') as new_cat ";

Categories