Manhag Posted November 21, 2012 Share Posted November 21, 2012 (edited) hello i am trying to update database using select box value i failed to do that <?php include('lib/db.php'); $id = htmlspecialchars($_GET["fid"]); $query1 = "SELECT `city` FROM `salaty_fb`.`users` WHERE `id`='$id'"; if ($result1 = mysqli_query($link, $query1)) { while($result = mysqli_fetch_array($result1)) { $city_db = $result['city']; } //$link->close(); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <div style="<? if (isset($_POST['submit'])) {echo "display:none";} ?>;direction:rtl;" > <form method="post" action="updatesql.php?fid=<? echo $facebook_id; ?>" target="POPUPW" onsubmit="POPUPW = window.open('about:blank','POPUPW', 'width=200,height=100');"> <?php echo "<select name=\"choose_city\">\n"; $choose_city = array('0' => 'Please choose user group', 'eg_cairo' => 'cairo', 'eg_alex' => 'alex', 'eg_giza' => 'giza'); foreach($choose_city as $city_key => $city_realname) { echo "<option value=\"$city_key\" name=\"$city_key\" id=\"$city_key\" "; if($city_key == $city_db) { echo " selected"; } echo ">$city_realname</option>\n"; } echo "</select><p />"; ?> <input type="submit" name="submit" value="Submit"> <br> <br> </form> </div> <?php } else { die(mysqli_error($link)); } if (isset($_POST['submit'])) { echo $_POST['city']; echo "||"; //get value of selected box $location = $_POST['city_key']; $query ="UPDATE `salaty_fb`.`users` SET `city`='$location' WHERE `id`='$id'"; if ($result = mysqli_query($link, $query)) { printf("تم حفظ التحديثات"); echo '<script language="javascript"> <!-- setTimeout("self.close();",1000) //--> </script> '; /* free result set */ // $result->close(); $link->close(); } else { die(mysqli_error($link)); } } ?> Edited November 21, 2012 by Manhag Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/ Share on other sites More sharing options...
mrMarcus Posted November 21, 2012 Share Posted November 21, 2012 You also failed to explain the issue in greater detail. I can only read minds on the weekend. During the week, I require more details. Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394100 Share on other sites More sharing options...
Manhag Posted November 21, 2012 Author Share Posted November 21, 2012 i wana use select box to update database // I get id from url and get "city data stored in database <?php include('lib/db.php'); $id = htmlspecialchars($_GET["fid"]); $query1 = "SELECT `city` FROM `salaty_fb`.`users` WHERE `id`='$id'"; if ($result1 = mysqli_query($link, $query1)) { while($result = mysqli_fetch_array($result1)) { $city_db = $result['city']; } //$link->close(); ?> //intialize form <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <div style="<? if (isset($_POST['submit'])) {echo "display:none";} ?>;direction:rtl;" > <form method="post" action="updatesql.php?fid=<? echo $facebook_id; ?>" target="POPUPW" onsubmit="POPUPW = window.open('about:blank','POPUPW', 'width=200,height=100');"> create select box depending on array <?php echo "<select name=\"choose_city\">\n"; $choose_city = array('0' => 'Please choose user group', 'eg_cairo' => 'cairo', 'eg_alex' => 'alex', 'eg_giza' => 'giza'); foreach($choose_city as $city_key => $city_realname) { echo "<option value=\"$city_key\" name=\"$city_key\" id=\"$city_key\" "; if($city_key == $city_db) { echo " selected"; } echo ">$city_realname</option>\n"; } echo "</select><p />"; ?> //end form code <input type="submit" name="submit" value="Submit"> <br> <br> </form> </div> //check database connection and update selected value to database <?php } else { die(mysqli_error($link)); } if (isset($_POST['submit'])) { echo $_POST['city']; echo "||"; //get value of selected box $location = $_POST['city_key']; $query ="UPDATE `salaty_fb`.`users` SET `city`='$location' WHERE `id`='$id'"; //if data updated ...use java script pop up to inform user that update was done if ($result = mysqli_query($link, $query)) { printf("update done"); echo '<script language="javascript"> <!-- setTimeout("self.close();",1000) //--> </script> '; /* free result set */ // $result->close(); $link->close(); } else { die(mysqli_error($link)); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394103 Share on other sites More sharing options...
mrMarcus Posted November 21, 2012 Share Posted November 21, 2012 OK... when you click the submit button, what happens? Errors? Nothing? Something? Computer blows up? Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394108 Share on other sites More sharing options...
Manhag Posted November 21, 2012 Author Share Posted November 21, 2012 database doesnot updated with the selected city it always "0" Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394110 Share on other sites More sharing options...
mrMarcus Posted November 21, 2012 Share Posted November 21, 2012 (edited) $location = $_POST['city_key']; That's the problem. city_key does not exist in your form. Only <select> can hold have a name attribute. <option> does not. <option> contains value="" which is what is passed through the form submission. You need to grab the value of the selected option like so: $location = $_POST['choose_city']; Furthermore, you need to sanitize your form data before blindly tossing it into a query. See mysqli_real_escape_string Edited November 21, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394114 Share on other sites More sharing options...
Manhag Posted November 21, 2012 Author Share Posted November 21, 2012 (edited) now it stores $city_realname but i wana store $city_key $choose_city = array('0' => 'Please choose user group', 'eg_cairo' => 'cairo', 'eg_alex' => 'alex', 'eg_giza' => 'giza'); foreach($choose_city as $city_key=> $city_realname) Edited November 21, 2012 by Manhag Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394127 Share on other sites More sharing options...
mrMarcus Posted November 21, 2012 Share Posted November 21, 2012 That's not your issue. See my last post for your issue. You're not understanding how form submissions work. Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394129 Share on other sites More sharing options...
Manhag Posted November 21, 2012 Author Share Posted November 21, 2012 That's not your issue. See my last post for your issue. You're not understanding how form submissions work. okay i read it is there any way to get the value i need? Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394133 Share on other sites More sharing options...
mrMarcus Posted November 21, 2012 Share Posted November 21, 2012 okay i read it is there any way to get the value i need? If you read it, you'd know the answer to your question. Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394136 Share on other sites More sharing options...
Manhag Posted November 21, 2012 Author Share Posted November 21, 2012 okay i read it agian i edited my file to add this line $location = $_POST['choose_city']; the output is the selected value "cairo but i want get the option attribute of the selected value "eg_cairo" Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394143 Share on other sites More sharing options...
mrMarcus Posted November 21, 2012 Share Posted November 21, 2012 Then just manipulate your code to ensure the correct values are being placed in the option value: <option value=\"$city_key\">...</option> Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394150 Share on other sites More sharing options...
Manhag Posted November 21, 2012 Author Share Posted November 21, 2012 Then just manipulate your code to ensure the correct values are being placed in the option value: <option value=\"$city_key\">...</option> that was i did echo "<option value=\"$city_key\" name=\"$city_key\" id=\"$city_key\" "; so i wana to print $city_realname and store in database $city_key is it clear now ?? Quote Link to comment https://forums.phpfreaks.com/topic/270989-update-database-using-select-box/#findComment-1394160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.