xox Posted January 18, 2011 Share Posted January 18, 2011 I have dropdown populated from mysql and now I want to add ID of selected value into other table, it doesn't get the ID. here's the code I have if (isset($_REQUEST['subcat'])) { $id_main = $_GET['categoriesID']; $DB-> Query('INSERT INTO subcat(id_main_cat,name_subcat) VALUES ("'.$id_main.'","'.$newSub.'")'); } ?> <br /> <?php $result = mysql_query("SELECT id,name_cat FROM category") or die(mysql_error()); echo "Pick main:"; echo "<select name='categoriesID'>"; //reads from table 'categories' while($row = mysql_fetch_array( $result )) { // display them in dropdown echo '<option value="'.$row['id'].'">'; echo $row['id'],$row['name_cat'] . '</option>'."\n"; } echo "</select><br />"; $_POST['categoriesID']; ?> <form method="post" action=""> <b>Add new:</b> <input type="text" name="newSubCat"> <input type="submit" name="addSubCat" value="Add"> </form> Table is populated only with $newSub but not with $id_main... Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/ Share on other sites More sharing options...
Maq Posted January 18, 2011 Share Posted January 18, 2011 Your submit button is named 'addSubCat' NOT 'subcat'. And if you're using POST, then use POST, not REQUEST. Change this line to: if (isset($_POST['addSubCat'])) Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161355 Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 You're using the POST method in your form, but attempting to use the value of a $_GET variable. Change $_GET['categoriesID'] to $_POST['categoriesID'] and the value should be there. As an aside, it's always a good idea to validate and sanitize form data. First to make sure all the data you need is actually there, and second to prevent malicious 'users' from running SQL injection and other attacks. Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161357 Share on other sites More sharing options...
AbraCadaver Posted January 18, 2011 Share Posted January 18, 2011 Also, you need to echo the select options inside the form. Right now you are echoing the select and then the form. Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161387 Share on other sites More sharing options...
xox Posted January 18, 2011 Author Share Posted January 18, 2011 Ok, I've managed all the issues you posted, thank you. But now form wont reload and values aren't stored. New code: if (isset($_POST['DodajPodkategorijo'])) { $id_glavne = $_GET['kategorije']; $DB-> Query('INSERT INTO podkategorije(id_glavne_kategorije,ime_podkategorije) VALUES ("'.$id_glavne.'","'.$novaPodkategorija.'")'); } ?> <br /> <?php $result = mysql_query("SELECT id,ime_kategorije FROM kategorije") or die(mysql_error()); echo "izberi glavno kategorijo:"; echo "<select name='kategorije'>"; //prebere vse kategorije iz tabele 'kategorije' while($row = mysql_fetch_array( $result )) { // in jih izpiše v dropdownu echo '<form method="post" action="">'; echo '<option value="'.$row['id'].'">'; echo $row['id'],$row['ime_kategorije'] . '</option>'."\n"; } echo "</select><br />"; $_POST['kategorije']; ?> <b>Dodaj podkategorijo:</b> <input type="text" name="novaPodkategorija"> <input type="submit" name="DodajPodkategorijo" value="Dodaj podkategorijo"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161445 Share on other sites More sharing options...
Maq Posted January 18, 2011 Share Posted January 18, 2011 You're missing your starting 'form' element tag. Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161447 Share on other sites More sharing options...
xox Posted January 18, 2011 Author Share Posted January 18, 2011 It's there echo '<form method="post" action="">'; Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161454 Share on other sites More sharing options...
xox Posted January 18, 2011 Author Share Posted January 18, 2011 Or do you think anything else? Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161475 Share on other sites More sharing options...
Maq Posted January 18, 2011 Share Posted January 18, 2011 Or do you think anything else? Does anything happen or is it a blank page? Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161499 Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 $novaPodkategorija is undefined in the code, unless register_globals = On (which it definitely should not). You need to assign the value of $_POST['novaPodkategorija'] to it as below, assuming it's expected to be string type data. $novaPodkategorija = mysql_real_escape_string($_POST['novaPodkategorija']); Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161512 Share on other sites More sharing options...
xox Posted January 19, 2011 Author Share Posted January 19, 2011 Or do you think anything else? Does anything happen or is it a blank page? Nothing happens, not reloading or anything. Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161767 Share on other sites More sharing options...
xox Posted January 19, 2011 Author Share Posted January 19, 2011 $novaPodkategorija is undefined in the code, unless register_globals = On (which it definitely should not). You need to assign the value of $_POST['novaPodkategorija'] to it as below, assuming it's expected to be string type data. $novaPodkategorija = mysql_real_escape_string($_POST['novaPodkategorija']); $novaPodkategorija is saved into "ime_podkategorije" (name of sub category) so I think this isn't an issue, I can't get ID of selected item in dropdown... Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161770 Share on other sites More sharing options...
Maq Posted January 19, 2011 Share Posted January 19, 2011 $novaPodkategorija is undefined in the code, unless register_globals = On (which it definitely should not). You need to assign the value of $_POST['novaPodkategorija'] to it as below, assuming it's expected to be string type data. $novaPodkategorija = mysql_real_escape_string($_POST['novaPodkategorija']); $novaPodkategorija is saved into "ime_podkategorije" (name of sub category) so I think this isn't an issue, I can't get ID of selected item in dropdown... That's not in the code you provided. Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161936 Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 Well, your <select tag is before your <form tag, which is inside your while() loop. That certainly won't work . . . Quote Link to comment https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161939 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.