Relational database - send id value in dropdown list - php

Is there a way to send the ID of the new selected value from the dropdown list in edit form?
SQL query
$id = $_GET['edit'];
$result = $polaczenie->query("SELECT idwizyty, data, pracownicy.imie, pracownicy.nazwisko, uslugi.nazwa, klienci.imie, klienci.nazwisko, starttime, endtime, reservedendtime, uslugi.cena FROM Wizyty, Klienci, Uslugi, Pracownicy WHERE wizyty.idpracownika = pracownicy.idpracownika AND wizyty.iduslugi = uslugi.iduslugi AND wizyty.idklienta = klienci.idklienta AND idwizyty=$id")
or die($mysqli->error());
while($row=mysqli_fetch_array($result))
{
$id = $row[0];
$data = $row[1];
$pracownik = $row[2]. ' ' .$row[3];
$usluga = $row[4];
$klient = $row[5]. ' ' .$row[6];
$time = $row[7];
and the dropdown list
<form action="Wizyty-proces.php?ID=<?php echo $id;?>" method="POST"
<label>Pracownik</label>
<?php
$query = "SELECT idpracownika, imie, nazwisko FROM `pracownicy`";
$wynik1 = mysqli_query($polaczenie, $query);
?>
<select name="nowypracownik">
<?php while($row1 = mysqli_fetch_array($wynik1)):
$value = $row1[1] .' '.$row1[2];
$selected = $value == $pracownik ? 'selected="selected"' : ''; ?>
<option <?php echo $selected?>><?php echo $value;?></option>
<?php endwhile; ?>
</select>

What you need to do is pass the id value back as the value attribute of the option. You can do that using by changing this line:
<option <?php echo $selected?>><?php echo $value;?></option>
to
<option <?php echo $selected?> value="<?php echo $row[0]; ?>"><?php echo $value;?></option>

Related

How to set Selected value to dropdown list from database PHP/SQL

I want to set selected values in dropdown list to the one im editing.
$id = $_GET['edit'];
$result = $polaczenie->query("SELECT * FROM wizyty WHERE idwizyty=$id")
or die($mysqli->error());
while($row=mysqli_fetch_assoc($result))
{
$id = $row['idwizyty'];
$data = $row['data'];
$pracownik = $row['pracownik'];
$usluga = $row['usluga'];
$klient = $row['klient'];
$time = $row['starttime'];
$cena = $row['cena'];
}
Dropdown list
<label>Pracownik</label>
<?php
$query = "SELECT imie, nazwisko FROM `pracownicy`";
$wynik1 = mysqli_query($polaczenie, $query);
?>
<select name="nowypracownik" value="<?php echo $pracownik; ?>">
<?php while($row1 = mysqli_fetch_array($wynik1)):;?>
<option selected="<?php echo $pracownik?>"><?php echo $row1[0] .' '. $row1[1] ;?></option>
<?php endwhile; ?>
</select><br><br>
I tried to do it like this but it's not working.
The only value for ‘selected’ attribute is ‘selected’
<option value="value" selected="selected">value</option>
So your code should look something like this
<select name="nowypracownik" value="<?php echo $pracownik; ?>">
<?php while($row1 = mysqli_fetch_array($wynik1));?>
$value = $row1[0] .' '. $row1[1];
$selected = $value == $pracownik ? 'selected="selected"' : '';
<option <?php echo $selected?>><?php echo $value;?></option>
<?php endwhile; ?>
</select><br><br>

Getting the value of select option

I have a dropdown list with 2 option (LOCAL and IMPORT). The option for Import is working and I can get the value and echo the $refnumb (IMCK2016-0000001). But when I select LOCAL I get the same value of $refnumb IMCK2016-0000001 instead of LOCCK2016-0000001. Please help me find what's wrong with my code. Thanks.
<table id='tblselect' class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type: </th>
<!--<tr><td><input type='submit' name='lookup_master' value='COPY' /></td></tr>-->
<?php $shipmenttype = array("0"=>"Please select...", "LOC"=>"LOCAL", "IM"=>"IMPORT");?>
<td><b><select name='shiptype' id='shiptype'>
<?php foreach($shipmenttype as $keyname=>$ship_type):?>
<?php
$selected ="";
$shiptype = $_POST['shiptype'];
if($keyname == $shiptype): $selected =" selected"; endif;
?>
<option value="<?php echo $keyname;?>" <?php echo $selected;?>><?php echo $ship_type;?></option>
<?php endforeach;?>
</select></b></td></tr>
</table>
<br><br>
<?php
$site = dbGetConfig("sitecode");
$dbnextID = dbNextID($keyname);
if($site=="CKI") {
$site="CK".date("Y");
}
if($site=="PQI") {
$site="PQ".date("Y");
}
$refnumb = $keyname.$site."-".str_pad($dbnextID,7,0, STR_PAD_LEFT);
?>
Reference Number:
<input type='text' name='ref_no1' id='ref_no1' value="<?php echo $refnumb ?>" readonly />
Here's the function for dbNextID.
function dbNextID($key) {
$sql1 = "insert into key_master (keyname) values (:keyname)";
$sql2= "update key_master set id = id + 1 where keyname = :keyname";
$sql3 = "select id from key_master where keyname = :keyname";
$conn = dbConnect();
$stmt1 = $conn->prepare($sql1);
$stmt2 = $conn->prepare($sql2);
$stmt3 = $conn->prepare($sql3);
$conn->beginTransaction();
$stmt1->execute(array(':keyname' => $key));
$stmt2->execute(array(':keyname' => $key));
$stmt3->execute(array(':keyname' => $key));
$value = $stmt3->fetchColumn(0);
$conn->commit();
$conn=null;
return $value;
}
Your variable $keyname is getting set inside your for loop. When you are printing out $refnumb, you are referencing $keyname outside the loop. This means that you will get the last element in $shipmenttype is "IM").
To fix this, you will need to track the selected value.
<table id='tblselect' class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type: </th>
<!--<tr><td><input type='submit' name='lookup_master' value='COPY' /></td></tr>-->
<?php $selectedValue = 0; $shipmenttype = array("0"=>"Please select...", "LOC"=>"LOCAL", "IM"=>"IMPORT");?>
<td><b><select name='shiptype' id='shiptype'>
<?php foreach($shipmenttype as $keyname=>$ship_type):?>
<?php
$selected ="";
$shiptype = $_POST['shiptype'];
if($keyname == $shiptype) $selected =" selected";$selectedValue = $keyname; endif;
?>
<option value="<?php echo $keyname;?>" <?php echo $selected;?>><?php echo $ship_type;?></option>
<?php endforeach;?>
</select></b></td></tr>
</table>
<br><br>
<?php
$site = dbGetConfig("sitecode");
$dbnextID = dbNextID($selectedValue);
if($site=="CKI") {
$site="CK".date("Y");
}
if($site=="PQI") {
$site="PQ".date("Y");
}
$refnumb = $selectedValue.$site."-".str_pad($dbnextID,7,0, STR_PAD_LEFT);
?>

PHP Query set combobox for another combobox

how to fix this syntax for this logic ?
i want to select my select option to select the another select option
<?php
$query_string = "SELECT * FROM products";
$query_string1 = "SELECT * FROM suppliers where ProductID = // firstSelectoption(value)";
$query_string2 = "SELECT * FROM categories";
$query = mysql_query($query_string);
$query1 = mysql_query($query_string1);
$query2 = mysql_query($query_string2);
?>
and in the body i make
<select name="first" id="first" onchange="childrenOnChange(this.value)">
<?php
while ($row = mysql_fetch_array($query)) {
echo '<option value=' . $row["ProductID"] . '>';
echo $row['ProductID'];
echo '</option>';
}
?>
</select>
<select name="second" id="second">
<?php
while ($row = mysql_fetch_array($query1)) {
echo '<script>';
echo 'var arr = array(';
$row['SupplierID'] . ',';
echo ')';
echo '</script>';
}
?>
</select>
i want to set the second select option value with $query1;
If you get the value from the query with $val = mysql_fetch_array($query1), then you can include the following in your loop:
$selected = '';
if ($row['ProductID'] == $val) {
$selected = "selected";
}
echo '<option value="'.$row['ProductID'].'" '.$selected.'>'.$row['ProductID'].'</option>';

How to insert data in a dropdown list

We managed to get the drop down list menu however, we are having difficulties getting the data from sql. So far, this is what we got.
<select>
<option id="">--Select jobscope--</option>
<?php
$con=mysqli_connect("host","user","password","database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$getIT = mysqli_query("SELECT job_title FROM `job_details`");
while($viewIT = mysqli_fetch_array($getIT)) {
}
?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
</select>
Shouldn't be like this ? with tag inside WHILE LOOP
while($viewIT = mysql_fetch_array($getIT)) {
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
}
$query = "SELECT * FROM test_groups_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$dd .= "<option value='{$row['group_id']}'>{$row['group_name']}</option>";
}
Try this,
<option value="">--select--</option>
<?php
while($rec = mysql_fetch_assoc($result)) {
?>
<option value="<?=$rec['job_title']?>"><?=$rec['job_title']?></option>
<?php }
}?>
</select>
I am not from php background. Try this.
<?php
$query = "SELECT job_title FROM job_details";
$result = $mysqli->query( $query );
echo '<select id="domain_account" name="domain_account" class="txtBox">';
echo '<option value="">-select-</option>';
while ($row = $result->fetch_assoc()){
?>
<option value="<?php echo $row['job_title']; ?>"><?php echo $row['job_title']; ?></option>
<?php
}
echo "</select>";
?>
Better use PDO or MYSQLi . MYSQL* is depriciated
U need to use that <option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option> line b/w while loop
$getIT = mysql_query("SELECT job_title FROM `job_details`");
while($viewIT = mysql_fetch_array($getIT)) {?>
<option id="<?php echo $viewIT['job_title']?>"<?php echo $viewIT['job_title']?></option>
<?hp }?>

displaying data from a table into a drop down menu

i have 2 tables
domains_info and tb2
i have got the form working well and entering data into the database
here is the top of my page
<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
//include database connection
include 'db_connect.php';
//write query
$query = "insert into domains_info
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."',
domain_account = '".$mysqli->real_escape_string($_POST['domain_account'])."',
renew_date = '".$mysqli->real_escape_string($_POST['renew_date'])."'";
if( $mysqli->query($query) ) {
//if saving success
header("Location:domains.php");
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}
here is the form
<select id="domain_account" name="domain_account" class="txtBox">
<option value="">-select-</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
i tried and changed the top of my page like this
<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
//include database connection
include 'db_connect.php';
//write query
$query = "insert into domains_info
set
domain = '".$mysqli->real_escape_string($_POST['domain'])."',
domain_account = '".$mysqli->real_escape_string($_POST['domain_account'])."',
renew_date = '".$mysqli->real_escape_string($_POST['renew_date'])."'";
if( $mysqli->query($query) ) {
//if saving success
header("Location:domains.php");
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}
$query = "select id, data
from tb2
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'
limit 0,1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$id = $row['id'];
$data = $row['data'];
and updated my form as this
<select id="domain_account" name="domain_account" class="txtBox">
<option value="">-select-</option>
<option value="<?php echo$data; ?>"><?php echo$data; ?></option>
</select>
as you can tell i am very new to this and its not working.
sorry i didnt explain what i was trying to achieve, i am trying to display a drop down form with data from a database.
Try this for populating the dropdown using database:
<?php
$query = "select id, data from tb2";
$result = $mysqli->query( $query );
echo '<select id="domain_account" name="domain_account" class="txtBox">';
echo '<option value="">-select-</option>';
while ($row = $result->fetch_assoc()){
?>
<option value="<?php echo $row['data']; ?>"><?php echo $row['data']; ?></option>
<?php
}
echo "</select>";
?>

Categories