i trying create form dropdown list populates database using while loop.the form submit user's input table in database.to end, using while loop hidden input mark options populated in drop down list, use insert database.
problem: while loop not loop if write code hidden input, after comment out code input.
excerpt of code:
<html> <head><link rel="stylesheet" type="text/css" href="style.css" /></head> <body> <?php //include("adminnav.php");?> <form action="<?php echo $_server['php_self']?>" name="addsub" method="post"> <table border="1"> <tr> <td>category: </td> <td> <select name="category"> <?php include("cxn.inc"); if($_session['auth']=="yes" && $_session['type']=="admin") { $userid=$_session['userid']; $branch="0"; $getcat="select id,category categories business=$userid && branch=$branch"; $cat=mysqli_query($cxn,$getcat) or die (mysqli_error($cxn)); while($row=mysqli_fetch_assoc($cat)) { $catid=$row['id']; echo"<option value=$row[category]>$row[category]</option>"; //echo"<input type='hidden' name='catid' value='$catid' />";<--this line } } else if ($_session['auth']=="yes" && $_session['type']=="sub") { $userid=$_session['userid']; $branchquery="select id branch bizid=$userid "; $getbranch=mysqli_query($cxn,$branchquery) or die(mysqli_error($cxn)); $num=mysqli_num_rows($getbranch); if($num>0)//get branch details if branches exist { $resultbranch = mysqli_fetch_assoc($getbranch); $branch=$resultbranch['id']; $getcat="select id,category categories business=$userid && branch=$branch"; $cat=mysqli_query($cxn,$getcat) or die (mysqli_error($cxn)); while($row=mysqli_fetch_assoc($cat)) { $catid=$row['id']; echo"<option value=$row[category]>$row[category]</option>"; //echo"<input type='hidden' name='catid' value='$catid' />"; } } else if($num==0)//get user(admin) details if no branches exist { $resultbranch = mysqli_fetch_assoc($getbranch); $branch=$resultbranch['id']; $getcat="select id,category categories business=$userid"; $cat=mysqli_query($cxn,$getcat) or die (mysqli_error($cxn)); while($row=mysqli_fetch_assoc($cat)) { $catid=$row['id']; echo"<option value=$row[category]>$row[category]</option>"; //echo"<input type='hidden' name='catid' value='$catid' />"; } } } ?> </select> </td> </tr> <tr> <td>subcategory:</td> <td><input type="text" name="subcategory" maxlength="50" size="30" required="required" /></td> </tr> <tr> <td> <input type="submit" name="addsub" value="submit" /> </td> <td> <?php if(isset($success)) { echo"$success"; } else if(isset($error)) { echo"$error"; } ?> </td> </tr> </table> </form> </body> </html>
extract of table:
--------------------------------------------- | id | business | branch | category | --------------------------------------------- | 155 | 5 | 0 | test1 | --------------------------------------------- | 156 | 5 | 0 | test2 | --------------------------------------------- | 157 | 5 | 0 | test3 | --------------------------------------------- | 158 | 5 | 0 | test4 | ---------------------------------------------
details of problem
when line commented out, dropdown list populates test,1,2,3,4 when add in, shows test1.furthermore, when test form(by submitting it) inserts table branch category "test1" inserts id 158(which belongs category 'test4'.
could kind soul kindly point out mistake , can/should correct it? suggestions improve coding welcome.
thanks!
edit(solution) else encounters same issue, did edit line of code in processing code page from
$catid=$row['id'];
to
$catid=$_post['category'];
credits solution goes corbin
you can't embed <input>
inside of <select>
.
just make $row['id']
value of select. should working ids in code handles form anyway.
Comments
Post a Comment