I want to send multiple ID's in database using PHP

I am working on a project of HRMS ‘Human Resource Management System’

And task management is one of the module that includes in my project but I am stuck when I am getting the multi-select form user input I didn’t get the employees id in $_POST['assign-to[]'] you can see I the assign-to select value is an array, so basically I want to ask that how do I loop this variable and send to database using PHP and the complete backend code of action script are given below kindly take a look my code and help me if any other thing you need ask me

<?php 	

require_once 'db.inc.php';
if (isset($_POST['task-submit'])) {

	$task_title = $_POST['task-title'];
	$assignTo = $_POST['assign-to[]'];
	$startDate =strtotime($_POST['start-date']);
	$dueDate = strtotime($_POST['due-date']);
	$descript = $_POST['descrp'];
	

	if (empty($task_title || $assign_to || $start_date || $due_date || $descript)){
		header("Location: ../admin/add-new-task.php?error=emptyfields&start_date".$startDate."&end_date=".$dueDate."&assignto=".$assignTo."&task_title".$task_title, true);
		exit();
	}
	else if (!preg_match("/^[a-zA-Z0-9]*$/", $task_title)) {
		header("Location: ../admin/add-new-task.php?error=invalid-Title", true);
		exit();
	}
	elseif (!empty($assignTo)) {
			foreach ($assignTo as $empId) {
				echo "</br>";
				echo "$empId";
				
			}
	}
	

	else {
		$sql = "INSERT INTO sys_task(task,start_date,due_date,description) VALUES (?, ?, ?, ?)";
		$stmt = mysqli_stmt_init($conn);
			if (!mysqli_stmt_prepare($stmt, $sql)) {
				header("Location: ../admin/add-new-task.php?error=sqlError", true);
				exit();
			}
			else {
			mysqli_stmt_bind_param($stmt, 'siis',$task_title,$startDate,$dueDate,$descript);
			mysqli_stmt_execute($stmt);
			mysqli_stmt_store_result($stmt);

			header("Location: ../admin/add-new-task.php?result=Task_is_successfully_added&start_date=".$startDate."&end_date=".$dueDate."&assignto=".$assignTo."&task_title".$task_title, true);
			exit();
			}



	}
	mysqli_stmt_close($stmt);
	mysqli_close($conn);






}

Thank you

if this is your form input name assign-to[], PHP will auto-gerneate an array from that $_POST['assign-to'] that you can loop over with foreach. But you will get a definitive view on the structure if you just do var_dump($_POST).

if (empty($task_title || $assign_to || $start_date || $due_date || $descript)){

i bet this line does not behave like expected…

1 Like

@chorn when try to var_dump it says Notice : Undefined index: assign-to in C:\xampp\htdocs\Hms\includes\addNewTask.inc.php on line 4

no It will work fine

It will - you should not have the square-brackets in your index here:

$assignTo = $_POST['assign-to[]'];

After this line (without the erroneous brackets), $assignTo will then contain an array of each of the elements. Once you have that array, you can loop through it and store each element in the appropriate place in your database table.

1 Like

done I am getting the value of $assignTo

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.