Jump to content

Insert Using Case


SalientAnimal
Go to solution Solved by Barand,

Recommended Posts

Hi All,

 

I have a form that has an attendance status field. This field needs to populate two different rows in my table. But I am not able to get my case statement to work.

 

Here is my current code:

        // INSERT THE NEW FOR INFORMATION INTO THE DATABASE TABLE
        if ($insert_stmt = $mysqli->prepare(	"INSERT INTO 
															shift_agent_report 
												(			
															shift
												,			username
												, 			agent
												, 			attendance_indicator
												, 			attendance_status
												, 			agent_comments
												) 
												VALUES 
												(
															?
												, 			?
												, 			?
												, 			(CASE 
																	WHEN (attendance_status = 'Present') THEN '0'
																	ELSE '1'
															END) AS attendance_indicator
												, 			?														
												, 			?
												)"))
			

		{
	
			$insert_stmt->bind_param('ssssss' ,$shift, $username, ucfirst($agent), $attendance_status, $attendance_status, $agent_comments);
            // EXECUTE THE PREPARED QUERY
            if (! $insert_stmt->execute())
			//PRINT THE NUMBERS OF ROWS THAT HAVE BEEN AFFECTED 
 			{
				//var_dump($_POST);exit;
				header('Location: ../error.php?err=Registration failure: INSERT');
				exit;

            }
			include "../success/shift_agent_success.php";
			exit;
        }

		

		/* CLOSE THE CONNECTION */
		$mysqli->close();
    }

}

?>
Link to comment
Share on other sites

  • Solution

Set the value of $attendance_indicator in the code prior to the insert and use "?" instead of the CASE statement

$attendance_indicator = ($attendance_status=='Present') ? "1": "0";
$insert_stmt->bind_param('ssssss' ,$shift, $username, ucfirst($agent), $attendance_indicator, $attendance_status, $agent_comments);

Alternatively, specify the status before the indicator in the insert column list so it then has a value before you do the CASE for the indicator and you only need 5 parameters as the indicator is a case statement

INSERT INTO 
     shift_agent_report 
     (            
          shift
     ,    username
     ,    agent
     ,    attendance_status
     ,    attendance_indicator
     ,    agent_comments
     ) 
     VALUES 
     (
          ?
     ,    ?
     ,    ?
     ,    ?                    
     ,    (CASE 
          WHEN (attendance_status = 'Present') THEN '0'
          ELSE '1'
          END)
     ,    ?
     )

then

$insert_stmt->bind_param('sssss' ,$shift, $username, ucfirst($agent), $attendance_status, $agent_comments);

Edited by Barand
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.