I am new to PHP and I have an application that is suppose to take 6 pieces of information and insert it into the table. I essentially have this application working but the issue that I am running into is that when I select multiple checkboxes it will only insert the last one checked into the table. I do not know why. Can someone help?

Below is my code:

//Database Connection
mysql_connect($_MISSING['db_host'], $_MISSING['db_username'], $_MISSING['db_password']) or die(mysql_error());
@mysql_select_db($_MISSING['db_database']) or die(mysql_error());

if (!$user_id) include("login.php");


print("<title>" . $_MISSING['organization'] . " - " . $_MISSING['html_title'] . " / Add Assignment");
print("<link rel=\"stylesheet\" href=\"../css/style.css\" title=\"style.css\" type=\"text/css\">");


$sql="SELECT * FROM student ORDER BY lastName";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

<p>Enter New Assignment</p>
<p><b>Subject:</b> <input name="subject" type="text" id="subject"value="" size="50"></p>
<p><b>Description:</b> <input name="description" type="text" id="description" value="" size="100"></p>


<input type="submit" name="Submit" value="Submit">

<?PHP

//Prints Student in a Table
print("<div class=\"student_wrapper\">");
print("<div class=\"student_checkbox\">");
print("<b>ID</b>");
print("</div>"); //Closes DIV student_checkbox
print("<div class=\"student_lastName\">");
print("<b>Last Name</b>");
print("</div>"); //Closes DIV student_lastname
print("<div class=\"student_firstName\">");
print("<b>First Name</b>");
print("</div>");//Closes DIV student_firstname
print("<div class=\"student_AP\">");
print("<b>AP</b>");
print("</div>");//Closes DIV student_AP
print("<div class=\"student_8th\">");
print("<b>8th</b>");
print("</div>");//Closes DIV student_8th
print("<br /><hr />");


$i = 0;

while($students=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $i++; ?>" /></td>
<td align="center"><input name="lastName[]" type="text" id="lastName[]" value="<? echo $students['lastName']; ?>"></td>
<td align="center"><input name="firstName[]" type="text" id="firstName[]" value="<? echo $students['firstName']; ?>"></td>
<td align="center"><input name="AP[]" type="text" id="AP[]" value="<? echo $students['AP']; ?>"></td>
<td align="center"><input name="last_teacher[]" type="text" id="last_teacher[]" value="<? echo $students['last_teacher']; ?>"></td>
<br />

</tr>

<tr>

<? 
}
?>


<?php

// Get values from form 
$lastName=$_POST['lastName'];
$firstName=$_POST['firstName'];
$AP = $_POST['AP'];
$last_teacher = $_POST['last_teacher'];
$description = $_POST['description'];
$subject = $_POST['subject'];
$ID = $checkbox[$i];

$checkbox=$_POST['checkbox'];


// Check if button name "Submit" is active, do this 
if($_POST['Submit']){
foreach($checkbox as $i){

	$sql2 = "INSERT INTO assignments SET firstName = '$firstName[$i]', lastName = '$lastName[$i]', AP = '$AP[$i]', last_teacher = '$last_teacher[$i]', description = '$description', subject = '$subject'";
	
}


$result2=mysql_query($sql2) or die(mysql_error());


if($result2){
header("location:../index.php");
}
mysql_close();
}

?>

mysql_query function must be inside foreach loop.

// Check if button name "Submit" is active, do this 
if($_POST['Submit']){
foreach($checkbox as $i){

	$sql2 = "INSERT INTO assignments SET firstName = '$firstName[$i]', lastName = '$lastName[$i]', AP = '$AP[$i]', last_teacher = '$last_teacher[$i]', description = '$description', subject = '$subject'";
	$result2=mysql_query($sql2) or die(mysql_error());
	
}

I cannot believe it that it was something that simple that caused the issue, but now it works. Thanks for the help.

make thread solved. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.