I'm having a bit of trouble using multiple queries within one php file. The code that I am trying to create would ideally create multiple html drop down menus populated with data from separate tables. My code is as follows:
echo "<form enctype='multipart/form-data' method='post' action='http://www.vgiver.com/uploadpicture1.php' name='Giftgiver'>";
$Wrap= #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Wrapping Paper: \n";
print "<Select name=\"Wrap_ID\">\n";
while ($row2=mysql_fetch_assoc($Wrap)){
$Wrap_ID=$row2['Wrap_ID'];
$Wrap_Picture =$row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture \n";
}
$result= #mysql_query("select Friend_ID, tbl_Friends.Name from tbl_Friends inner join tbl_Users on tbl_Friends.Access_ID=tbl_Users.Access_ID where tbl_Friends.Access_ID = $myuserid");
$Wrap= #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Friend: \n";
print "<Select name=\"Friend_ID\">\n";
while ($row=mysql_fetch_assoc($result)){
$Friend_ID=$row['Friend_ID'];
$Name =$row['Name'];
print "<option value=$Friend_ID>$Name \n";
}
However, as it is now, this code will create a selection box for only one set of data. It does not matter which query I do first, it will either display a drop down menu for a friend's list, or a drop down menu for my wrapping paper, but it will not display both.
Any help would be appreciated.
You have forgotten ending <select>, <p>, <option> tag.
It should looks like:
<?php
echo "<form enctype='multipart/form-data' method='post' action='http://www.vgiver.com/uploadpicture1.php' name='Giftgiver'>";
$Wrap = #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Wrapping Paper:</p> \n";
print "<Select name=\"Wrap_ID\">\n";
while($row2 = mysql_fetch_assoc($Wrap)) {
$Wrap_ID = $row2['Wrap_ID'];
$Wrap_Picture = $row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture </option>\n";
}
echo '</select>';
$result = #mysql_query("select Friend_ID, tbl_Friends.Name from tbl_Friends inner join tbl_Users on tbl_Friends.Access_ID=tbl_Users.Access_ID where tbl_Friends.Access_ID = $myuserid");
$Wrap = #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Friend: </p>\n";
print "<Select name=\"Friend_ID\">\n";
while($row = mysql_fetch_assoc($result)) {
$Friend_ID = $row['Friend_ID'];
$Name = $row['Name'];
print "<option value=$Friend_ID>$Name </option>\n";
}
echo '</select>';
After each of your while loops you need to end the <select> tag:
print "<select name=\"Wrap_ID\">\n";
while ($row2=mysql_fetch_assoc($Wrap)){
$Wrap_ID=$row2['Wrap_ID'];
$Wrap_Picture =$row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture</option> \n";
}
print '</select>';
You should also end your <option> tags properly like in the example above
You don't close your <selects>. The second set of data is there, but HTML ignores it. try viewing the source of your file you'll see.
You forgot to close your select's and your option's:
print "<p> Select a Wrapping Paper: \n";
print "<Select name=\"Wrap_ID\">\n";
while ($row2=mysql_fetch_assoc($Wrap)){
$Wrap_ID=$row2['Wrap_ID'];
$Wrap_Picture =$row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture</option>\n";
}
print "</select>"; //<-------add this
EDIT:
Also your paragraph. HTML requires tags to be closed:
<tag>content</tag>
So you would have:
print "<p> Select a Wrapping Paper: </p> \n";
instead of:
print "<p> Select a Wrapping Paper: \n";
Your full code should be:
echo "<form enctype='multipart/form-data' method='post' action='http://www.vgiver.com/uploadpicture1.php' name='Giftgiver'>";
$Wrap= #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Wrapping Paper:</p> \n";
print "<Select name=\"Wrap_ID\">\n";
while ($row2=mysql_fetch_assoc($Wrap)){
$Wrap_ID=$row2['Wrap_ID'];
$Wrap_Picture =$row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture</option> \n";
}
print "</select>";
$result= #mysql_query("select Friend_ID, tbl_Friends.Name from tbl_Friends inner join tbl_Users on tbl_Friends.Access_ID=tbl_Users.Access_ID where tbl_Friends.Access_ID = $myuserid");
$Wrap= #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Friend: </p> \n";
print "<Select name=\"Friend_ID\">\n";
while ($row=mysql_fetch_assoc($result)){
$Friend_ID=$row['Friend_ID'];
$Name =$row['Name'];
print "<option value=$Friend_ID>$Name</option> \n";
}
print "</select>";
Bonus hints:
Use single quotes instead of double quotes when you don't need to do parsing for variables as it is faster.
Use echo instead of print.
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 think i am doing some mistake in my code. can someone please correct me. how to get the list box values in another page.
print "<form method=\"post\" action=\"fetch_test.php\">";
print "Report From: ";
print "<select name=fromdate[]>";
print "<option selected=\"selected\">Date</option>";
print "<option value=01>01</option>";
print "<option value=02>02</option>";
print "<option value=03>03</option>";
print "</select>";
print "<select name=frommonth[]>";
print "<option selected=\"selected\">Month</option>";
print "<option value=jan>Jan</option>";
print "<option value=feb>Feb</option>";
print "<option value=mar>Mar</option>";
print "</select>";
print "<br><br>";
print "<input type=submit>";
print "</form>";
fetch_test.php contains below code.
<?php
print $_POST['fromdate'];
print $_POST['frommonth'];
?>
You are using the name as an Array.
Fixed code:
print "<form method=\"post\" action=\"fetch_test.php\">";
print "Report From: ";
print "<select name=fromdate>";
print "<option selected=\"selected\">Date</option>";
print "<option value=01>01</option>";
print "<option value=02>02</option>";
print "<option value=03>03</option>";
print "</select>";
print "<select name=frommonth>";
print "<option selected=\"selected\">Month</option>";
print "<option value=jan>Jan</option>";
print "<option value=feb>Feb</option>";
print "<option value=mar>Mar</option>";
print "</select>";
print "<br><br>";
print "<input type=submit>";
print "</form>";
You stored it in an array, so call it in an array also.
<?php
print $_POST['fromdate'][$yournumbervalue];
print $_POST['frommonth'][$yournumbervalue];
?>
Try quoting your select tag names, you want to have <select name="fromdate[]"> to get an array, and you want to use <select from="fromdate"> to get a single value.
Unless there are more than one name='fromdate' or name='frommonth', you don't need the [].
Just try normal:
print "<select name='fromdate'>";
Make sure you put ' around the name attribute!
Then use:
$_POST['fromdate'];
At the moment you're putting it into an array, so if you want to keep it the way it is, use:
foreach ($fromdate as $date){
echo $date;
}
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>";
}
?>
I have a dropdown menu created with the code below which fetches all brands from a brands table. It uses a while loop thus showing all on the menu starting alphabetically (i.e., Adiddas and so on). Therefore I don't list them as individual <options> line by line.
echo "<form action=\"type.INC.php\" method=\"get\">\n";
echo "<select name=\"brand\">\n";
$stmt = mysqli_stmt_init($hook);
if($stmt=mysqli_prepare($hook,"SELECT brandid, brandname FROM brands WHERE brandid "));
{
mysqli_stmt_bind_param($stmt,"i", $brandid);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $brandid, $brandname);
while(mysqli_stmt_fetch($stmt))
{
$brandname = htmlspecialchars($brandname, ENT_QUOTES, 'UTF-8');
echo "<option value=\"$brandid\">$brandname </option>";
}
echo "</select>\n";
echo "<input name=\"submit\" type=\"submit\" id=\"brandid\" value=\"submit\" />\n";
echo "</form> \n";
How can I show the sentence "SELECT A BRAND" to appear as the default first value? Should I just enter "SELECT A BRAND" into my brands table and assign a primaryID of zero to it?
Any better way to do this?
All of my searches regarding this question result in topics relating to the 'select=selected attribute'.
Thanks,
Jen
Just add it manually before the loop:
echo "<form action=\"type.INC.php\" method=\"get\">\n";
echo "<select name=\"brand\">\n";
// Select a brand, empty value:
echo "<option value=\"\">(SELECT A BRAND)</option>";
$stmt = mysqli_stmt_init($hook);
if($stmt=mysqli_prepare($hook,"SELECT brandid, brandname FROM brands WHERE brandid "));
{
mysqli_stmt_bind_param($stmt,"i", $brandid);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $brandid, $brandname);
while(mysqli_stmt_fetch($stmt))
{
$brandname = htmlspecialchars($brandname, ENT_QUOTES, 'UTF-8');
echo "<option value=\"$brandid\">$brandname </option>";
}
echo "</select>\n";
echo "<input name=\"submit\" type=\"submit\" id=\"brandid\" value=\"submit\" />\n";
echo "</form> \n";
Add
echo "<option>SELECT A BRAND</option>";
as the third line of your code.
$sql = "SELECT DISTINCT Branch FROM student_main";
$result = mysql_query($sql);
$row_num = mysql_num_rows($result);
$rows = mysql_fetch_array($result);
echo "<select name='Branch'>";
for($i=0;$i<=$row_num-1;$i++){
echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>";
}
echo "</select>";
echo "<input type='submit' Value='submit' />";
echo "</form>";
I am trying to create a dropdown using the above code for my form. But its not working. There are 3 distinct values in the Branch column but in the dropdown, it shows only one value(the first one) and the next two as blank values.
However when in echo $row_num, its shows 3.
Thats means its fetching the three rows, but then why its not showing in the dropdown list.
If I run the same query in phpmyadmin it shows the correct answer i.r it returns 3 distinct Branch values.
You should do something like this:
$sql = "SELECT DISTINCT Branch FROM student_main";
$result = mysql_query($sql);
echo "<select name='Branch'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
echo "</select>";
echo "<input type='submit' Value='submit' />";
echo "</form>";
you need to mysql_fetch_array() for each row. That function returns an associative array for one row only. just include it inside your for loop just above your echo statement.
edit: mysql_fetch_array() actually returns an array (by default) that has associative indices and numbered indices. You can continue using it the same way, though.
You need to loop through your query using the following:
$sql = "SELECT DISTINCT Branch FROM student_main";
$result = mysql_query($sql);
echo "<select name='Branch'>";
while($rows = mysql_fetch_array($result)){ // should probably use mysql_fetch_assoc()
echo "<option value='".$rows['Branch']."'>".$rows['Branch']."</option>";
}
echo "</select>";
echo "<input type='submit' Value='submit' />";
echo "</form>";
mysql_fetch_array only returns the current dataset as an array, and moves the internal pointer ahead. You need to repeatedly call mysql_fetch_array to get all results.
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row['Branch']."'>".$row['Branch']."</option>";
}
There is a problem in the loop using a while loop:
while($rows=mysql_fetch_array($result)){
echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>";
}
Try this
What you really need is to learn how to use templates.
But it seems Stackoverflow is definitely not the place where one can learn professional ways of website developing.
get your data first
$select = $array();
$sql = "SELECT DISTINCT Branch FROM student_main";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($res)) $select = $row[];
And then use it in the template
<form>
<select name='Branch'>
<? foreach($select as $row): ?>
<option value="<?=htmlspecialchars($row['Branch'])?>">
<?=htmlspecialchars($row['Branch'])?>
</option>
<? endforeach ?>
</select>
<input type='submit' Value='submit' />
</form>
mysql_fetch_array will only return the first row...
see here for full details :)