kokotas Posted January 8, 2011 Share Posted January 8, 2011 Hi, I have this form which will create a checkbox list using data from my database and also determin if a checkbox had been checked before and check if it had. <form style="text-align:center" name="PrefRestaurant" id="PrefRestaurant" action="preferances_check.php" method="post"><table align="center"> <?php checkbox(id, name, restaurants, id); ?></table> <input type="submit" name="Prefer" id="Prefer" value="Επιλογή"/></form> function checkbox($intIdField, $strNameField, $strTableName, $strOrderField, $strMethod="asc") { $strQuery = "select $intIdField, $strNameField from $strTableName order by $strOrderField $strMethod"; $rsrcResult = mysql_query($strQuery); while ($arrayRow = mysql_fetch_assoc($rsrcResult)) { $testqry = "SELECT * FROM user_restaurant WHERE user_id = $_SESSION[userId] AND restaurant_id = $arrayRow[id]"; $rsltestqry = mysql_query($testqry); $numrows = mysql_num_rows($rsltestqry); if ($numrows == 1) { echo "<tr align=\"left\"><td><input type=\"checkbox\" name=\"restaurant[]\" value=\"$arrayRow[id]\" checked/>$arrayRow[name]</td></tr>"; } else{ echo "<tr align=\"left\"><td><input type=\"checkbox\" name=\"restaurant[]\" value=\"$arrayRow[id]\" />$arrayRow[name]</td></tr>"; } } } Now the part which I can't get to work is when I'm trying to store the new values in my database. When I click the submit button I clear my database of any row that is related to the currently loggedin user and I want to store his new preferences (checked cheboxes). I've read that only the cheked checkboxes' values are POSTed so I did this (preferances_check.php) foreach($_POST['restaurant'] as $value) { $query="INSERT INTO user_restaurant VALUES ('$_SESSION[userId]','$value')"; } But it is not working, nothing gets written in my table Could someone please enlighten me on this? Thnks! Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 8, 2011 Share Posted January 8, 2011 Hi try this $restaurant=$_POST['restaurant']; foreach ($restaurant as $value) { $query="INSERT INTO user_restaurant (user_id, name) VALUES ('$_SESSION[userId]','$value')"; $res=mysql_query($query); } assuming user_id and name are the column names in your database Quote Link to comment Share on other sites More sharing options...
kokotas Posted January 8, 2011 Author Share Posted January 8, 2011 Thanks so much!!!! It finally worked! I only had to change (user_id, name) to (user_id, restaurant_id). :D:D Again thank you, I was trying to figure out all day yesterday 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.