Ok so here is my code:
$select_status = 0;
$select_status = "<select name='status'>\n";
$select_status .= "<option value=''>SELECT ONE</option>\n";
$sdataset = mysql_query("SELECT id, name FROM phponly_category") or die(mysql_error());
while($srow=mysql_fetch_assoc($sdataset)) {
echo implode(", ", $srow);
echo "<br />";
$select_status .= "<option value='".$srow['name']."'";
$select_status .= ">".$srow['name']."</option>\n";
} // end while loop
echo "out of the loop";
$select_status .= "</select>\n";
// now insert the <select> list control into the page
echo $select_status;
The code works fine until the last row when it breaks. I cannot get the echo $select_status printed.
I have tried to see what is going on with the SQL query results by printing each row but everything looks fine there. For some reason, at the last row the while loop breaks and even the code after while loop doesn't get executed.
Don't do the or die(mysql_error()) portion in the while test...do it before.
if($sdataset==false) {
die(mysql_error());
}
while($srow=mysql_fetch_array($sdataset)) {
$select_status .= "<option value='".$srow['name']."'".">".$srow['name']."</option>\n";
} // end while loop
The or die() on your while() loop will actually kill the script when you read the end of the result set. mysql_fetch will return false, triggering the or die().
While checking for errors is good on queries, you can't do it like this on the fetching part, because you get false positives like this.
I personally don't like to echo out html code, if your goal is to do validate whether there's result comes out of the query, you can do something like this
<?php
$sdataset = mysql_query("SELECT id, name FROM phponly_category");
if (mysql_num_rows($sdataset) > 0) {
?>
<select name='status'>
<option value=''>SELECT ONE</option>
<?php
while($srow = mysql_fetch_array($sdataset)) {
?>
<option value='<?php echo $srow['name'] ?>'><?php echo $srow['name'] ?></option>
<?php } // end while loop ?>
</select>
<?php
} // end of if
else {
// Whatever you wanna put here
}
?>
EDITED: There's a typo at mysql_num_rows, try this one again
Use mysql_fetch_assoc so you can get the data using the field name as the key.
<?php
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('test');
$select_status = "<select name='status'>\n";
$select_status .= "<option value=''>SELECT ONE</option>\n";
$sdataset = mysql_query("SELECT id, name FROM phponly_category") or die(mysql_error());
while($srow=mysql_fetch_assoc($sdataset)) {
$select_status .= "<option value='".$srow['name']."'".">".$srow['name']."</option>\n";
} // end while loop
$select_status .= "</select>\n";
echo $select_status;
?>
Note: The use of this extension is discouraged. Take a look at mysqli or PDO.
Related
In this code i am trying to fetch city names into the html dropdown. Kindly corrct me if i am wrong anywhere, it give me an error
<?php
$query = Run("select city_name from City");
echo "<select name="city-name" style="width: 210px;">";
while ($row = mssql_num_rows($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
?>
use mysqli_fetch_array($query) insted of mssql_num_rows($query)
try this one
echo "<select>";
while ($row =mysqli_fetch_array($query))
{
echo "<option value='".$row['city_name']."'>".$row['city_name']."</option>";
}
echo "</select>";
mssql_num_rows returns the number of rows in the result set, it doesn't iterate and return individual rows. Try using mssql_fetch_object instead.
If you start a string with " then you have to escape all occurrences of " inside your string or use '.
For example:
echo "<select name=\"city-name\" style=\"width: 210px;\">";
or
echo '<select name="city-name" style="width: 210px;">';
so you don't accidently close the string.
Also like the others pointed out, you have to use
mysqli_fetch_array($query).
Use this
$query = Run("select city_name from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_fetch_object($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
Use This
$query = Run("select `city_name` from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_num_rows($query))
{
echo "<option value='.$row->city_name.'>".$row->city_name."</option>";
}
echo "</select>";
I am trying to fetch all "requestors" from a table and offer them as options in a form. As far as I know I didn't fetch the first row before but somehow my code misses the first entry.
Also when I submit the form and try to get the selected value with $_POST it doesn't take the selected value but instead takes the entry before that one.
Here is the code:
<select name="requestor">
<option value="%">Please select Requestor</option>
<?php
$con=mysqli_connect("localhost","username","password","database");
//============== check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
$logins = mysqli_query($con, "select Login_name from login");
while ($dsatz = mysqli_fetch_assoc($logins))
{
echo $dsatz["Login_name"] . "<option value=". $dsatz["Login_name"]."</option>" . "<br />";
}
mysqli_close($con);
?>
</select>
try
echo "<option value='".$dsatz["Login_name"]."'>".$dsatz["Login_name"].'"</option>";
within the loop
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 have four drop-down lists that I would like to populate with values from an MSSQL table. All four lists should contain the same values. The query looks like this:
$data = $con->prepare("SELECT ID, Code FROM Table WHERE Code = :value ORDER BY Code");
$input = array('value'=>'value'); //'value' is hardcoded, not a variable
$data->execute($input);
And here is the code for my drop-downs:
<?php
echo "<select name=\"proj1[]\">";
while($row = $data->fetch(PDO::FETCH_BOTH))
{
echo "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
echo "</select>";
?>
This works fine for one drop-down. If I try to create another one (proj2[], proj3[], proj4[]) and apply the same query, however, the PHP page stops loading at that point and the second drop-down does not populate. The only way I've found around it is to copy the query and change the variables ($data becomes $data2 for proj2[], and so on). I'd really rather not have to write the same query four times. Is there a way around it?
$select = '';
while($row = $data->fetch(PDO::FETCH_BOTH))
{
$select .= "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
echo "<select name=\"proj1[]\">";
echo $select;
echo "</select>";
echo "<select name=\"proj2[]\">";
echo $select;
echo "</select>";
//etc...
Why not just put all of it in a veriable and then using it 4 times?
Somthing like this:
<?php
while($row = $data->fetch(PDO::FETCH_BOTH))
{
$options .= "<option value='".$row['Code']."'>".$row['Code']."</option> ";
}
for($i = 0; $i <= 4; $i++){
echo "<select name=\"proj1[]\">";
echo $options;
echo "</select>";
}
?>
So I have this drop down list in my form which pull "tags" from database as value for drop down options:
<select name="cartags">
<?php $result = mysql_query("SELECT * FROM Products WHERE ID > '0'");
while($row = mysql_fetch_array($result))
{
echo "<option value=\""; echo $row['Tag']; echo "\""; echo ">"; echo $row['Tag']; echo "</option>";
}
?>
</select>
What is my problem? My problem is that I am adding a lot of products into my databas and my code make dropdown list with tags for all this producst even if they have same tag. So what I need is solution how to prevent that same tag appear twice in my drop down.
I am pretty new to PHP and this is my first question here so I really hope that I explained my problem well.
Thanks in advance!
What is the purpose of WHERE ID > '0'? If ID is an auto-increment then it will always be positive. If not, it should be.
Why are you using mysql_fetch_array and then only using the associative keys? You should use mysql_fetch_assoc instead.
Why are you using a new echo every time you want to output a variable? Just concatenate.
Why are you setting the same string in value as the option's text? Without a value, it defaults to the text anyway.
Why are you not using backticks around your column and table names?
Try this instead:
<select name="cartags">
<?php
$result = mysql_query("SELECT DISTINCT `Tag` FROM `Products`");
while(list($tag) = mysql_fetch_row($result)) {
echo "<option>".$tag."</option>";
}
?>
</select>
Try this
<select name="cartags">
<?php $result = mysql_query("SELECT Tag, COUNT(Tag) tg Products WHERE ID > '0' GROUP BY Tag HAVING COUNT(Tag)>0 ORDER BY tg DESC");
while($row = mysql_fetch_array($result))
{
echo "<option value=\""; echo $row['tg']; echo "\""; echo ">"; echo $row['tg']; echo " </option>";
}
?>
</select>
It will also display the top tags that have the most first.