I am trying to fetch some data from my db to a dropdown. But it does not work. This is my code. Thanks
<div class="col-md-5">
<select multiple class="form-control" style="height: 100px" id="experienceEdit">
<?php
$sqlVessel = "SELECT * FROM vessel_types ORDER BY vessel_types ASC";
$resultVessel = $conn->query($sqlVessel);
if ($resultVessel->num_rows > 0) {
// output data of each row
while ($rowVessel = $resultVessel->fetch_assoc()) {
?>
<option class="experienceValEdit" value="<?php echo $rowVessel['idvessel_types']; ?>"><?php echo $rowVessel['vessel_types']; ?></option>
<?php
}
} else {
}
?>
Check this line.
if ($resultVessel->num_rows > 0) {
There should be a -
I think this should generate some erros. Isnt it?#usinshif
Related
In need some help on this.
I have a form with a select who works perfectly and add all the values selected in my database. But now I want to do my update form, and I want that when I click to modify a user and arrive on the form, the values are already selected.
For example if in my first form, if I had selected the values 1 and 2, I would like 1 and 2 to be already selected when i go to the modification form. I've already did that :
<?php
$nomLogiciel = getDemandeNomLogiciel($id_demande)[0];
$nom = explode(", ", $nomLogiciel);
foreach ($nom as $item) {
$profilsTmp = getProfilByLogiciel($item); ?>
<div class="form-floating mb-3">
<select class="form-control selectpicker" id="id_profil" name="id_profil[]" multiple data-live-search="true">
<?php foreach ($profilsTmp as $item2) { ?>
<option value="<?php echo $item2['id_profil']; ?>"><?php echo $item2['profil']; ?></option>
<?php } ?>
</select>
<label for="id_profil">Profil <?php echo $item ?></label>
</div>
<?php } ?>
I already managed to do it with a simple select like this :
<select class="form-control selectpicker" id="nomPole" name="nomPole">
<option value="Ambulatoire" <?php if ($one['pole'] == "Ambulatoire") { echo ' selected="selected"'; } ?>>Ambulatoire</option>
<option value="Habitat et Vie Sociale" <?php if ($one['pole'] == "Habitat et Vie Sociale") { echo ' selected="selected"'; } ?>>Habitat et Vie Sociale</option>
</select>
But I would like to know how to do it with multiple values in a foreach ?
If you need my SQL function :
function getDemandeNomLogiciel($id_demande) {
global $bd;
$stmt = $bd->prepare('SELECT nom_logiciel FROM demandes WHERE id_demande = :id_demande');
$stmt->bindParam(ID_DEMANDE, $id_demande);
$stmt->execute();
return $stmt->fetch();
}
function getProfilByLogiciel($nomLogiciel) {
global $bd;
$stmt = $bd->prepare('SELECT * FROM profils_logiciels WHERE nom_logiciel = :nomLogiciel');
$stmt->bindParam(NOM_LOGICIEL, $nomLogiciel);
$stmt->execute();
return $stmt->fetchAll();
}
Thanks for your help !
I tried this and it worked very well. I just check if the values of my previous request can be found in the database. If yes I select them :
<?php
foreach ($structures as $item) { ?>
<option value="<?php echo $item['id_structure']; ?>"
<?php if ($item['id_structure'] == $structurePrincipaleID['id_structure']) { echo ' selected="selected"'; } ?>>
<?php echo $item['nom_structure'] ?></option>
<?php } ?>
I would like to ask on how do I populate my dropdown/<select> via retrieving my data in MySQL database. I am using procedural prepared statement to add a security or avoid SQL injection.
Problem: It's only retrieving one data from my database which is I do have a two data stored in my table and how do I insert it through my <option> tag? I'm currently doing right now is first retrieve all research_title.
index.php
<form action="#" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="LabelTitle">Research Title</label>
<?php
include 'includes/includes_getOptionList.php';
?>
</div>
includes_getOptionList.php
<?php
// Connection in DB is stored here
include 'connection_operation.php';
$query = "SELECT research_title FROM tbl_topicresearch";
$smtmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($smtmt, $query)) {
echo "SQL Statement" . mysqli_stmt_error($smtmt);
} else {
mysqli_stmt_execute($smtmt);
$getResult = mysqli_stmt_get_result($smtmt);
if ($getResult) {
?>
<select class="form-control" name="research_title" id="research_title">
<?php
while ($row = mysqli_fetch_assoc($getResult)) {
$title_research = $row['research_title'];
echo $title_research;
echo '<option value="<?php echo $row[research_title]; ?>"> <?php echo $row[research_title]; ?> </option>';
?>
</select>
<?php
}
} else {
}
}
?>
The </select> close tag should be outside the while loop.
You must not include PHP inside PHP (ex: echo '<?php ... ?>';).
You have an extra call (echo $title_research;).
Code:
if ($getResult)
{
echo '<select class="form-control" name="research_title" id="research_title">';
while ($row = mysqli_fetch_assoc($getResult))
{
$title_research = $row['research_title'];
echo '<option value="' . $title_research . '">' . $title_research. '</option>';
}
echo '</select>';
}
else
{
echo '<p>no results</p>';
}
Or:
<?php if ($getResult) : ?>
<select class="form-control" name="research_title" id="research_title">
<?php while ($row = mysqli_fetch_assoc($getResult)): ?>
<option value="<?php echo $row['research_title'] ?>">
<?php $row['research_title'] ?>
</option>
<?php endwhile; ?>
</select>
<?php else: ?>
<p>no results</p>
<?php endif; ?>
I want to echo the selected value from the database to update it then store it
for example I have an asset with category printers from table category which contains other categories and when I want to edit this asset on the edit page I should get a dropdown list contains all the categories and selected on printers then if I want to change it I will if not leave unchanged
The array is drop-down from table category inner joined with user_asset table in the database by asset_category as a foreign key
this is what I have done so far
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?>
</option>
<?php }?>
</select>
</div>
You can add if check if ($row['cate_name'] == 'computer') { ?> and then add selected to this option:
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required >
<?php while( $row = mysqli_fetch_array($result)) {
if ($row['cate_name'] == 'computer') { ?>
<option value="<?php echo $row['category_id'];?>" selected><?php echo $row['cate_name'];?></option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>"><?php echo $row['cate_name'];?></option>
<?php }
}?>
</select>
Notice: If you have multiple elements with that category it will select the last one.
the answer is very simple.. let's put this code
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {
if($row['cate_name']== printers) { ?>
<option value="<?php echo $row['category_id'];?>" selected="selected">
<?php echo $row['cate_name'];?> </option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?> </option>
<?php }?>
</select>
</div>
The logic is that using while loop, checking the condition using if class, and when it satisfies make it as selected. Then it will be echo as selected Value.
i already made a box in which skills are multiselected if we type something in text box,but i want to add those skills which are not inside my database that user type.
my question is how i make that?
my code is-
<span class="pf-title">skills</span>
<div class="pf-field no-margin">
<select id="lstFruits"multiple="multiple"class="chosen"name="skill[]"
data-placeholder="Please Select Skill">
<?php
$fetch="SELECT * FROM `skill`";
$record=mysqli_query($conn,$fetch);
while($records_row=mysqli_fetch_array($record))
{?>
<option value="<?php echo $records_row['skill_name'];?>"<?
php foreach($sk as $s){if($records_row['skill_name'] == $s)
{ echo 'selected="selected"';}}?>><?php echo
$records_row['skill_name'];?></option>
<?php }
?>
</select>
Try this way:-
<select id="lstFruits" multiple="multiple" class="chosen" name="skill[]" data-placeholder="Please Select Skill">
<?php
$fetch="SELECT * FROM `skill`";
$record=mysqli_query($conn,$fetch);
$flag = false;
while($records_row = mysqli_fetch_array($record)){
foreach($sk as $s){
if($records_row['skill_name'] == $s) {
echo '<option value="'.$records_row['skill_name'].'" selected="selected">'.$records_row['skill_name'].'</option>';
$flag = true;
break;
}
}
if(!$flag) {
echo '<option value="'.$records_row['skill_name'].'">'.$records_row['skill_name'].'</option>';
}
$flag = false;
}
?>
</select>
I'm sorry for such a basic question but I'm a bit stumped. I've been trying to build a basic website for a database I created that will graph the data. There are two variable lists that the user selects from and the data from this then is supposed to generate the requested information. However, I'm completely lost as to how to get the data selected from the list to appear in the results page. It will search under the 'country' variable, but not goods. I know that I'm probably doing something stupid, but I'm not sure what it is.
The code I'm using for the dropdown menus on the forms is as follows:
<form id="Country" name="Country" method="get" form action="/database/results_page.php"><table border="1">
<tr>
<td width="68">Name</td>
<td width="48"><span id="sprytextfield4">
<select name="selcountry" id="selcountry" title="<?php echo $row_rsCountrydropdown['']; ?>">
<?php
do {
?>
<option value="<?php echo $row_rsCountrydropdown['country']?>" <?php if($varcountry_rsexportsearch == $row_rsCountrydropdown['country']){echo 'selected';}?>><?php echo $row_rsCountrydropdown['country']?></option>
<?php
} while ($row_rsCountrydropdown = mysql_fetch_assoc($rsCountrydropdown));
$rows = mysql_num_rows($rsCountrydropdown);
if($rows > 0) {
mysql_data_seek($rsCountrydropdown, 0);
$row_rsCountrydropdown = mysql_fetch_assoc($rsCountrydropdown);
}
?>
</select>
<select name="selgoods" id="selgoods" title="<?php echo $row_rsGoodsdropdown['']; ?>">
<?php
do {
?>
<option value="<?php echo $row_rsGoodsdropdown['name']?>" <?php if($vargoods_rsexportsearch == $row_rsGoodsdropdown['name']){echo 'selected';}?>><?php echo $row_rsGoodsdropdown['name']?></option>
<?php
} while ($row_rsGoodsdropdown = mysql_fetch_assoc($rsGoodsdropdown));
$rows = mysql_num_rows($rsGoodsdropdown);
if($rows > 0) {
mysql_data_seek($rsGoodsdropdown, 0);
$row_rsGoodsdropdown = mysql_fetch_assoc($rsGoodsdropdown);
}
?>
</select>
</tr>
</table>
<input type="submit" name="submit" id="submit" value="Submit" onChange="row_rsCountrydropdown.submit()" />
</form>
and my SQL for the results page is as follows:
mysql_select_db($database_cork_normalised, $cork_normalised);
$query_query = "SELECT exports.trade_year, country_id.country, goods.name, exports.Cork FROM country_id, goods, exports WHERE country_id.country_id='varcountry' and goods.goods_id='vargoods'";
$query = mysql_query($query_query, $cork_normalised) or die(mysql_error());
$row_query = mysql_fetch_assoc($query);
$maxRows_query = 10;
$pageNum_query = 0;
if (isset($_GET['pageNum_query'])) {
$pageNum_query = $_GET['pageNum_query'];
}
$startRow_query = $pageNum_query * $maxRows_query;
$vargoods_query = "-1";
if (isset($_POST['selgoods'])) {
$vargoods_query = $_POST['selgoods'];
}
$varcountry_query = "-1";
if (isset($_POST['selcountry'])) {
$varcountry_query = $_POST['selcountry'];
}
If anyone could help I'd be really grateful, this is my first foray into PHP and I'm a bit lost in it.