Would like to fetch the details from table and display in dropdown but below code displays only dropdown without any data.not sure whats worng.
<?php
include 'config.php';
$sql = "select name from finance";
$result = mysqli_query($con,$sql);
echo "<select name='name'>";
while ($row = mysqli_fetch_row($result)) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
echo "</select>";
?>
can some please advice.
mysqli_fetch_row does not return associated array.
Instead, Use mysqli_fetch_array
Also, check if your query is returning any record
Related
I'm trying to create a dropdown list with items from the mysql database. The problem is that my list is empty. I looked around but I could not find a solution.
$q = $db->getQuery(true);
$q->select('#__peaks.peak_name');
$q->from($db->quoteName('#__peaks'));
$db->setQuery($q);
$result = $db->loadColumn();
echo "<select name='peak_name'>";
echo "<option size =30 ></option>";
while($row = mysql_fetch_array($result))
{
echo "<option value='".$row['peak_name']."'>".$row['peak_name']."</option>";
}
echo "</select>";
Where am I wrong? Please help.
I've set up a php form that registers a project to our database, it has a drop down that populates from our customer/supplier databases.
I've also set up a function to edit these projects, the problem I have is that when I go to my edit page it just displays the customer/supplier name and not in the drop down but a value box - is there a way to have the edit page display the dropdown but also be selected on the original supplier/customer?
Register project page
<?php
$result = mysql_query('SELECT name FROM customers ORDER BY name ASC');
echo '<select name='client'>';
while($row = mysql_fetch_assoc($result))
{ `
echo '<option value = ''.$row[name].''>'.$row[name].'</option>';
}`
echo '</select>';
?>
Edit page
<input type='text' name='client' value='<?php echo $client; ?>'/>
I tried a few tutorials and code tweaks but kept getting errors. I am aware of my sql injection problem, at the moment this site is internal.
Any help would be appreciated.
thanks
instead of $row[name] you should use $row['name']
$client= "<select name='client'>"; // you had error here also.
while($row = mysql_fetch_assoc($result))
{
$client.= "<option value = '".$row['name']."'>'".$row['name'].'</option>';
}
$client.= '</select>';
now echo $client to get dropdown.no need of constructing separate select tag now.
for selected use like this:
$client1= "<select name='client'>";
while($row = mysql_fetch_assoc($result))
{
if($row['name'] == $clientValue){
$client.= "<option selected='selected' value = '".$row['name']."'>'".$row['name'].'</option>';
}else{
$client1.= "<option value = '".$row['name']."'>'".$row['name'].'</option>';
}
}
$client1.= '</select>';
on echo of $client1 you will get selected based on the value $clientValue which you have to pass.
On your edit page:
<?php
$result = mysql_query('SELECT name FROM customers ORDER BY name ASC');
echo "<select name=\"customer\">";
while($row = mysql_fetch_assoc($result))
{
if ($row['name'] == $client)
{
echo "<option selected value=\"" . $row['name'] . "\">" . $row['name'] . "</option>";
}
else
{
echo "<option value=\"" . $row['name'] . "\">" . $row['name'] . "</option>";
}
}
echo "</select>";
?>
I also suggest that you change the old extension for mysql. I can't see no SQL Injection problem for now, but you should take care of it even if it is internal, because, from different reasons you will forget to sanitize it later. If you are writing it, then write it correctly.
Now for the problem, you are not using the quotes correctly, hence the errors. Do not use the same type of quotes, but change them, like so:
echo '<select name="client">';
Or if you use double quotes for concatenation, use single inside.
In case you have to use the same, escape them with \
For starters, you have a syntax error here:
echo '<select name='client'>';
(There are probably more quoting errors throughout the code, but I digress...)
As for using a drop-down, what you're looking for is the selected attribute. When you're building the page elements to display the form on the "edit" page, presumably you have the values that you're looking to display. When your loop finds an element which matches the value, select it:
while($row = mysql_fetch_assoc($result))
{
if ($knownValue == $row[name]) {
echo '<option selected value = ''.$row["name"].''>'.$row["name"].'</option>';
} else {
echo '<option value = ''.$row["name"].''>'.$row["name"].'</option>';
}
}
I've been working with this for almost an 3 hours now. It just seems unfixable. I've tried millions of options but just won't work.
So I want to echo from column title in table news so they apear like options in option select.
THis is how it looks now:
Here is the code:
<?php
global $mysqli;
mysqli_connect($mysqli,'localhost', 'root', '');
mysqli_select_db ($mysqli,"filip12356");
$sql = "SELECT DISTINCT title FROM news";
$result = mysqli_query($mysqli,$sql);
echo "<select name='title_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['title'] . "'>" . $row['title'] . "</option>";
}
echo "</select>";
?>
Well you seem to be using mysqli_ everywhere except here...
$row = mysql_fetch_array($result))
Should be..
$row = mysqli_fetch_array($result))
I would ensure that all files are UTF-8 encoded without BOM. It seems that the server is misunderstanding the php as text.
I would like to remain my drop down value which I select for submitting after posting the form. My form posts to the same page.
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
echo "Country: <select name='country' value=''>";
while($r = mysql_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id.">".$cname."</option>";
}
echo "</select>"; ?>
Remove your current echo inside the loop and replace it with the following:
if($_POST["country"]==$id)
echo "<option value='".$id."' selected='selected'>".$cname."</option>";
else
echo "<option value='".$id."' >".$cname."</option>";
This will check if the current option being displayed is the one that was submitted and it will select it in that case.
If I understand what you are looking for correctly you need to use the $_POST value of your select to set the selected item...
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
$country = '';
echo "Country: <select name='country'>";
while($r = mysqli_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id;
echo ($_POST["country"]==$id) ? ' selected="SELECTED"' : '';
echo ">".$cname."</option>";
}
echo "</select>"; ?>
Setting selected="SELECTED" for the $id that matches $_POST['country'] will make it the selected item in your dropdown.
And, get rid of mysql* functions and use mysqli* functions instead...
I fetching a result set from a MySQL table and displaying it on the web page as under:
<?php
$link = mysql_connect(....);
mysql_select_db(....);
$sql = "select * from products where category = '" .$_POST['prod_cat']. "'";
$rs = mysql_query($sql, $link);
echo "<form name='prodselect' action='prodlist.php' method='post'>";
echo "<table>";
$rowcount = 1;
while ($row = mysql_fetch_array($rs))
{
echo "<tr>";
echo "<td>" .$row['product_name']. "</td>";
echo "<td><input type='checkbox' name='line_" .$rowcount. "'></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
?>
This displays the list of products with checkbox for every product row.
The user will select products using the checkboxes.
I want to display / save the selected products in the "Products for Purchase" list on another web page.
Please help me out.
Thanks.
I can suggest you to create the checkbox like this:
echo "<td><input type='checkbox' name='products[]' value='" .$row['product_id']. "'></td>";
then on the prodlist.php you can get the array $_POST['products']