How can I send two values through a select? - php

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.

Related

store the country ID from country drop down box in mysql but it is storing as 0 in db

//This is my php to view dropdown box <?php
include('connect.php');
//for retriving data from DB and show it in drop down box
$query="SELECT cname FROM country";
$result = mysqli_query ($con, "$query");
echo "<select name=country value=''>";
while($r=mysqli_fetch_row($result))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
echo "</select>";
?>
But when i am storing in DB my country_id is always 0.
You are not fetching country id from SQL Query
Change
$query="SELECT cname FROM country";
To
$query="SELECT cid, cname FROM country"; // Update cid with your country id field
Therefore $r[0] is getting blank value.
Use this code for your reference
//This is my php to view dropdown box
<?php
include('connect.php');
//for retriving data from DB and show it in drop down box
$query="SELECT countryid,cname FROM country";
$result = mysqli_query ($con, "$query");
echo "<select name='country'>";
while($r=mysqli_fetch_row($result))
{
echo "<option value='$r[countryid]'> $r[cname] </option>";
}
echo "</select>";
?>
I am going with Pupil's answer to change the query to
$query="SELECT cid, cname FROM country"; // Update cid with your country id field
and then change this
echo "<select name=country value=''>";
to
echo "<select name=country>"; //remove value=''
EDIT:
Please check your column type may be it is "INT" when you try to store text it stores 0.

mysqli join two tables to match user name with the corresponding user id

I'm having a hard time figuring this out..
I have two tables "teacher_info" and "section_info".
In my forms I included all the attributes in my section_info table except I used teacher name instead of teacher id for easy selection which is a dropdownlist of teacher's name, this is my code
<?php
include("anhsis.php");
mysqli_select_db($con,"anhsis");
$result= mysqli_query($con,"SELECT t_lname,t_fname FROM teacher_info");
echo"<select name='adviser' class='form-control' required>";
echo"<option value='0'>--Select Adviser--</option>";
while ($row=mysqli_fetch_array($result)) {
echo "<option value='".$row['t_lname']."".$row['t_fname']."'>".$row['t_lname'].", ".$row['t_fname']."</option>";
}
echo'</select>'
?>
and heres my php code to insert data in "section_info"
<?php
include_once('anhsis.php');
$room_id = $_POST['room_id'];
$section = $_POST['section'];
$adviser = $_POST['teacher_id'];
$level = $_POST['level'];
$curriculum = $_POST['curriculum'];
mysqli_select_db($con,"anhsis");
$result= mysqli_query($con,"SELECT * FROM section_info WHERE room_id= '$room_id'");
if (mysqli_num_rows($result)>0){
echo '<script type="text/javascript">';
echo 'alert("TIN No already exist!")';
echo '</script>';
}
else{
mysqli_query($con,"INSERT INTO section_info VALUES('$room_id','$section','$adviser','$level','$curriculum')");
}
?>
my problem is that in my section_info theres no attribute of teacher's name, instead it has teacher_id. So how am I going to insert teacher_id from the "teacher_info" table to the "section_info" table by just selecting the teacher's name in my dropdownlist. Or is it possible?
Thanks!
$result= mysqli_query($con,"SELECT t_lname,t_fname,teacher_id FROM teacher_info");
echo "<select name='adviser' class='form-control' required>";
echo "<option value='0'>--Select Adviser--</option>";
while ($row=mysqli_fetch_array($result)) {
echo "<option value='".$row['teacher_id']."'>".$row['t_lname'].", ".$row['t_fname']."</option>";
}
This way you will have teacher_id value in $adviser variable, ready to use.

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();
}

Select field and retrieve corresponding id

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.

php drop-down menu from MYSQL, how to return other column value?

I'm having a input form that asks for a location. The locations are stored in a mysql db and have an id (colomn: id and column: location).
I have a drop down menu that is generated from those records in the db:
<select name="location">
<?php
$query="SELECT location FROM locations";
$result=mysql_query($query) or die;
while ($row=mysql_fetch_array($result)) {
$location=$row['location'];
echo "<option>$location</option>";
?>
</select>
This all works. When the form is sumbitted, I obviously get a POST[location] for example "Belgium".
Let's say Belgium is the location in my db and has id 5, how can I return the ID as the POST variable from the dropdown box, instead of the location. Ofcourse I want the dropdown to show the locations, and not the ID's.
Thanks in advance!
Each option can take a value and show another string so use value="my_value" for each option inside the select tag
<select name="location">
<?php
$query="SELECT id, location FROM locations";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)) {
echo "<option value=\"" . $row['id'] . "\">" . $row['location'] . "</option>";
}
?>
</select>
now your POST['location'] will contain the db id for selected location
if you change the SQl query to include the ID of the location,
you can assign that value to the dropdown selected value.
<select name="location">
<?php
$query="SELECT id, location FROM locations";
$result=mysql_query($query) or die;
while ($row=mysql_fetch_array($result)) {
$location=$row['location'];
$id = $row['id'];
echo "<option value='".$row['id']."'>".$location."</option>";
?>
</select>
select ID from locations;
$ID=$row['ID'];
Replace ID by the ID-name of your column ofcourse.
If you say select * from locations you can do something like this:
$row['anyCOLUMNNAME']
You can choose any column you like and use that information from that particular row.
<select name="location">
<?php
// select columns you need, separate by , (comma)
$query = "SELECT `column1`, `column2` FROM `locations`;";
$result = mysql_query($query) or die;
while ($row = mysql_fetch_array($result)) {
// selected columns become accessible in $row array
// value attribute needs to be escaped here
echo '<option value="', htmlentities($row['column1']),'">',
htmlentities($row['column2']), '</option>'; // escape label too
// <option> does not accept HTML in label so it should be escaped
} // done!
?>
</select>
^ this (read comments for explanations)

Categories