PHP get value under mysql row - php

I'm trying to get a value that is associated (through mysql data-base row) to a unique Id through an option form:
<form>
<td>
<select name='rolo'>
<option value='$id'>$item</option>
</td>
<td>
$value
</td>
</select>
</form>
So, when I choose an option, I get the correspondent $value associated with it in the next .
What I have tried so far:
$query = "SELECT * FROM rolostock WHERE id='$id' ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo $row['value'];
}
I am not a php expertise, and I thought this would do it? Thanks in advance!
EDIT:
What i'm trying is to (html form) SELECT > OPTION $id > (php) get $value of $id in the next (html) TD . :\

Try with this:
$query = "SELECT * FROM `rolostock` WHERE id = '$id';";
$result = mysql_query($query);
if (!$result) exit("The query did not succeded");
else {
while ($row = mysql_fetch_array($result)) {
echo $row['value'];
}
}
If running this prints The query did not succeded then you have an error in your query. Try running it via PhpMyAdmin.
Also use:
<option value='<?php echo $id;?>'><?=php echo $item;?></option>
instead of:
<option value='$id'>$item</option>

Related

Display all data in dropdown from database

i am using Php/Mysql , i have the client table and trying to display data in a drop down list. Unfortunately, only one client is displayed in drop down which i have the total of 3 clients. Why only one ? For example : Michael King, Michael Jordan , Michael John when i select all the data from table and make an output to display in dropdown, Michael John is only in the dropdown.
Here my Mysql code :
//All data is selected from client_tb
<?php
$sql = "SELECT * FROM client_tb";
$result = $conn->query($sql);
while($row=mysqli_fetch_array($result))
{
$id = $row['id'];
$lname = $row['lname'];
$fname = $row['fname'];
}
?>
//my dropdown which will show the clients from client_tb but only one will appear.
<option value ="<?=$lname?><?=$fname?>"><?=$lname?> , <?=$fname?> </option> </select><br><br>
You can also achieve dropdown outside the while loop. Try this:
$sql = "SELECT * FROM client_tb";
$result = $conn->query($sql);
$options =array();
while($row=mysqli_fetch_array($result))
{
$options[] =$row;
}
Your dropdown:
<select name="">
<?php
foreach($options as $option):
echo '<option value ="'.$option['lname'].''.$option['fname'].'">'.
$option['lname'].','.$option['fname'].'</option>';
endforeach;
?>
</select>
You could also add your db query into a function , then call it.
function myFunction() {
$sql = "SELECT * FROM client_tb";
$result = $conn->query($sql);
while($row=mysqli_fetch_array($result))
{
$myvalues[] =$row;
}
return $myvalues;
}
Now the dropdown,
Note the options are inside the loop
<select name="">
<?php foreach($myvalues as $myvalue) {
echo '<option value="'.$myvalue['lname'].''.$myvalue['fname'].'">'.
$myvalue['lname'].','.$myvalue['fname'].'</option>';
}
?>
</select>

Get value form same table

I have dropdown menu with 3 values.
and here is my table (table name is Sms)
What I want to do? Example : If I choose 2,49 and press submit, then I get sonum value.
This is my form
<div class="col_12" style="margin-top:100px;">
<div class="col_6">
<label for="asukoht">Vali Hind</label>
<form class="vertical" method="GET">
<select name="hind">
<option value="1">-- Vali --</option>
<?php
// Tegin dropdown menüü, kust saab valida komponendi, mille alla see pilt läheb
$andmed = mysql_query("SELECT * FROM Sms");
// Dropdown menüü
while($rida = mysql_fetch_array($andmed)){
echo '<option value="'.$rida['id'] . '">'.utf8_encode($rida['hind'] ). '</option>';
}
?>
<input type="submit" name="add" id="add">
</form>
I tried something like this
if(mysql_query("DESCRIBE `Sms`")) {
$sql = "SELECT sonum FROM `Sms`";
echo $sql;
}
I think it should be pretty easy, but I'm looking for a solution and I didnt found it.
Thank you for helping !
You need to work on SQL and Loop.
Based on your code:
if(mysql_query("DESCRIBE `Sms`")) {
$sql = "SELECT sonum FROM `Sms`";
echo $sql;
}
First we do change the query including $_GET parameter.
So this:
$sql = "SELECT sonum FROM `Sms`";
Will become:
$sql = "SELECT sonum FROM `Sms` WHERE id = ".$_GET['hind'];
It will be better if you check that the var exist and is setted with something like:
if(isset($_GET['hind']) && is_numeric(trim($_GET['hind']){//Code here}
But it is off-topic.
Now let's change echo $sql; with a loop, we need to loop and fetch the data.
while($result = mysql_fetch_array($sql)){
echo '<option value="'.$result ['id'] . '">'.utf8_encode($result ['hind'] ). '</option>';
}
I've only changed what i know, you know your system ^_^
You should do:
$sql = "SELECT sonum FROM Sms WHERE id = ".$_GET['hind'];
Then do :
echo mysql_query($sql);
$sql = "SELECT sonum FROM Sms WHERE id = ".$_GET['hind'];
while($rida = mysql_fetch_array($sql)){
echo '<option value="'.$rida['id'] . '">'.utf8_encode($rida['hind'] ). '</option>';
}
Do not use MYSQL queries...try MySQLi or PDO with prepared statement.

Making a dynamic drop down menu sticky in php

I have a dynamic drop down menu on a PHP form which is working fine in that it retrieves/inputs the right id and will not process the form if no option is collected.
However, I am not sure how to make it sticky. I can do it on a static drop down with no problems but obviously I am missing something, can anyone help?
Below is the drop down menu:
echo '<div align="left">
<select name="dealership_id">
<option value="NULL">Choose a Dealer:</option>';
$query = 'SELECT * FROM dealership ORDER BY users_dealer_name ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0] \" <?php if (isset($_POST['dealership_id']) && $_POST['dealership_id'] == '$row[0]') {echo 'selected=\"selected\"';} ?> >$row[3]</option>";
}
// Complete the dropdown
echo '</select>
</div>
';
Below is the validation code
if (isset($_POST['dealership_id'])) {
$dealer_id = (int) $_POST['dealership_id'];
} else {
$dealer_id = 0;
}
if ($dealer_id > 0) {
$query = "SELECT dealership_id FROM dealership WHERE dealership_id=$dealer_id";
$result = mysql_query ($query); }
else {
echo '<p><font color="red">Please select your Dealership</font></p>';
}
BTW, row 0 is the primary key, row 3 is the name.
I don't think there should be single quotes around $row[0] in the following:
$_POST['dealership_id'] == '$row[0]'
By using single quotes you are literally comparing the string $row[0] instead of the variable value
Here's your code with some changes that are at least valid syntax; I didn't test to see if it works, but it should. It would be helpful for you to research string concatenation in php, some useful info here: http://www.php.net/manual/en/language.operators.string.php
echo '<div align="left">
<select name="dealership_id">
<option value="NULL">Choose a Dealer:</option>';
$query = 'SELECT * FROM dealership ORDER BY users_dealer_name ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0]\"";
if (isset($_POST['dealership_id']) && $_POST['dealership_id'] == $row[0]){
echo ' selected=\"selected\"';
}
echo ">$row[3]</option>";
}
// Complete the dropdown
echo '</select>
</div>
';

PHP echo out data into HTML drop drop down menu

I have a HTML etc.. tags now what I want to achieve is upon a selection of ie. i want to load the related info from database to in a new tag with as many tags.
I am using PHP to do achieve this now at this point if for example i choose option1 then the query behind it retrieves relevant information and stores it in a array, and if I select option2 exactly the same is done.
The next step I made is to create a loop to display the results from array() but I am struggling to come up with the right solution to echo retrieved data into etc. As its not my strongest side.
Hope you understand what I am trying to achieve the below code will clear thing out.
HTML:
<select id="workshop" name="workshop" onchange="return test();">
<option value="">Please select a Workshop</option>
<option value="Forex">Forex</option>
<option value="BinaryOptions">Binary Options</option>
</select>
PHP:
$form = Array();
if(isset($_POST['workshop'])){
$form['workshop'] = $_POST['workshop'];
$form['forex'] = $_POST['Forex'];
$form['binary'] = $_POST['Binary'];
//Retrieve Binary Workshops
if($form['workshop'] == 'Forex'){
$sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR course LIKE '&forex%'";
$query2 = mysqli_query($link, $sql2);
while($result2 = mysqli_fetch_assoc($query2)){
//The problem I am having is here :/
echo "<select id='Forex' name='Forex' style='display: none'>";
echo "<option value='oko'>.$result[1].</option>";
echo "</select>";
print_r($result2);echo '</br>';
}
}else{
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%Binary%' OR course LIKE '%binary%'";
$query = mysqli_query($link, $sql);
while($result = mysqli_fetch_assoc($query)){
print_r($result);echo '</br>';
}
}
}
Try this code:
$query2 = mysqli_query($link, $sql2);
echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
echo "<option value='oko'>{$result['course']}</option>";
}
echo "</select>";
echo '</br>';
From the top in your php:
// not seeing uses of the $form I removed it from my answer
if(isset($_POST['workshop'])){
$workshop = $_POST['workshop'];
$lowerWorkshop = strtolower($workshop);
// neither of $_POST['Forex'] nor $_POST['Binary'] are defined in your POST. you could as well remove those lines?
//Retrieve Binary Workshops HERE we can define the sql in a single line:
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%$workshop%' OR course LIKE '&$lowerWorkhop%'";
$query = mysqli_query($link, $sql); // here no need to have two results
// Here lets build our select first, we'll echo it later.
$select = '<select id="$workshop" name="$workshop" style="display: none">';
while($result = mysqli_fetch_assoc($query)){
$select.= '<option value="' . $result['id'] . '">' . $result['course'] . '</option>';
// here I replaced the outer containing quotes around the html by single quotes.
// since you use _fetch_assoc the resulting array will have stroing keys.
// to retrieve them, you have to use quotes around the key, hence the change
}
$select.= '</select>';
}
echo $vSelect;
this will output a select containing one option for each row returned by either of the queries. by the way this particular exemple won't echo anything on screen (since your select display's set to none). but see the source code to retrieve the exemple.

Problem populating a dropdown box with MySQL query results (PHP/MySQL)

so as the title states, using the following code I have got it populating the dropbox with a single result from the query, that result being the latest added in the table.
here is my code:
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
$aa = "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $aa; ?></select>
The odd thing is, I use this same code for another field, and it works, populating the dropdown with all the results, however in this case it only fills in the last unit code in the table and not all of which are attached to the particular user id.
I would appreciate anyones thoughts :D
thanks
$aa .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
add . before = and initiate $aa = ''; before while loop
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
$options = "";
while($row = mysql_fetch_assoc($result)){
$options .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $options; ?></select>
should work. You forgot a . in your while loop

Categories