Selecting value from database using where condition like below - php

<select name="sample" class="form-control" id='select'>
<?php
$sam1="attingal"; $sam2="kollam";
$getstd="SELECT stdid,firstname,lastname FROM student WHERE centre='$sam1' AND centre='$sam2' ORDER BY firstname ASC";
$qs=$conn->query($getstd);
$qs->setFetchMode(PDO::FETCH_NUM);
while($getstd=$qs->fetch()) {
?>
<option value='<?php echo $getstd[0]; ?>'>
<?php echo $getstd[1]." ".$getstd[2]; ?>
</option>
<?php } ?>
</select>
Nothing is displaying when I run the above code

Your problem is here:
WHERE centre='$sam1' AND centre='$sam2'
That condition can never be true. a field can't hold 2 different values at once.
You supposedly wanted OR, so change it to
WHERE centre='$sam1' OR centre='$sam2'

Related

Select value from the dropdown in which the data are taken from database

I need to post the value from the drop down, in which the dropdown contains items which are retrieved from the database. To display the item of the table I use echo in the option. But then, I need to get that value of item selected to be updated in the database. As be seen below, I've tried the code which (surely) will not work. How is it possible to get the the value of selected item?
Your suggestion will be appreciated.
Thank you.
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option name="user_name"><?php echo $hasil['user_name'] ?></option>
<option name="user_name" value="<?php echo $hasil['user_name'] ?>" hidden></option><?php }?>
</select>
<select name="user" class="form-control" id="user">
<?php while ($hasil = mysqli_fetch_array($result2)){ ?>
<option><?php echo $hasil['user_name'] ?></option>
<?php } ?>
</select>
This code solves your problem, Check at your side and let me know in case of any issue.
We need to give name only once in select tag and no need to add a extra option only for value.
You must fill your dropdown from base table after that you must check value or id from second table that stored in database. If id or value is equal you must set selected='selected' in your option element
Hope it is help you to resolve you problem
" >
" >
" >
*

retrieve value from drop down bar and display result

on calculatePC.php, I have this code to display the finish_product
Select product:
<select class="itemTypes">
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
Let's say I have chosen Table as the finish_product.
On docalculate.php, I would like to display what I've chosen based on the dropdown list I've selected.
I tried this but there is error.
<?php echo $_POST['finish_product'] ?>
May I know how to display the result?
This doesn't exist:
$_POST['finish_product']
because you don't have a form element named "finish_product" in your markup. Add that name to the form element:
<select name="finish_product" class="itemTypes">
You need to do two things:-
Create a form before select and give it an action
give name attribute to your select box, then only you can get data in $_POST
So do like below:-
<form method="POST" action = "docalculate.php"> // give the php file paht in action
<select class="itemTypes" name="finish_product"> // give name attribute to your select box
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
<input type ="submit" name="submit" value ="Submit">
</form>
Now on docalculate.php:-
<?php
echo "<pre/>";print_r($_POST['finish_product']); // to see value comes or not
Note:- through Jquery its also possible. Thanks

Posting php form with variable contents

I have the below code. It's a function creating a from containing a dropdown menu and a selectionbox, and is being called from a different page).
<?php
require_once 'forbindtilDB.php';
function populerDropdownsOpretProgram()
{
global $mysqliOOP;
$stmt = $mysqliOOP->prepare("SELECT DISTINCT primaer FROM oevelser ORDER BY primaer ASC");
?>
<form action="temp.php" method="post">
<select multiple>
<option value="0">Vælg ønskede øvelse(r)</option>
<?php
$stmt->execute();
$stmt->bind_result($primaereMuskelgruppe);
while ($stmt->fetch())
{
?>
<option value = "<?php echo $primaereMuskelgruppe;?>" >
<?php echo $primaereMuskelgruppe;
?>
</option>
<?php
}
$stmt->close();
?>
</select>
<?php
$stmt = $mysqliOOP->prepare("SELECT antalreps FROM antalreps ORDER BY antalreps ASC");
?>
<select>
<option value="0">Vælg ønskede antal gentagelser</option>
<?php
$stmt->execute();
$stmt->bind_result($antalreps);
while ($stmt->fetch())
{
?>
<option value = "<?php echo $antalreps;?>" >
<?php echo $antalreps;
?>
</option>
<?php
}
$stmt->close();
?>
</select>
<input type="submit">
</form>
<?php
}
I want to post the user input on a different page (currently temp.php), but I don't know how to handle the fact that the form contents is variables fetched by a mysqli call.
So far I've tried different versions of the below on the temp.php-page
<?php
echo $_POST[$antalreps];
?>
<?php
echo $_POST[$primaereMuskelgruppe];
?>
But I'm getting errors stating that there is undefined variables (antalreps and primaeremuskelgruppe) and undefined indexes...
Also, there's the added complexity that the selection box may return more than one result.
Pretty sure echo $_POST[$antalreps]; etc. is the wrong way to go about this, but I haven't been able to figure out alternatives...
Any hints?
You should add 'name' to your select and then use it.
For example instead of:
<select>
you should have:
<select name="yourname">
and then in php to display its value use:
echo $_POST['yourname'];
and for select multiple you can use:
<select name="othername[]">
and then in PHP use it as array:
foreach ($_POST['othername'] as $item) {
echo $item;
}
of course where I put yourname and othername you should put descriptive name such as colours for color box and similar

Require help to show selected list items in php/html form based on existing MySQL data

Any help would be greatly appreciated!!
I am building a php form to update a MySQL database linked to my website.
Part of my form includes list fields where the user can hold ctrl to select multiple values and these are then passed into one MySQL field as comma separated values. See below for three real examples of the data:
3014
3014,3015,3026
3026,3028
The form is updating my database as required, however when the user goes back in to the form to do another update the multiple values they chose aren't pre-selected.
Can anyone help me to figure out why $row_editme['NewsTypeID'] is the field containing the string of values. The field type is a SET.
<td nowrap="nowrap" align="right">NewsTypeID:</td>
<td>
<select name="NewsTypeID[]" size="4" multiple="multiple">
<?php
do {
?>
<option value="<?php echo $row_types['id']?>" <?php if (!(strcmp($row_types['id'], htmlentities($row_editme['NewsTypeID'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_types['name']?></option>
<?php } while ($row_types = mysql_fetch_assoc($types)); ?>
</select>
</td>
Try something like this,
<select name="NewsTypeID[]" size="4" multiple="multiple">
<?php
do {
$selected = htmlentities($row_editme['NewsTypeID'], ENT_COMPAT, 'utf-8');
$selected_arr = explode(",",$selected);
?>
<option value="<?php echo $row_types['id']?>" <?php if (in_array($row_types['id'],$selected_arr)) {echo "selected";} ?>>
<?php echo $row_types['name']?>
</option>
<?php } while ($row_types = mysql_fetch_assoc($types)); ?>
</select>
I think it is not supposed to be selected.
Can you try checked?
<input type="checkbox" <?php /*if condition is met here, then*/ echo "checked='checked'" ?> />
It will pre-select all the values the user selected.

how to store selected value of a drop down list in to a session variable in php

currently i am working on a system. here i have a drop down list and its values are populating from database. now what i need is when ever some one selects a record from the list the selected value should be get stored in a session variable which ll get displayed in an another page. can we do so. i am coding in php.
my code is:
<td align="center">
<?php
$sql_dep=" Select * from places_tbl";
$row_dep = $db->Execute($sql_dep);
$total_dep = $row_dep->RecordCount();
?>
<select name="place" class="txtbx08" id="place">
<option value="">--Please Select City--</option>
<?php if($total_dep>0) {
while(!$row_dep->EOF)
{
?>
<option value="<?php echo $row_dep->fields["place_id"];?>">
<?php echo ucfirst($row_dep->fields["place_name"]); ?>
</option>
<?php
$row_dep->MoveNext();
}}
?>
</select></td>
I'm not sure how you're retrieving the selected value (assumingly through POST), but the normal procedure would be:
session_start();
$_SESSION['place'] = $_POST['place'];

Categories