Jump to content

Insert based on post array values


Adamhumbug
 Share

Recommended Posts

When i click my submit button i am posting the following.

		Array
(
    [submitStaffOrder] => 
    [staffMember] => Array
        (
            [0] => 2
            [1] => ANYCHEF1
        )

    [date] => Array
        (
            [0] => 2020-02-18
            [1] => 2020-02-18
        )

    [startTime] => Array
        (
            [0] => 11:01
            [1] => 10:10
        )

    [finishTime] => Array
        (
            [0] => 11:01
            [1] => 10:01
        )

    [delExisting] => Array
        (
            [0] => 
            [1] => toDel
        )

)

I would like to update/insert if delExisting is blank and delete if it has toDel in there.

I have the following but i am pretty sure the if is not working.

$date = $_POST['date'];
	$stime = $_POST['startTime'];
	$ftime = $_POST['finishTime'];
	$del = $_POST['delExisting'];
	$staff = $_POST['staffMember'];

$stmt = $conn -> prepare("
	                         INSERT IGNORE INTO ssm_staff_order (job_id, staff_id, staff_start_time, staff_finish_time, staff_start_date, staff_finish_date) 
	                         VALUES (?,?,?,?,?,?)
	                         ON DUPLICATE KEY 
	                         UPDATE 
	                         staff_start_time = VALUES(staff_start_time),
	                         staff_finish_time = VALUES(staff_finish_time);
	                         ");

	$delstmt = $conn -> prepare("
		                         DELETE FROM ssm_staff_order
		                         WHERE staff_id = ?
		                         AND job_id = ?
		                         AND staff_start_date = ?
		                         ");

	foreach ($staff as $key => $staffId) {
		if ($del == '') {
			$start = $date[$key].' '.$stime[$key];
			$finish = $date[$key].' '.$ftime[$key];
			$stmt -> bind_param('isssss', $jid, $staffId, $start, $finish, $date[$key], $date[$key]);
			$stmt -> execute();
		}
		

		
		if ($del == 'toDel') {
			$delstmt -> bind_param('sis', $staffId, $jid, $date[$key]);
			$delstmt -> execute();
		}
	}
	
}

Can i do what i am trying to do or am i way off here?

 

I struggle with arrays :(

Edited by Adamhumbug
Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.