spertuit Posted June 30, 2013 Share Posted June 30, 2013 Well I am having the worst weekend ever. I have been working on this code all day and google is not my friend today. So here is the problem, I am trying to write a form that is basically keeping records of people who have stayed as guest and what meals they ate (breakfast, lunch, dinner, midnight snack). So the html form is pretty simple, and I have a few checkboxes and a little javascript to add more rows if there are more guest. Everything is working fine, but the checkboxes values are killing me. Basically if the box isnt checked than do what? Anyway my insert isnt working at all Anyway here is some basic coding Html form for checkboxes: <td><input type="checkbox" name="breakfast[]" /></td> <td><input type="checkbox" name="lunch[]" /></td> <td><input type="checkbox" name="dinner[]" /></td> <td><input type="checkbox" name="midnight[]" /></td> <td><input type="checkbox" name="lodging[]"/></td> <td><input type="text" name="guestName[]"/></td> So as you can see checkboxes for the meals and a text input for the guest name. Now here is my function (please go easy I am new to pdo) public function createMeal($id,$table){ try{ //write query if(isset($_POST['guestName'])){ $guestName = $_POST['guestName']; $repName = $_POST['repName']; $repTitle = $_POST['repTitle']; if(isset($_POST['breakfast'])){ $breakfast = $_POST['breakfast']; } if(isset($_POST['lunch'])){ $lunch = $_POST['lunch']; } if(isset($_POST['dinner'])){ $dinner = $_POST['dinner']; } if(isset($_POST['midnight'])){ $midnight = $_POST['midnight']; } if(isset($_POST['lodging'])){ $lodging = $_POST['lodging']; } } foreach($guestName as $a => $b) { $query = "INSERT INTO meals(repName, repTitle, breakfast, lunch, dinner, midnight, lodging, guestName, masterLogID) VALUES (:repName, :repTitle, :breakfast, :lunch, :dinner, :midnight, :lodging, :guestName, :masterLogID)"; $stmt = $this->conn->prepare($query); $stmt->bindParam(':repName',$repName[$a]); $stmt->bindParam(':repTitle',$repTitle[$a]); $stmt->bindParam(':breakfast',$breakfast[$a]); $stmt->bindParam(':lunch',$lunch[$a]); $stmt->bindParam(':dinner',$dinner[$a]); $stmt->bindParam(':midnight',$midnight[$a]); $stmt->bindParam(':lodging',$lodging[$a]); $stmt->bindParam(':guestName',$guestName[$a]); $stmt->bindParam(':masterLogID',$masterLogID); // Execute the query $stmt->execute(); } echo "Record was saved."; }catch(PDOException $exception){ //to handle error echo "Error: " . $exception->getMessage(); } } I am getting the message "Record was saved", but nothing is in the table. Any help is greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/ Share on other sites More sharing options...
mac_gyver Posted July 1, 2013 Share Posted July 1, 2013 Basically if the box isnt checked than do what? you would insert one row for each data item and there would only be rows for actual data. your_table should be laid out like this - guest_id, meal (breakfast, lunch, ...), date 123 breakfast 2013-07-01 123 midnight 2013-07-01 123 breakfast 2013-07-02 123 lunch 2013-07-02 as to your code, did you set the error mode to PDO::ERRMODE_EXCEPTION so that the PDO statements would throw an exception if they fail? also, you need to prepare the query and bind the variables ONCE (before the start of the loop.) the only thing inside the loop would be code to populate the bound variables with each successive set of values and to execute the query. Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/#findComment-1438756 Share on other sites More sharing options...
spertuit Posted July 1, 2013 Author Share Posted July 1, 2013 I've been trying all kinds of things, this is killing me. I have it working, but the value on the checkboxes is not coming out correctly. public function createMeal($id,$table){ $breakfast = array(); $lunch = array(); $dinner = array(); $midnight = array(); $lodging = array(); $guestName = $_POST['guestName']; if(isset($_POST['breakfast'])){ $breakfast = $_POST['breakfast']; } if(isset($_POST['lunch'])){ $lunch = $_POST['lunch']; } if(isset($_POST['dinner'])){ $dinner = $_POST['dinner']; } if(isset($_POST['midnight'])){ $midnight = $_POST['midnight']; } if(isset($_POST['lodging'])){ $lodging = $_POST['lodging']; } $repName = $_POST['repName']; $repTitle = $_POST['repTitle']; $masterLogID = $id; foreach($guestName as $a => $b) { if(isset($breakfast[$a])){ $breakfastLog = $breakfast[$a]; }else{ $breakfastLog = "off"; } if(isset($lunch[$a])){ $lunchLog = $lunch[$a]; }else{ $lunchLog = "off"; } if(isset($dinner[$a])){ $dinnerLog = $dinner[$a]; }else{ $dinnerLog = "off"; } if(isset($midnight[$a])){ $midnightLog = $midnight[$a]; }else{ $midnightLog = "off"; } if(isset($lodging[$a])){ $lodgingLog = $lodging[$a]; }else{ $lodgingLog = "off"; } $sql="INSERT INTO meals SET repName=:repName,repTitle=:repTitle, guestName=:guestName, breakfast=:breakfast, lunch=:lunch, dinner=:dinner, midnight=:midnight, lodging=:lodging, masterLogID=:masterLogID"; if(!$stmt = $this->conn->prepare($sql)){ // prepare failed echo "<pre>Prepare failed:\n"; print_r($pdo->errorInfo()); echo "</pre>"; } else { if(!$stmt->execute(array(':repName'=>$repName,':repTitle'=>$repTitle,':guestName'=>$guestName[$a],':breakfast'=>$breakfastLog,':lunch'=>$lunchLog,':dinner'=>$dinnerLog,':midnight'=>$midnightLog,':lodging'=>$lodgingLog,':masterLogID'=>$masterLogID))){ // execute failed echo "<pre>Execute failed:\n"; print_r($stmt->errorInfo()); echo "</pre>"; } } echo "<pre>Meal Log Updated:\n"; echo "</pre>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/#findComment-1438794 Share on other sites More sharing options...
Solution mac_gyver Posted July 1, 2013 Solution Share Posted July 1, 2013 you should start by checking if your form is submitting the data you think it is. for any checkboxes that you have checked for each guest, do the check boxes have an array index that associates them with the guest name they correspond to? Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/#findComment-1438799 Share on other sites More sharing options...
kicken Posted July 1, 2013 Share Posted July 1, 2013 Rather than a different name for each box, use the same name, and different values: <td><input type="checkbox" name="meal[]" value="breakfast" /></td> <td><input type="checkbox" name="meal[]" value="lunch" /></td> <td><input type="checkbox" name="meal[]" value="dinner" /></td> <td><input type="checkbox" name="meal[]" value="midnight" /></td> <td><input type="checkbox" name="meal[]" value="lodging" /></td> <td><input type="text" name="guestName[]"/></td> If your going to be having several rows, one per guest, you'll need to set your own index in the name rather than using [] in order to keep the meals and guest names groupped properly: <td><input type="checkbox" name="meal[0]" value="breakfast" /></td> <td><input type="checkbox" name="meal[0]" value="lunch" /></td> <td><input type="checkbox" name="meal[0]" value="dinner" /></td> <td><input type="checkbox" name="meal[0]" value="midnight" /></td> <td><input type="checkbox" name="meal[0]" value="lodging" /></td> <td><input type="text" name="guestName[0]"/></td> Next row would be [1], then [2], etc. Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/#findComment-1438801 Share on other sites More sharing options...
mac_gyver Posted July 1, 2013 Share Posted July 1, 2013 Rather than a different name for each box, use the same name, and different values: except that more than one checkbox can be checked for any guest. Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/#findComment-1438803 Share on other sites More sharing options...
spertuit Posted July 1, 2013 Author Share Posted July 1, 2013 You got it! In my javascript I had one of my cell id's off by one number and it was messing up the values being passed into my arrays. Now I have to go back and make my function look decent again. Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/#findComment-1438804 Share on other sites More sharing options...
spertuit Posted July 1, 2013 Author Share Posted July 1, 2013 My function is still messed up Geeze im try to pass in on or off from my checkboxes and my values are not being assigned correctly in my function, but at least I'm closer now. Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/#findComment-1438805 Share on other sites More sharing options...
kicken Posted July 1, 2013 Share Posted July 1, 2013 except that more than one checkbox can be checked for any guest. That is what the [] is for. It will create an array of all the selected values. I did however, leave that bit out by accident in my second code block: <td><input type="checkbox" name="meal[0][]" value="breakfast" /></td> <td><input type="checkbox" name="meal[0][]" value="lunch" /></td> <td><input type="checkbox" name="meal[0][]" value="dinner" /></td> <td><input type="checkbox" name="meal[0][]" value="midnight" /></td> <td><input type="checkbox" name="meal[0][]" value="lodging" /></td> <td><input type="text" name="guestName[0]"/></td> $_POST['meal'][0] would contain an array with each of the values selected for guest [0]. Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/#findComment-1438807 Share on other sites More sharing options...
spertuit Posted July 1, 2013 Author Share Posted July 1, 2013 The values are being passed properly now from the html form to my function now its just handling the array in my loops and passing the values into my table The function is pretty ugly right now, but it looks like I have my conditional statements all wrongs and I am not handling the arrays properly public function createMeal($id,$table){ $breakfast = array(); $lunch = array(); $dinner = array(); $midnight = array(); $lodging = array(); $guestName = $_POST['guestName']; if(isset($_POST['breakfast'])){ $breakfast = $_POST['breakfast']; } if(isset($_POST['lunch'])){ $lunch = $_POST['lunch']; } if(isset($_POST['dinner'])){ $dinner = $_POST['dinner']; } if(isset($_POST['midnight'])){ $midnight = $_POST['midnight']; } if(isset($_POST['lodging'])){ $lodging = $_POST['lodging']; } $repName = $_POST['repName']; $repTitle = $_POST['repTitle']; $masterLogID = $id; foreach($guestName as $a => $b) { if(isset($breakfast[$a])){ $breakfastLog = $breakfast[$a]; }else{ $breakfastLog = "off"; } if(isset($lunch[$a])){ $lunchLog = $lunch[$a]; }else{ $lunchLog = "off"; } if(isset($dinner[$a])){ $dinnerLog = $dinner[$a]; }else{ $dinnerLog = "off"; } if(isset($midnight[$a])){ $midnightLog = $midnight[$a]; }else{ $midnightLog = "off"; } if(isset($lodging[$a])){ $lodgingLog = $lodging[$a]; }else{ $lodgingLog = "off"; } $sql="INSERT INTO meals SET repName=:repName,repTitle=:repTitle, guestName=:guestName, breakfast=:breakfast, lunch=:lunch, dinner=:dinner, midnight=:midnight, lodging=:lodging, masterLogID=:masterLogID"; if(!$stmt = $this->conn->prepare($sql)){ // prepare failed echo "<pre>Prepare failed:\n"; print_r($pdo->errorInfo()); echo "</pre>"; } else { if(!$stmt->execute(array(':repName'=>$repName,':repTitle'=>$repTitle,':guestName'=>$guestName[$a],':breakfast'=>$breakfastLog,':lunch'=>$lunchLog,':dinner'=>$dinnerLog,':midnight'=>$midnightLog,':lodging'=>$lodgingLog,':masterLogID'=>$masterLogID))){ // execute failed echo "<pre>Execute failed:\n"; print_r($stmt->errorInfo()); echo "</pre>"; } } echo "<pre>Meal Log Updated:\n"; echo "</pre>"; print_r ($breakfast); } } I have to try and clean it up, I have torn this function up trying to get it to work properly. Quote Link to comment https://forums.phpfreaks.com/topic/279740-pdo-need-help-passing-checkboxes/#findComment-1438810 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.