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.
Related
I have a updateProducts page where I want to show a category drop down and within the drop down I want that category pertaining to a particular product must be shown on the top of the list. Which means, the drop down must show all the categories but the selected category should be shown on the top of the list.
Code:
$pid=$_GET['id']; //getting the id from another page
$data=mysqli_query($con,"SELECT * FROM products WHERE pid='$pid'");
while($row=mysqli_fetch_array($data))
{
echo "<td><img src='../product_images/".$row['image']."' height='66px' width='66px' ></td>";
echo "<td><input type='text' value='".$row['title']."'></td>";
echo "<td><input type='text' value='".$row['body']."'></td>";
echo "<td><input type='text' value='".$row['cost']."'></td>";
}
$data2=mysqli_query($con,"SELECT * FROM category");
echo "<td><select>";
echo "<option>Category</option>";
while($row2=mysqli_fetch_array($data2))
{
echo "<option>".$row2['category']."</option>";
}
echo "</select></td>";
First loop the results and seperate the one that goes on top, then output the select.
$cid=??;
$display_top;
$display_rest=array();
while($row2=mysqli_fetch_array($data2))
{
//if id == on_top_id set the variable on_top
if($row2['id'] === $cid){
$display_top= "<option>".$row2['category']."</option>";
}
//the rest
else{
$display_rest[] = "<option>".$row2['category']."</option>";
}
}
//output the select
echo "<td><select>";
echo "<option>Category</option>";
//first echo the id_on_top
echo $display_top;
//echo the rest
echo implode('',$display_rest);
echo "</select></td>";
I need your help to resolve this issue in my script as I am trying to
update a quantity of the product in products table based on
getting information via post from the first page.
Everything seems ok but the quantity is not getting updated in the table.
The table already had some quantity for this product.
Page 1:
$selectP="select prodid, prodname, prodtype from products where prodtype = 'BP'";
$result=mysql_query($selectP) or die (mysql_error());
echo "<form method=POST action=quantupdate.php>";
echo "<center><table border=1 cellpadding=5>";
echo "<tr><td>Select a Product to Update Quantity in Stock </td>";
echo "<td>";
echo "<select size=\"1\" name=\"product_selection\" id=\"product_selection\">";
echo "<option value=\"0\">- Product -</options>";
while($row = mysql_fetch_array($result)) {
echo "<option value='".$row['prodid']."'>".$row['prodname']."</option>";
}
echo "</select>";
echo "<tr><td>Select Quantity </td>";
echo "<td>";
echo "<select size=\"1\" name=\"pq\" id=\"pq\">";
echo "<option value=\"0\">Select Qty</options>";
echo "<option value=\"5\">5</options>";
echo "<option value=\"10\">10</options>";
echo "<option value=\"20\">20</options>";
echo "<option value=\"30\">30</options>";
echo "</select>";
echo "<tr><td><input type=submit name=submit id=submit value='Update Now'></td>";
echo "<td><input type=reset value='Clear Form'></td></tr>";
echo "</table></center>";
echo "</form>" ;
Page 2:
$bprod=$_POST['product_selection'];
$quantity=$_POST['pq'];
if(isset($_REQUEST['product_selection'])) {
$bprod=$_POST['product_selection'];
} else {
echo "Not Working???";//do something about it
}
$updatequantity="UPDATE products
SET prodquantity = ".$quantity." WHERE prodname = ".$bprod;
$exeupdatequantity=mysql_query($updatequantity);
Everything seems ok, no error message but table is not getting updated with the new quantity.
Please help.
It's problem with your update query. You try to update record by prodname, but you pass prodid to it, becouse $_POST['product_selection'] variable contains value of option, which in your case is prodid.
Also,
echo "<option value=\"0\">- Product -</options>";
should be
echo "<option value=\"0\">- Product -</option>";
Also, you should go with what Tomek said, and pass your values to query in apostrophes.
Ultimately, your query should be:
$updatequantity = "UPDATE products SET prodquantity = '".$quantity."' WHERE prodid = '".$bprod."';";
Fix your updating query:
$updatequantity="UPDATE products
SET prodquantity = '".$quantity."' WHERE prodname = '".$bprod."';";
You need to use quotes in strings in MySQL, otherwise they will be treated as column name.
And remember, that using unescaped values from $_POST / $_REQUEST / $_GET is potentially very unsafe.
Use for example mysql_escape_string to escape your values:
$quantity = mysql_escape_string($qunatity);
$bprod = mysql_escape_string($bprod);
Hi I have senario where i need to update the MySQL table which has student ID and Student Marks. Now the Student ID is unique here. How do I use only one form to update all the students marks.
$result= mysql_query("SELECT fname,usn FROM student where branch='$branch' and section='$section' and semester='$semester'") or die(mysql_error());
echo "<form action=\"marks.php\" method=\"POST\">";
echo "<table border='6' width='500' cellspacing='10' cellpadding='10' style='font-size:14px'>";
echo "<caption>";
echo "<b style='font-size:18px'>Internal</b> ";
echo "<b style='font-size:18px'>";
echo $internal;
echo "</b>";
echo " <b style='font-size:18px'>marks of</b> ";
echo "<b style='font-size:18px'>";
echo $subject;
echo "</b>";
echo "</caption>";
echo "<tr><th>USN</th><th>FNAME</th><th>MARKS</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['usn'];
echo "</td><td>";
echo $row['fname'];
echo "</td><td>";
echo "<input name=\"internal\" type=\"text\" value=\"\" >";
echo "</td></tr>";
echo "</table>";
echo "<input name=\"update\" id=\"update\"type=\"submit\" value=\"submit \"align=\"middle\" >";
Each time you print out the input field for the student mark, you could print a name attribute for the input field that uniquely identifies it - using an integer counter, for example, from 0 to N-1, where N is the number of students. You could also pass a hidden input field with the total number of students. The PHP code that receives the data then uses the hidden input field to loop over the input fields in the form data.
For example, if the input fields end up being named FIELD0 ... FIELD20, then the hidden input field has value 21, so the PHP code simply says this:
$marks = array();
for ($i = 0; $i < $NUMOFMARKS; $i++) {
$marks[] = $POST['FIELD' . $i];
}
Then build your SQL query from the array of marks.
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.
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']