Select field and retrieve corresponding id - php

I'm searching a way to retrieve an ID corresponding to a 'select' without execute any other query when I select an item from Database:
I use the select item in a form.
Here is the way I select some names from a table from Database:
$sql = "SELECT ID, Name,Surname FROM Table;";
$result = mysql_query($sql);
if(!$result) die ('Unable to run query:'.mysql_error());
$la = "<SELECT name='names'>";
$la .= "<OPTION selected='selected' disabled='disabled' >Choose a name</OPTION>";
while(list($id, $name) = mysql_fetch_row($result)) {
$selectnames .= "<OPTION >$name</OPTION>";
}
$selectnames .= "</SELECT>";
I want to know the ID corresponding to the '$selectnames' I select from a form,
Thanks!

You need to set the value of the option to $id:
while(list($id, $name) = mysql_fetch_row($result)) {
$selectnames .= "<OPTION value='$id'>$name</OPTION>";
}
And then when the form is posted you can check $_POST['names'] to get the ID. As you might have noticed, if you don't specify the value then $_POST['name'] will contain the $name value rather than the $id value.

Related

Displaying mysqli_fetch array results in dropdown php

I have a drop down that is being populated from a static select. Then when a choice is made in the first drop down a query runs and the second drop down is populated from the database depending on selection in first select box. Here is the code. I'm having a problem displaying the second drop down with the data.
$selectvalue = mysqli_real_escape_string($mysqli, $_GET['selectvalue']);
$result = mysqli_query($mysqli, "SELECT DISTINCT '$selectvalue' FROM accounts ");
echo '<option value="">Please select...</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row['$selectvalue'].'">' . $row['$selectvalue'] . "</option>";
//echo $row['drink_type'] ."<br/>";
}
mysqli_free_result($result);
mysqli_close($connection);
?>
<?php
$selectvalue = mysqli_real_escape_string($mysqli, $_GET['selectvalue']);
$result = mysqli_query($mysqli, "SELECT DISTINCT * FROM accounts WHERE col_name = '".$selectvalue."' ");
echo '
<select name="some_name">
<option value="">Please select...</option>';
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['col_name'].'">'.$row['col_name']."</option>";
}
mysqli_free_result($result);
mysqli_close($connection);
?>

how to insert a php variable in a sql query?

now i have two drop down lists, one is for name and the other is time. I want when the user clicks on the desired name in the first dropdown box, the time displayed in the second drop box should belong to the name choosed in the first dropdown box.
$con=mysqli_connect("localhost","root","123","fyp");
$query1 = mysqli_query($con,"SELECT fname FROM lecturer");
echo "Select lecturer:<select name= 'fname'>";
$name = 'fname';
$name = mysql_real_escape_string($name);
while($row=mysqli_fetch_array($query1))
{
echo "<option value='". $row['fname']."'>".$row['fname']. '</option>';
}
echo '</select>';
$con=mysqli_connect("localhost","root","123","fyp");
$query3 = mysqli_query($con,"SELECT stime FROM studbooking WHERE lecname is '$name'");
echo "Select Booking time:<select name= 'stime'>";
while($row=mysqli_fetch_array($query3))
{
echo "<option value='". $row['stime']."'>".$row['stime']. '</option>';
}
echo '</select>';
how to use php variables in sql query?
You can use mysqli->bind_param for this.
mysqli::bind_param
Example
$statement = $mysqli->prepare("SELECT stime FROM studbooking WHERE lecname=?");
$statement->bind_param('s', $name);
$statement->execute();
$result = $statement->get_result();
while ($row = $result->fetch_assoc()) {
print_r($row);
}
Change:
$name = mysql_real_escape_string($name);
to:
$name = mysqli_real_escape_string($con, $name);
You can't use mysql functions with a mysqli connection, and vice versa.

Why is my drop down list not populating with the table data?

WHy is my drop down list not populating with the table data? (dropdown box is empty)
And what is used to display data upon selection of an item in that drop down - is it a "VIEW" (please do provide a study link so I can learn)
My Code
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
//check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
else
{
echo "Connected to mySQL</br>";
}
//$query = 'SELECT FirstName FROM persons';
//$result = mysqli_query($con,$query);
$query = mysqli_query($con,"SELECT 'FirstName' FROM persons");
//print_r($query);
//echo '<select name="FirstName">';
echo "<select name= 'FirstName'>";
//while($row=mysqli_fetch_array($result))
while($row=mysqli_fetch_array($query))
{
echo $row;
//echo "<option value='".$row['FirstName']."'>".'</option>';
}
echo '</select>';
?>
You had 2 errors:
I pointed the first in the comment: to print an option you must use this code:
echo "<option value='". $row['FirstName']."'>".$row['FirstName']
. '</option>';
The second is in your SQL: you are not selecting the FirstName field from the database, but a string 'FirstName' instead. That's why it is printed twice as you said. Use this SQL to get the field:
$query = mysqli_query($con,"SELECT FirstName FROM persons");
Also usually people put an id of the record and not a field, that may have possible duplicates into the value of an <option>. So, I would have used:
echo "<option value='". $row['id']."'>".$row['FirstName']
. '</option>';
selecting the id from the database together with first name.
Try this:
echo "<option value='".$row['FirstName']."'>".$row['FirstName']."</option>";
Also seems that you are having an issue with the database query. Swap your while loop with the following and see if it works
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo "<option value='".$row['FirstName']."'>".$row['FirstName']."</option>";
}
$result->free();
}

How can I send two values through a select?

I want to send the name and id field through the option tag. Right now as it is set up it is only sending id, how can send name as well to insert it in the data base?
<select name="category_id" size="1"><br />';
$sql = "SELECT id, name
FROM categories
ORDER BY name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
echo "<option value=\"".$row['id']."\">".$row['name']."</option>\n ";
}
echo'
</select>
Make the option value have this format: name$id
<select name="category_id" size="1"><br />';
$sql = "SELECT id, name
FROM categories
ORDER BY name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
echo "<option value=\"".$row['name']."$".$row['id']."\">".$row['name']."</option>\n ";
}
echo'
</select>
then when you retrieve the data you can explode it as so
$option = explode("$", $_POST['category_id']);
echo $option[0]; // Name
echo $option[1]; // Id
If I understand you correctly, you want a hidden <input> field and some javascript that writes the <option>'s text.
However, I can't think of a single application where that is desirable... The point of database normalization is to avoid duplication.

Keeping lastly selected value from a dropdownlist after button click

Just like the title says i'm having difficulties in achieving it.
Here's my dropdownlist:
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[data] name=\"blabla\">$nt[data]</option>";
}
echo "</select>";
?>
Here's the buttonclick:
<?php
if(isset($_POST['Submit']))
{
$query = "SELECT SUM(suma), paskirtis FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' AND data ='".$_POST['data']."' group by paskirtis";
$result = mysql_query ($query);
echo "<tr><td>Paskirtis:</td><td>Biudzetas:</td><td>Isleista:</td><td>Likutis:</td></tr>";
while($nt=mysql_fetch_array($result)){
if($nt['SUM(suma)'] != null){
$suma = $nt['SUM(suma)'];
}
echo "<tr><td>$nt[paskirtis]</td>
<td><input type=\"text\" name=\"isleista[]\" value=\"Skiriamų pinigų kiekis...\" method=\"post\"></td><td>".$suma." Lt</td><td>--</td></tr> <br>";
}
}
?>
After I press it, it retrieves the data I want from the date I've chosen from the drop down list and also reset whole drop down list showing the first value of the dates from sql database, not the one I selected. If anyone knows how to keep the selected value in the list any help is greatly appriciated!
Try this, you need to place select="selected" in the while loop. See below code how I placed the $selected
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
$selected = ($_POST['blabla'] == $nt[data])?'selected="selected"':NULL;
echo "<option value=$nt[data] name=\"blabla\" $selected >$nt[data]</option>";
}
echo "</select>";
?>

Categories