stirrah Posted October 26, 2013 Share Posted October 26, 2013 Hi, I got a dropdown and a textbox. The dropdown is getting data from the database and showing the names. Now i want to save that names ID and send it to the database. How can i do this? Do i have to make another query something like this? Or can i make it some other (better) way, Maybe this is a question for the SQL-forum? $categoryId = mysql_query("SELECT categoryId FROM `projectCategory` WHERE categoryName = '" . mysql_real_escape_string($_POST['categoryName'])); Here is my code: <?php if (isset($_GET['add_h_success']) && empty($_GET['add_h_success'])) { echo 'Projekt tillagt.'; } else { if (empty($_POST) === false && empty($errors) === true) { add_hproject($_POST['huvudProjectName'], $categoryId); header('Location: add_hproject.php?add_h_success'); } else if (empty($errors) === false) { echo output_errors($errors); } ?> <?php $options = ''; $category=mysql_query("SELECT categoryName, categoryId FROM `projectCategory`"); while($row = mysql_fetch_array($category)) { $options .="<option>" . $row['categoryName'] . "</option>"; } ?> <form action="" method="POST"> <ul> <li> <?php $menu=" <select name='categoryId'> " . $options . " </select> </form>"; echo $menu; ?> </li> <li>Namn: <br> <input type="text" name="huvudProjectName"> </li> <li> <input type="submit" value="Lägg till"></li> </ul> </form> Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted October 26, 2013 Solution Share Posted October 26, 2013 (edited) Get the category id and name from the database when you make the options list and set the id as the value for the options $category=mysql_query("SELECT categoryName, categoryId FROM `projectCategory`"); while($row = mysql_fetch_array($category)) { $options .="<option value=\"".$row['categoryId']."\">" . $row['categoryName'] . "</option>"; } When the form is submitted $_POST['categoryId''] will contain the category id for the selected category. Edited October 26, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
stirrah Posted October 26, 2013 Author Share Posted October 26, 2013 Ah! Smart! My problem now is that i cant submit the value in the dropdown...why? my code looks like this right now: <?php if (isset($_GET['add_h_success']) && empty($_GET['add_h_success'])) { echo 'Projekt tillagt.'; echo $cat; } else { if (empty($_POST) === false && empty($errors) === true) { $cat = $_POST['categoryName']; $categoryId = mysql_query("SELECT categoryId FROM `projectCategory` WHERE categoryName = $cat"); add_hproject($_POST['huvudProjectName'], $categoryId); header('Location: add_hproject.php?add_h_success'); } else if (empty($errors) === false) { echo output_errors($errors); } ?> <?php $options = ''; $category=mysql_query("SELECT categoryName, categoryId FROM `projectCategory`"); while($row = mysql_fetch_array($category)) { $options .="<option>" . $row['categoryName'] . "</option>"; } ?> <form action="" method="POST"> <ul> <li> <?php $menu=" <select name='categoryName'> " . $options . " </select> "; echo $menu; ?> </li> <li>Namn: <br> <input type="text" name="huvudProjectName"> </li> <li> <input type="submit" value="Lägg till"></li> </ul> </form> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 26, 2013 Share Posted October 26, 2013 And, what do you mean - exactly - that you cannot submit. Are you saying you are clicking the submit button and nothing happens. Or that you click it and it submits - but you don't see the results you expect. We are working in the blind and depend upon you to provide all the information necessary to help you. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 26, 2013 Share Posted October 26, 2013 (edited) Don't know why you have change your code from what you had. All you needed to do is replace the while loop with my code and then pass $_POST['categoryId'] variable to the add_hproject() function. The problem with your code above is with the query. Values used in queries need to be wrapped in quotes $categoryId = mysql_query("SELECT categoryId FROM `projectCategory` WHERE categoryName = '$cat'"); And mysql_query does not return the categoryId only the result resource. You need to use one of the mysql_fetch_* functions or mysql_result to get the categoryId value from the query. Edited October 26, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
stirrah Posted October 27, 2013 Author Share Posted October 27, 2013 And, what do you mean - exactly - that you cannot submit. Are you saying you are clicking the submit button and nothing happens. Or that you click it and it submits - but you don't see the results you expect. We are working in the blind and depend upon you to provide all the information necessary to help you. Sorry! I mean that the $_POST never gets the value. Quote Link to comment Share on other sites More sharing options...
stirrah Posted October 27, 2013 Author Share Posted October 27, 2013 When I press the submit button, the variable $cat never gets the value. $cat = $_POST['categoryName']; I have tried to echo $cat but it's empty Quote Link to comment Share on other sites More sharing options...
stirrah Posted October 27, 2013 Author Share Posted October 27, 2013 guys! it works now. Thanks SO much for all your help! Quote Link to comment 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.