Jump to content

PDO Need help passing checkboxes


spertuit
Go to solution Solved by mac_gyver,

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
		}
	}
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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].
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.