PHP - Assign Database Value As Selected Value In A Drop Down Box
Hey guys, im trying to make a dropdown box get a selected value based on the value from multiple rows.
Here is what im trying to do: <?php // select the value from the database // $value = ... ?> <form action="..."> <option value="1"<?php if ($value == 1) { echo ' selected="selected"'; } ?>>First</option> <option value="2"<?php if ($value == 2) { echo ' selected="selected"'; } ?>>Seccond</option> <option value="3"<?php if ($value == 3) { echo ' selected="selected"'; } ?>>Third</option> <input type="submit"> </form> The thing is I have no idea what to put at the top and how to assign the $value part. Here is my current code. <?php mysql_connect("localhost", "", "")or die("cannot connect"); mysql_select_db("test")or die("cannot select DB"); $tbl_name="test_mysql"; $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); // MOVED this code to the top before you output anything if (isset($_POST['Submit'])) { for($i=0;$i<$count;$i++){ $month = $_POST['month']; $date = $_POST['date']; $message = $_POST['message']; $title = $_POST['title']; $id = $_POST['id']; $icon = $_POST['icon']; // ILYA $monthday= $month[$i]."<br>".$date[$i]; $sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "', icon='$icon[$i]' WHERE id=".$id[$i]; // ILYA if(!($result1 = mysql_query($sql1))){ "<BR>Error UPDATING $tbl_name "; exit(); } } } $result=mysql_query($sql); $count=mysql_num_rows($result); ?> Here is the selection dropdown box im trying to make work: <select name="icon[]" style="display:blolck; width:200px" class="mydds"> <option value="1" title="icon/icon_phone.gif">Phone</option> <option value="2" title="icon/icon_sales.gif">Graph</option> <option value="3" title="icon/icon_faq.gif">Faq</option> </select> The thing is, it gets multiple rows at once and I want to make it select each one differently based on the rows value. I dont want it to like interfere. Do you kinda know what I mean? Also, heres hows the database looks like. Picture of webpage. Here is the full code if anyone would like to see: <?php mysql_connect("localhost", "", "")or die("cannot connect"); mysql_select_db("test")or die("cannot select DB"); $tbl_name="test_mysql"; $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); // MOVED this code to the top before you output anything if (isset($_POST['Submit'])) { for($i=0;$i<$count;$i++){ $month = $_POST['month']; $date = $_POST['date']; $message = $_POST['message']; $title = $_POST['title']; $id = $_POST['id']; $icon = $_POST['icon']; // ILYA $monthday= $month[$i]."<br>".$date[$i]; $sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "', icon='$icon[$i]' WHERE id=".$id[$i]; // ILYA if(!($result1 = mysql_query($sql1))){ "<BR>Error UPDATING $tbl_name "; exit(); } } } $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Image Dropdown</title> <link rel="stylesheet" type="text/css" href="dd.css" /> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <script type="text/javascript" src="jquery.dd.js"></script> </head> <body> <form name="form1" method="post" action="updat5.php"><!--ILYA--> <tr> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>ID</strong></td> <td align="center"><strong>Month - Date</strong></td> <td align="center"><strong>Message</strong></td> <td align="center"><strong>Title</strong></td> <td align="center"><strong>Icon</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px; width: 20px;" id="id" name="id[]" value="<?php echo $rows['id']; ?>"></td> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>"> <input style="border: 1px solid #C3C3C3;height: 20px;" name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>"> </td> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="message[]" size="70" type="text" id="message" value="<?php echo $rows['message']; ?>"></td> <td align="center"><input style="border: 1px solid #C3C3C3;height: 20px;" name="title[]" size="70" type="text" id="title" value="<?php echo $rows['title']; ?>"></td> <td align="center"> <select name="icon[]" style="display:blolck; width:200px" class="mydds"> <option value="1" title="icon/icon_phone.gif">Phone</option> <option value="2" title="icon/icon_sales.gif">Graph</option> <option value="3" title="icon/icon_faq.gif">Faq</option> </select> </td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><br><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> <script language="javascript" type="text/javascript"> function showvalue(arg) { alert(arg); //arg.visible(false); } $(document).ready(function() { try { oHandler = $(".mydds").msDropDown().data("dd"); oHandler.visible(true); //alert($.msDropDown.version); //$.msDropDown.create("body select"); $("#ver").html($.msDropDown.version); } catch(e) { alert("Error: "+e.message); } }) </script> </body> </html> Similar TutorialsHey how would I post the selected value from the following code? Quote $sql="SELECT username FROM members"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $username=$row["username"]; $options.="<OPTION VALUE=\"$id\">".$username; } ?> <SELECT NAME="person"> <OPTION VALUE=0>Choose <?=$options?> </SELECT> I've got a HTML drop down box as similar to this: <select name="dropdown"> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> If I run a mysql query and get a result of "Option 3", is there anyway using PHP to give Option 3 the selected value? Hi, A quick question please , I am trying to assign a selected index to a drop down list based on data retrieved from the database. I tried using JavaScript : document.getElementById('Country').selectedIndex ='".$array['Country']."'; but it does work. Any suggestion? Hi all I am trying to create a menu that shows the initially selected value depending on which page the user is on: <?php $fetchcategories=mysql_query("SELECT * FROM `product_categories` ORDER BY id ASC"); while($returnedcategories=mysql_fetch_array($fetchcategories)) { echo "<option value=\"".$returnedcategories['name']."\" selected=\"selected\" >".$returnedcategories['name']."</option>"; } ?> There are three product categories; Stone, Wood and Accessories. in the SQL. When on the stone.php page I need the option value 'stone' to stay selected. Many thanks for your help Pete I have several drop down boxes. When submit is clicked it execute query on another page. Problem is when submit is clicked selected values are return to defaults. Is it possible for the values to be remembered after submitting them? Part of the drop down code: Code: [Select] <?php $result = mysql_query("SELECT * FROM product GROUP BY Rim"); ?> <select id="Rim" name="Rim" selected="selected" style="width:158px;position:static;z-index:-1;"> <option value="rim" selected="selected"><?php echo "Rim"; ?></option> <?php while ($row = mysql_fetch_array($result)) { echo "<option>" . $row['Rim'] . "</option>"; echo "<br />"; } ?> </select> Hi all I am trying to get my drop down menu to use a session variable as the selected menu option. Here's my code: echo "<option value=\"\">Delivery Area</option>"; $selected = ""; if(isset($_SESSION['postal_area']) && $_SESSION['postal_area'] == "England") { $selected = "selected"; } elseif(isset($_SESSION['postal_area']) && $_SESSION['postal_area'] == "Wales") { $selected = "selected"; } elseif(isset($_SESSION['postal_area']) && $_SESSION['postal_area'] == "Scotland") { $selected = "selected"; } else { $selected = ""; } echo " <option value=\"England\" selected=\"".$selected."\">England</option>"; echo "<option value=\"Scotland\" selected=\"".$selected."\">Scotland</option>"; echo "<option value=\"Wales\" selected=\"".$selected."\">Wales</option>"; echo "</select>"; However, this shows the 'Wales' one all the? Please help! Many thanks Pete Hey guys I keep getting the error "Database not selected" with this script <?php session_start(); if ($_SESSION['adminlogin'] == 1){ //Run } else { header('Location: Log-In.php'); exit; } ?> <!DOCTYPE HTML> <html lang="en-GB"> <head> <meta charset="utf-8"> <!--Search Engine Meta Tags--> <meta name="author" content="Worldwide Lighthouses"> <meta name="keywords" content="Lighthouses,Lightships,Trinity House,Fog Signals,Fog Horns,Fresnel"> <meta name="description" content="Worldwide Lighthouses is the number 1 source of information, pictures and videos on the Subject of Lighthouses and Lightships"> <!--Stylesheets/Javascript--> <link rel="stylesheet" href="../../Page-Layout.css" media="screen and (min-width: 481px)"> <link rel="stylesheet" href="../../Mobile-Page-Layout.css" media="only screen and (max-width:480px)"> <!--Mobile Browser Support--> <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <!--IE Support--> <!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <link rel="stylesheet" href="../Page-Layout.css"><![endif]--> <meta name="application-name" content="Worldwide Lighthouses"> <meta name="msapplication-starturl" content="http://worldwidelighthouses.com/"> <meta name="msapplication-tooltip" content="Worldwide Lighthouses: Your number one source of Lighthouse Information, Videos and Pictures"> <meta name="msapplication-task" content="name=Lighthouses;action-uri=http://worldwidelighthouses.com/Lighthouses.php;icon-uri=http://worldwidelighthouses.com/IE9/Lighthouses.ico"> <meta name="msapplication-task" content="name=Lightships;action-uri=http://worldwidelighthouses.com/Lightships.php;icon-uri=http://worldwidelighthouses.com/IE9/Lightships.ico"> <meta name="msapplication-task" content="name=Fog Signals;action-uri=http://worldwidelighthouses.com/Fog-Signals.php;icon-uri=http://worldwidelighthouses.com/IE9/Fog-Signals.ico"> <meta name="msapplication-task" content="name=Glossary;action-uri=http://worldwidelighthouses.com/Glossary.php;icon-uri=http://worldwidelighthouses.com/IE9/Glossary.ico"> <title>Mailing List Administration | Worldwide Lighthouses</title> </head> <body> <header> <h1 id="WWLH">Worldwide Lighthouses</h1> <form method="get" action="http://www.worldwidelighthouses.com/Search/search.php" id="Search-Box"> <input type="search" placeholder="Search Worldwide Lighthouses" name="query" id="query" size="30" value="" autocomplete="off"> <input type="submit" value="Search"> <input type="hidden" name="search" value="1"> </form> </header> <nav> <ul id="Nav"> <li class="MenuButton" id="Index"><a href="../Index.php"><p class="Nav">Home</p></a></li> <li class="MenuButton" id="Lighthouses"><a href="../Lighthouses.php"><p class="Nav">Lighthouses</p></a></li> <li class="MenuButton" id="Lightships"><a href="../Lightships.php"><p class="Nav">Lightships</p></a></li> <li class="MenuButton" id="FogSignals"><a href="../Fog-Signals.php"><p class="Nav">Fog Signals</p></a></li> <li class="MenuButton" id="Daymarks"><a href="../Daymarks.php"><p class="Nav">Daymarks</p></a></li> <li class="MenuButton" id="Buoys"><a href="../Buoys.php"><p class="Nav">Buoys</p></a></li> <li id="MenuButtonLast"><a href="../Glossary.php"><p class="Nav">Glossary</p></a></li> </ul> </nav> <?php if ($_SESSION['adminlogin']==1) { echo '<div id="logout"> <div style="float:left; width:30%; text-align:left;!important"> <a href="Log-In-Accept-Deny.php">Back to Admin Home</a> </div> <div style="float:right; width:70%;"> <a href="Logout.php">Log Out of Admin</a> <p style="font-size:10px;">Always Sign Out when Finished!</p> </div></div>';} ?> <article> <h1 class="Title">Update Contact Record</h1> <div class="Textbox"> <? $id=$_GET['id']; $username="worldw44_php"; $password="c@rtmeldrive41"; $database="worldw44_newsemail"; mysql_connect(localhost,$username,$password); $query="SELECT * FROM contacts WHERE id='$id'"; $result=mysql_query($query) or die( 'Error: ' . mysql_error() ); $num = mysql_num_rows($result); $i=0; while ($i < $num) { $name=mysql_result($result,$i,"name"); $email=mysql_result($result,$i,"email"); ?> <form action="updated.php" method="post"> <input type="hidden" name="ud_id" value="<? echo $id; ?>"> Name: <input type="text" name="ud_first" value="<? echo $name; ?>"><br> E-mail: <input type="text" name="ud_email" value="<? echo $email; ?>"><br> <input type="Submit" value="Update"> </form> <? ++$i; } mysql_close(); ?> <?php if ($_SESSION['adminlogin'] == 1){ echo "Logged in on server side."; }?> </div> </article> <footer> <ul> <li><a href="http://www.worldwidelighthouses.com/About.php">About</a></li> <li><a href="http://www.worldwidelighthouses.com/Contact-us.php">Contact</a></li> <li><a href="http://www.worldwidelighthouses.com/Use-Our-Media.php">Use our media</a></li> <li><a href="http://www.worldwidelighthouses.com/Search/search.php">Search</a></li> <li><a href="http://www.worldwidelighthouses.com/Social-Networking.php">Social</a></li> <li><a href="#Top">Back to top</a></li> </ul> <br> <br> &#169; Worldwide Lighthouses <?php echo date("Y"); ?> </footer> </body> I have no idea why? Any help? AS far as my understanding i have selected it as "Contactinfo"? Thanks Danny Hi all,
I am trying to validate all required fields in my contact form using php. However I am unable to retain what was selected in my country dropdown list after it is selected and submitted if the form had errror while validating. I want the form to retain the value that was selected by the user to stay in the form so that users can make necessary changes and resubmit the form all other values stay in the form except the country dropdown. Please help Below is the code. <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php // define variables and set to empty values $nameErr = $emailErr = $genderErr = $websiteErr = $countryErr = ""; $name = $email = $gender = $comment = $website = ""; $country =""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); // check if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = test_input($_POST["email"]); // check if e-mail address syntax is valid if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "Invalid email format"; } } if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); // check if URL address syntax is valid (this regular expression also allows dashes in the URL) if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "Invalid URL"; } } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = "Gender is required"; } else { $gender = test_input($_POST["gender"]); } $country = $_POST['country']; if (!in_array($country, array("AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR", "CI", "HR", "CU", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "Gg", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "AN", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", "RO", "RU", "RW", "SH", "KN", "LC", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "CS", "SC", "SL", "SG", "SK", "SI", "SB", "SO", "ZA", "GS", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW" ))) { $country1 = "Please select your country";} } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>contact us</h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name" value="<?php echo $name;?>"> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email" value="<?php echo $email;?>"> <span class="error">* <?php echo $emailErr;?></span> <br><br> <legend>Country:</legend> <select name="country"> <option value="NULL">select</option> <option value="AF">Afghanistan</option> <option value="AX">Ã…Land Islands</option> <option value="AL">Albania</option> <option value="DZ">Algeria</option> <option value="AS">American Samoa</option> <option value="AD">Andorra</option> <option value="AO">Angola</option> <option value="AI">Anguilla</option> <option value="AQ">Antarctica</option> <option value="AG">Antigua And Barbuda</option> <option value="AR">Argentina</option> <option value="AM">Armenia</option> <option value="AW">Aruba</option> <option value="AU">Australia</option> <option value="AT">Austria</option> <option value="AZ">Azerbaijan</option> <option value="BS">Bahamas</option> <option value="BH">Bahrain</option> <option value="BD">Bangladesh</option> <option value="BB">Barbados</option> <option value="BY">Belarus</option> <option value="BE">Belgium</option> <option value="BZ">Belize</option> <option value="BJ">Benin</option> <option value="BM">Bermuda</option> <option value="BT">Bhutan</option> <option value="BO">Bolivia</option> <option value="BA">Bosnia And Herzegovina</option> <option value="BW">Botswana</option> <option value="BV">Bouvet Island</option> <option value="BR">Brazil</option> <option value="IO">British Indian Ocean Territory</option> <option value="BN">Brunei Darussalam</option> <option value="BG">Bulgaria</option> <option value="BF">Burkina Faso</option> <option value="BI">Burundi</option> <option value="KH">Cambodia</option> <option value="CM">Cameroon</option> <option value="CA">Canada</option> <option value="CV">Cape Verde</option> <option value="KY">Cayman Islands</option> <option value="CF">Central African Republic</option> <option value="TD">Chad</option> <option value="CL">Chile</option> <option value="CN">China</option> <option value="CX">Christmas Island</option> <option value="CC">Cocos (Keeling) Islands</option> <option value="CO">Colombia</option> <option value="KM">Comoros</option> <option value="CG">Congo</option> <option value="CD">Congo, The Democratic Republic Of The</option> <option value="CK">Cook Islands</option> <option value="CR">Costa Rica</option> <option value="CI">Cote D'Ivoire</option> <option value="HR">Croatia</option> <option value="CU">Cuba</option> <option value="CY">Cyprus</option> <option value="CZ">Czech Republic</option> <option value="DK">Denmark</option> <option value="DJ">Djibouti</option> <option value="DM">Dominica</option> <option value="DO">Dominican Republic</option> <option value="EC">Ecuador</option> <option value="EG">Egypt</option> <option value="SV">El Salvador</option> <option value="GQ">Equatorial Guinea</option> <option value="ER">Eritrea</option> <option value="EE">Estonia</option> <option value="ET">Ethiopia</option> <option value="FK">Falkland Islands (Malvinas)</option> <option value="FO">Faroe Islands</option> <option value="FJ">Fiji</option> <option value="FI">Finland</option> <option value="FR">France</option> <option value="GF">French Guiana</option> <option value="PF">French Polynesia</option> <option value="TF">French Southern Territories</option> <option value="GA">Gabon</option> <option value="GM">Gambia</option> <option value="GE">Georgia</option> <option value="DE">Germany</option> <option value="GH">Ghana</option> <option value="GI">Gibraltar</option> <option value="GR">Greece</option> <option value="GL">Greenland</option> <option value="GD">Grenada</option> <option value="GP">Guadeloupe</option> <option value="GU">Guam</option> <option value="GT">Guatemala</option> <option value="Gg">Guernsey</option> <option value="GN">Guinea</option> <option value="GW">Guinea-Bissau</option> <option value="GY">Guyana</option> <option value="HT">Haiti</option> <option value="HM">Heard Island And Mcdonald Islands</option> <option value="VA">Holy See (Vatican City State)</option> <option value="HN">Honduras</option> <option value="HK">Hong Kong</option> <option value="HU">Hungary</option> <option value="IS">Iceland</option> <option value="IN">India</option> <option value="ID">Indonesia</option> <option value="IR">Iran, Islamic Republic Of</option> <option value="IQ">Iraq</option> <option value="IE">Ireland</option> <option value="IM">Isle Of Man</option> <option value="IL">Israel</option> <option value="IT">Italy</option> <option value="JM">Jamaica</option> <option value="JP">Japan</option> <option value="JE">Jersey</option> <option value="JO">Jordan</option> <option value="KZ">Kazakhstan</option> <option value="KE">Kenya</option> <option value="KI">Kiribati</option> <option value="KP">Korea, Democratic People'S Republic Of</option> <option value="KR">Korea, Republic Of</option> <option value="KW">Kuwait</option> <option value="KG">Kyrgyzstan</option> <option value="LA">Lao People'S Democratic Republic</option> <option value="LV">Latvia</option> <option value="LB">Lebanon</option> <option value="LS">Lesotho</option> <option value="LR">Liberia</option> <option value="LY">Libyan Arab Jamahiriya</option> <option value="LI">Liechtenstein</option> <option value="LT">Lithuania</option> <option value="LU">Luxembourg</option> <option value="MO">Macao</option> <option value="MK">Macedonia, The Former Yugoslav Republic Of</option> <option value="MG">Madagascar</option> <option value="MW">Malawi</option> <option value="MY">Malaysia</option> <option value="MV">Maldives</option> <option value="ML">Mali</option> <option value="MT">Malta</option> <option value="MH">Marshall Islands</option> <option value="MQ">Martinique</option> <option value="MR">Mauritania</option> <option value="MU">Mauritius</option> <option value="YT">Mayotte</option> <option value="MX">Mexico</option> <option value="FM">Micronesia, Federated States Of</option> <option value="MD">Moldova, Republic Of</option> <option value="MC">Monaco</option> <option value="MN">Mongolia</option> <option value="MS">Montserrat</option> <option value="MA">Morocco</option> <option value="MZ">Mozambique</option> <option value="MM">Myanmar</option> <option value="NA">Namibia</option> <option value="NR">Nauru</option> <option value="NP">Nepal</option> <option value="NL">Netherlands</option> <option value="AN">Netherlands Antilles</option> <option value="NC">New Caledonia</option> <option value="NZ">New Zealand</option> <option value="NI">Nicaragua</option> <option value="NE">Niger</option> <option value="NG">Nigeria</option> <option value="NU">Niue</option> <option value="NF">Norfolk Island</option> <option value="MP">Northern Mariana Islands</option> <option value="NO">Norway</option> <option value="OM">Oman</option> <option value="PK">Pakistan</option> <option value="PW">Palau</option> <option value="PS">Palestinian Territory, Occupied</option> <option value="PA">Panama</option> <option value="PG">Papua New Guinea</option> <option value="PY">Paraguay</option> <option value="PE">Peru</option> <option value="PH">Philippines</option> <option value="PN">Pitcairn</option> <option value="PL">Poland</option> <option value="PT">Portugal</option> <option value="PR">Puerto Rico</option> <option value="QA">Qatar</option> <option value="RE">Reunion</option> <option value="RO">Romania</option> <option value="RU">Russian Federation</option> <option value="RW">Rwanda</option> <option value="SH">Saint Helena</option> <option value="KN">Saint Kitts And Nevis</option> <option value="LC">Saint Lucia</option> <option value="PM">Saint Pierre And Miquelon</option> <option value="VC">Saint Vincent And The Grenadines</option> <option value="WS">Samoa</option> <option value="SM">San Marino</option> <option value="ST">Sao Tome And Principe</option> <option value="SA">Saudi Arabia</option> <option value="SN">Senegal</option> <option value="CS">Serbia And Montenegro</option> <option value="SC">Seychelles</option> <option value="SL">Sierra Leone</option> <option value="SG">Singapore</option> <option value="SK">Slovakia</option> <option value="SI">Slovenia</option> <option value="SB">Solomon Islands</option> <option value="SO">Somalia</option> <option value="ZA">South Africa</option> <option value="GS">South Georgia And The South Sandwich Islands</option> <option value="ES">Spain</option> <option value="LK">Sri Lanka</option> <option value="SD">Sudan</option> <option value="SR">Suriname</option> <option value="SJ">Svalbard And Jan Mayen</option> <option value="SZ">Swaziland</option> <option value="SE">Sweden</option> <option value="CH">Switzerland</option> <option value="SY">Syrian Arab Republic</option> <option value="TW">Taiwan, Province Of China</option> <option value="TJ">Tajikistan</option> <option value="TZ">Tanzania, United Republic Of</option> <option value="TH">Thailand</option> <option value="TL">Timor-Leste</option> <option value="TG">Togo</option> <option value="TK">Tokelau</option> <option value="TO">Tonga</option> <option value="TT">Trinidad And Tobago</option> <option value="TN">Tunisia</option> <option value="TR">Turkey</option> <option value="TM">Turkmenistan</option> <option value="TC">Turks And Caicos Islands</option> <option value="TV">Tuvalu</option> <option value="UG">Uganda</option> <option value="UA">Ukraine</option> <option value="AE">United Arab Emirates</option> <option value="GB">United Kingdom</option> <option value="US">United States</option> <option value="UM">United States Minor Outlying Islands</option> <option value="UY">Uruguay</option> <option value="UZ">Uzbekistan</option> <option value="VU">Vanuatu</option> <option value="VE">Venezuela</option> <option value="VN">Viet Nam</option> <option value="VG">Virgin Islands, British</option> <option value="VI">Virgin Islands, U.S.</option> <option value="WF">Wallis And Futuna</option> <option value="EH">Western Sahara</option> <option value="YE">Yemen</option> <option value="ZM">Zambia</option> <option value="ZW">Zimbabwe</option> </select> <span class="error">*<?php echo $country1;?></span> <br><br> Website: <input type="text" name="website" value="<?php echo $website;?>"> <span class="error"><?php echo $websiteErr;?></span> <br><br> Gender: <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male <span class="error">* <?php echo $genderErr;?></span> <br><br> Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> <br><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> Edited by Ch0cu3r, 15 July 2014 - 03:37 PM. Reformatted post Hi all I need to combine these two scripts: Firstly, the following decides which out of the following list is selected based on its value in the mySQL table: <select name="pack_choice"> <option value="Meters / Pack"<?php echo (($result['pack_choice']=="Meters / Pack") ? ' selected="selected"':'') ?>>Meters / Pack (m2)</option> <option value="m3"<?php echo (($result['pack_choice']=="m3") ? ' selected="selected"':'') ?>>Meters / Pack (m3)</option> <option value="Quantity"<?php echo (($result['pack_choice']=="Quantity") ? ' selected="selected"':'') ?>>Quantity</option> </select> Although this works OK, I need it also to show dynamic values like this: select name="category"> <?php $listCategories=mysql_query("SELECT * FROM `product_categories` ORDER BY id ASC"); while($categoryReturned=mysql_fetch_array($listCategories)) { echo "<option value=\"".$categoryReturned['name']."\">".$categoryReturned['name']."</option>"; } ?> </select> I'm not sure if this is possible? Many thanks for your help. Pete mysql.php <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ckeditor",$con); ?> --------------------------------------------------------- add.php <?php include("mysql.php"); if(isset($_POST["button2"])) { $sql="INSERT INTO cktext (section) VALUES ('$_POST[select2]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } ?> ----------------------------------------------------------------- home.php <form id="form1" name="form1" method="post" action="add.php"> <tr> <td>Section:</td> <td><select name="select2" id="select2"> <option selected="selected" value="MALE">Male</option> <option selected="" value="FEMAIL">FEMAIL</option> </select></td> </tr> <input type="submit" name="button2" id="button2" value="Upload" /> </form> In DATABASE :- cktext table attribute "section" is varchar type. BUT IT RETURN ME BLANK OUTPUT. hi. i am making mcq type test. lot of questions stored in db, i want that some of them retrieve i.e. 5 after 5 test should end. how is hould do, query is ...SELECT question,questionid, choice1, choice2, choice3, choice4, answer FROM questionbank order by RAND() limit 1";... limit 1 is used for display one question in a page. Hi, I am new to php and need help. I am building a library system for an assignment. I want to provide a functionality that registers (adds) all transactions to defferent databases. I am stuck on how to add a selected row (selected by a checkbox) to the database and make sure that 2 users cannot update the same record at the same time. Thanks for your help. bellow is my code: Code: [Select] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta name="generator" content="PSPad editor, www.pspad.com"> <title>Web Works DD - Web Development & Software Solutions</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="css/style.css" /> </head> <body> <!--<div class="utilities"> <!-- UTILITIES MENU -- <a href="index.php" style=text-decoration:none;><img src="images/home.png" border="0" alt="Home Page"/> Home Page</a> <a href="sitemap.php" style=text-decoration:none;><img src="images/sitemap.png" border="0" alt="Sitemap"/> Sitemap</a> </div><!-- END UTILITIES --> <div id="right_bar"> <!-- right bar starts here --> </div> <!-- end of right bar --> <div id="top_bar"> <!-- top bar starts here --> <a href="index.php" style=text-ecoration:none; title="Web Works DD - Web Development & Software Solutions"><img src="images/mainLib.jpg" border=0 style=height:180;/> </a> <img src="images/topText.jpg" border=0 style=height:180;/> </div> <!-- end of top bar --> <div id="container"> <div id="top-logo"> <!--<a href="index.php" style=text-ecoration:none; title="Web Works DD - Web Development & Software Solutions"><img src="images/Main_logo.jpg" border=0 /> </a> --> </div> <ul class="dropdown dav"> <li><a href="index1.php">HOME</a></li> <li><a href="#" class="dir">VIEW RWSOURCES</a> <ul> <li><a href="books.php">BOOKS</a></li> <li><a href="dvds.php">DVDS</a></li> <li><a href="journals.php">JOURNALS</a></li> </ul> </li> <li><a href="staffLogin.php">STAFF ZONE</a></li> <li><a href="studentsLogin.php">STUDENT ZONE</a></li> </ul> <table> <!--This defines a new form --> <form action="" method="post"> <!--This creates the dropdown in html--> <select name="fields"> <option value="1">Please select...</option> <option value="isbnno">isbnno</option> <option value="title">title</option> <option value="description">description</option> <option value="author">author</option> </select> </td> </tr> <tr> <td> <input type="text" name="value" size="40"> </td> </tr> <tr> <td> <!-- Create a button --> <input type="submit" value="Select" name="select"> </td> </tr> </form> </table> <?php session_start(); include("secure/db_open.php"); /* $myusername=$_SESSION['myusername']; echo $myusername; $insert = $checkbox[$i]; $cops = $_POST['copies']; $userid ="SELECT id FROM students WHERE username = '$myusername'"; $uido = mysql_query($userid); $row = mysql_fetch_array($uido); echo $row['id']; */ // get variable after selecting something from the dropdown with name 'fields' $select = $_POST['select']; $valu = $_POST['value']; // if something has been chosen if (!empty($select)) { $fields = $_POST['fields']; //get the chosen value $allbooks="SELECT * FROM books WHERE {$fields} = '$valu'"; //select books that are available - driven by the user's input $copas="SELECT * FROM books WHERE {$fields} = '$valu' AND available_copies > 0"; $copy = mysql_query($copas); //count the number of rows resulting from the query $count=mysql_num_rows($copy); //$copse = mysql_fetch_array($copy); //select books that are not available $notavailable="SELECT * FROM books WHERE {$fields} = '$valu' AND available_copies = 0"; $shownotavailable=mysql_query($notavailable); $count1=mysql_num_rows($shownotavailable); //display a form echo "<table width='1100' border='0' cellspacing='1' cellpadding='0'>"; echo "<tr>"; echo "<td>" ; echo "<form name='form1' method='post' action=''>"; echo "<table width='1100' border='0' cellpadding='3' cellspacing='1' bgcolor='#CCCCCC'>"; echo "<tr>"; echo "<td align='center' colspan='9' bgcolor='#FFFFFF'>"; echo "<strong>Available to Loan </strong>" ; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td align='center' bgcolor='#FFFFFF'>Loan</td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>ISBN No.</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Title</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Description</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Author</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Loan Type</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Location</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Available Copies</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Desired Num of Copies</strong></td>"; echo "</tr>"; while(($row = mysql_fetch_assoc($copy))) { echo "<tr>"; echo "<td align='center' bgcolor='#FFFFFF'><input name='checkbox[]' type='checkbox' id='checkbox[]' value=".$row['bookid']."></td>"; echo "<td>" .$row['isbnno']. "</td>"; echo "<td>" .$row['title']. "</td>"; echo "<td>" .$row['description']. "</td>"; echo "<td>" .$row['author']. "</td>"; echo "<td>" .$row['loan_type']. "</td>"; echo "<td>" .$row['location']. "</td>"; echo "<td>" .$row['available_copies']. "</td>"; echo "<td align='center' bgcolor='#FFFFFF'><input name='copies' type='text' id='copies' size='2'></td>"; echo "</tr>"; } echo "<tr>"; echo "<td colspan='9' align='center' bgcolor='#FFFFFF'><input name='loan' type='submit' id='loan' value='Loan Book'></td>"; echo "</tr>"; echo "</table>"; if (isset($_POST)) { session_start(); $myusername=$_SESSION['myusername']; $cops = $_POST['copies']; $userid ="SELECT id FROM students WHERE username = $myusername"; $uido = mysql_query($userid); $row1 = mysql_fetch_array($uido); $sid = $row1['id']; for ($i=0;$i<$count;$i++) { $insert = $checkbox[$i]; //echo "Welcome, ".$row['id']; $insertloaned="INSERT INTO studentloanedbooks(studentid, bookid, noofcopies) VALUES ($sid, $insert, $cops)"; $loaned = mysql_query($insertloaned); } } if($count1>0) { //display the books details //display a form echo "<br>"; echo "<br>"; echo "<table width='1100' border='0' cellspacing='1' cellpadding='0'>"; echo "<tr>"; echo "<td>" ; echo "<form name='form1' method='post' action=''>"; echo "<table width='1100' border='0' cellpadding='3' cellspacing='1' bgcolor='#CCCCCC'>"; echo "<tr>"; echo "<td align='center' colspan='9' bgcolor='#FFFFFF'>"; echo "<strong>Available to Loan </strong>" ; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td align='center' bgcolor='#FFFFFF'>Loan</td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>ISBN No.</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Title</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Description</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Author</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Loan Type</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Location</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Available Copies</strong></td>"; echo "<td align='center' bgcolor='#FFFFFF'><strong>Desired Num of Copies</strong></td>"; echo "</tr>"; while(($row = mysql_fetch_assoc($shownotavailable))) { echo "<tr>"; echo "<td align='center' bgcolor='#FFFFFF'><input name='checkbox[]' type='checkbox' id='checkbox[]' value='.$row[bookid]'></td>"; echo "<td>" .$row['isbnno']. "</td>"; echo "<td>" .$row['title']. "</td>"; echo "<td>" .$row['description']. "</td>"; echo "<td>" .$row['author']. "</td>"; echo "<td>" .$row['loan_type']. "</td>"; echo "<td>" .$row['location']. "</td>"; echo "<td>" .$row['available_copies']. "</td>"; echo "<td align='center' bgcolor='#FFFFFF'><input name='copies' type='text' id='copies' size='2'></td>"; echo "</tr>"; } echo "<tr>"; echo "<td colspan='9' align='center' bgcolor='#FFFFFF'><input name='reserve' type='submit' id='reserve' value='Reserve Book'></td>"; echo "</tr>"; echo "</table>"; } } // include("secure/db_close.php"); //include("secure/db_close.php"); ?> <!--if($cop>0){ --> <!-- this is my footer div --> <div id="footer"> Copyright © 2010 | <a href="privacy.php">Privacy Policy</a> | <a href="terms.php">Terms of Service</a> <img src="images/mini_logo1.png" align="right" style="margin-right:5" /> </div> <!-- End footer div --> </div> </div> </body> </html> MOD EDIT: [code] . . . [/code] BBCode tags added. I have a form gathering data from the database. There is one field 'country' which is a combo box. The combo box is populated from the countries table which joins the customers table to provide the country specific to the customer. I'm trying to have the combo box display the country associated with the customer. I think I'm close in my code, but unsure. This is a playground to so I can implement on our other site. <?php require_once('database.php'); $sql2 = "SELECT * From countries "; $countries = $db->query($sql2); if(isset($_GET['customerID'])) { $customerID = filter_input(INPUT_GET, 'customerID', FILTER_SANITIZE_NUMBER_INT); $sql = "SELECT * FROM customers WHERE customerID =$customerID "; //$sql2 = "SELECT * From countries //INNER JOIN customers ON countries.countryCode=customers.countryCode //WHERE customers.customerID = $customerID"; $stmt = $db->query($sql); } if(isset($_GET['customerID'])){ $customerID = filter_input(INPUT_GET, 'customerID', FILTER_SANITIZE_NUMBER_INT); $countryQuery = " {$sql2} INNER JOIN customers ON countries.countryCode = customers.countryCode WHERE customers.customerID = $customerID"; $countriesQuery = $db->prepare($countryQuery); $countriesQuery->execute(['customerID' => $_GET['customerID']]); $selectedCountry = $countriesQuery->fetch(PDO::FETCH_ASSOC); var_dump($selectedCountry); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- the head section --> <head> <title>My Guitar Shop</title> <link rel="stylesheet" type="text/css" href="main.css" /> </head> <!-- the body section --> <body> <div id="page"> <div id="header"> <h1>SportsPro Technical Support</h1> <p>Sports management software for the sports enthusiast.</p></h1> </div> <div id="main"> <h1>View/Update Customer</h1> <form action="update.php" method="get" > <?php ?> <div id="content"> <!-- display a table of products --> <h2>Customers</h2> <form name="customerInfo"> <?php foreach ($stmt as $cust) { ?> <div> <label>First Name</label> <input type="text" name="name" class ="form-control" value ="<?php echo $cust['firstName']; ?>"> </div><br> <div> <label>Last Name</label> <input type="text" name="name" class ="form-control" value ="<?php echo $cust['lastName']; ?>"> </div><br> <div> <label>Address</label> <input type="text" name="address" class ="form-control" value ="<?php echo $cust['address']; ?>"> </div><br> <div> <label>City</label> <input type="text" name="city" class ="form-control" value ="<?php echo $cust['city']; ?>"> </div><br> <div> <label>State</label> <input type="text" name="state" class ="form-control" value ="<?php echo $cust['state']; ?>"> </div><br> <form action="update.php" method="get"> <select name="country"> <option value=""></option> <?php foreach ($countries->fetchAll() as $country): ?> <option value="<?php echo $country['customerID']; ?> <?php echo isset($customerID) == $selectedCountry['customerID'] ? ' selected':''?> "><?php echo $country['countryName']; ?></option> <?php endforeach;?> </select> </form> </div> <br> <div> <label>Country Code</label> <input type="text" name="countryCode" class ="form-control" value ="<?php echo $cust['countryCode']; ?>"> </div><br> <div> <label>Zip Code</label> <input type="text" name="postalCode" class ="form-control" value ="<?php echo $cust['postalCode']; ?>"> </div><br> <div> <label>Email </label> <input type="text" name="email" class ="form-control" value ="<?php echo $cust['email']; ?>"> </div><br> <div> <label>Phone Number </label> <input type="text" name="phone" class ="form-control" value ="<?php echo $cust['phone']; ?>"> </div><br> <div> <label>Password </label> <input type="text" name="password" class ="form-control" value ="<?php echo $cust['password']; ?>"> </div><br> <div> <?php } ?> </div> </div> <form action="UpdateCustomer.php" method="get"> <input type="submit" name="data" value="Update_Data"></input> </form> <div id="footer"> <p> © <?php echo date("Y"); ?> SportsPro, Inc. </p> </div> </div><!-- end page --> </body> </html>
Hello everyone, I'm new to php/mysql and am trying to setup a basic online address book script that has come straight from a tutorial book. I've created the tables etc and have the correct login details. I thought that was it but somehow I keep getting 'no database selected' in the top-left of the browser. Below is the file for opening the connection, is this a job for mysql_select_db and if so where would it need to go? i've been playing around but just cant get rid of that damn message, any help is massively appreciated : (login details hidden for security) <?php function doDB() { global $mysqli; //server and database connection $mysqli = mysqli_connect("serveraddress", "user", "pass"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } } ?> Thanks for having a look! I have an audio mix submitting form on my website. The first part of the form takes the details from the user and inserts a new row in the first table 'mix', auto incrementing to generate a mix_id primary key. The second part then uses a series of check boxes to select which genres the mix contains and a foreach for the resulting array to insert, into the 2nd table 'genres_for_mix', a new row for each genre id (foreign key for the genres table) that is selected and the mix_id that it concerns. Now I'm starting to think this is a bad way to do this because: 1. I don't know how to echo this out when a user edit's the mix to make all the relevant checkboxes selected. 2. When a user deselects a choice, it would be quite long winded to delete the nessesary row. So instead I'm thinking that it would be much wiser to user the serialise functions to put the array into the database However, I still don't know how to make the right checkboxes selected when I echo out the edit form. Do I use something like inarray()? If so how? Also, it's really important that I can query the database for mixes that are associated with a specific genre. Without unserialising the array and therefore echo'ing out all the results, I don't know how to query this. Would I just use a 'WHERE genre_field LIKE 'genre_id'? Can anyone help me with this please? Hey Everyone... First off, I am only a young web developer and i'm working on a school project and am making a text-based game online... Now what i'm having trouble with... I want a drop-down list that has a list of characters classes Clubber Mixer Sauceror Tamer And I want whatever is selected to be placed into the database along with the username/password (THIS ALL WORKS FINE JUST NOT THE DROP DOWN LIST) All help appreciated Hi Good day Can someone pls help me regarding with this matter. I have a page for inserting records. And I have a combobox that is fetched through the specific field in database. I also have a table of records, for each row i have an href edit which throws the values to its respective texbox and combobox. THE PROBLEM is that when i click the href edit, the value from a selected row was not displayed from the combobox Hey can anyone please help? I making this form on my website and I have two drop down lists, one is for sizes and the other is for colors. They both have an option for "other" and I'm wanting a textbox to appear when "other" is selected. I got the first drop down to work but I can't figure out how to get the second one to work without messing with the first. Here is the code I have please help: <form name="form1" method="post" action=""> <script type="text/javascript"> function showfield(name){ if(name=='Other')document.getElementById('div1').innerHTML='Other: <input type="text" name="other" />'; else document.getElementById('div1').innerHTML=''; } </script> <p>Size: <select name="size" id="size" onchange="showfield(this.options[this.selectedIndex].value)"> <option selected="selected">Please select ...</option> <option value="1' H X 6" W">2' H X 1' 6" W</option> <option value="1' H X 2' W">1' H X 2' W</option> <option value="1' H X 1' 4" W">1' H X 1' 4" W</option> <option value="Other">Other</option> </select> <div id="div1"></div> <p>Color: <select name="Color" id="Color" onchange="showfield(this.options[this.selectedIndex].value)"> <option selected="selected">Please select ...</option> <option value="Walnut Stain/Dark Brown">Walnut Stain/Dark Brown</option> <option value="Tan">Tan</option> <option value="Walnut">Walnut</option> <option value="Other">Other</option> </select> <div id="div1"></div> </p> I have a webpage where all the database items are displyes in a table format.The table also has a check box.Upon clicking the delete button i need to delete all the items whish has the checkbox checked. How will i do that |