Jump to content

Recommended Posts

Hi, I am trying to send a form to the database, which works fine, except for my single checkbox. The checkbox value of "1" is not going into the the correct user's row in the database table. It is a bit hit and miss where it goes, I need to somehow tie it to an id or something and I'm not sure how to do it.

 

This is from a page where I want to display a list of students from a database with a checkbox next to their name. This works fine. Selecting this checkbox is supposed to log that the student has had a lesson, leaving it blank means they were absent:

 



<!-- begin roll form --> 

<form id="roll" name="roll" method="post" action="roll_submit_process.php">
                  
    <table width="300" border="1" cellpadding="5" class="shop" align="center">
                    <tr>
                    <th><div align="center" class='ContentText'>Student Name</div></th><th width="40"><div align="center" class='ContentText'>Roll</div></th></tr><tr>
<?php


include('includes/connection.php');
$result = mysqli_query($connection,"SELECT * FROM students WHERE tutor_1='$user' ORDER BY last_name ASC")
or die(mysqli_error($connection));


while($row = mysqli_fetch_array($result)) {

$id = $row['id'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$week = "2";
$term = "1";
$year = "2010";


echo "<td align='left'><p><strong>".$last_name . "</strong> " . $first_name ."</p></td>";
echo "<td align='center' width='40'>";

echo "<input type='hidden' name='first_name[]' value='$first_name' />";
echo "<input type='hidden' name='last_name[]' value='$last_name' />";
echo "<input type='hidden' name='week[]' value='$week' />";
echo "<input type='hidden' name='term[]' value='$term' />";
echo "<input type='hidden' name='year[]' value='$year' />";
echo "<input type='hidden' name='id[]' value='$id' />";
echo "<input type='checkbox' name='attendance[]' value='1' />";
echo "</td>";
echo "</tr>";
      
}

?>
                      
</table>

<p> </p>
                  
<p align="center"><input name="Submit" type="submit" value="Submit Roll" /></p>
                  
    </form>

<!-- End roll form --> 

</html>

 

On the process page I have this:

 

<?php require_once("includes/tutor_session.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php

for($i=0;$i<=count($_POST['id']);$i++) {

//I know I am escaping data that came only from the database - I didn't bother editing my copy and pasted snippet!

$first_name = mysqli_real_escape_string($connection,$_POST['first_name'][$i]);
$last_name = mysqli_real_escape_string($connection,$_POST['last_name'][$i]);
$week = mysqli_real_escape_string($connection,$_POST['week'][$i]);
$term = mysqli_real_escape_string($connection,$_POST['term'][$i]);
$year = mysqli_real_escape_string($connection,$_POST['year'][$i]);
$attendance = $_POST['attendance'][$i];


mysqli_query($connection,"INSERT INTO roll (first_name, last_name, week, term, year, attendance)
VALUES ('$first_name', '$last_name', '$week', '$term', '$year','$attendance')")or trigger_error("SQL", E_USER_ERROR);
}
header("Location: tutorcp.php");
exit;

?>

 

All of the fields in the table are filled out correctly, but the attendance value of "1" could end up against any student name, and not always the name I had checked. I also get an extra blank field inserted. I don't know why, it hasn't happened before.

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/196950-problem-with-checkboxes-in-array/
Share on other sites

Ok, when sorting the database by id, it is not as random as I thought, but not correct.

 

I created 5 sample students.

 

If I select any number of checkboxes, it places "1" from the first student's id every time.

 

So If I select 2 students, the number "1" will go into the first two student's attendance fields, even though I may have selected students 3 and 4 on the list. It doesn't leave the fields for the unchecked students blank. It want's to fill the fields from the first student down.

I have now learned that if a checkbox is not checked, it won't be added to an array, so it is skipped. Therefore the number "1" will end up against the name of the wrong student as the number 1 is added down the list from student 1 downwards, and instead of skipping a field against a student where the check box was not checked.  It fills it in until the total amount of checked checkboxes was posted are added against the student's name in order, to the database.

 

If I check 3 check boxes out of 5 students, and they are students, 2, 3 and 5, it will enter "1" for students 1, 2 and 3,  the first 3 students in the list. It doesn't skip 1, and start entering "1" beside students 2 and 3, skip student 4, and put "1" against student 5.

 

Does anyone know a work around? I'm sure I'm not the only person to ever try to do this.  :confused:

change form to

<!-- begin roll form --> 
<form id="roll" name="roll" method="post" action="roll_submit_process.php">
   <table width="300" border="1" cellpadding="5" class="shop" align="center">
                    <tr>
                    <th><div align="center" class='ContentText'>Student Name</div></th><th width="40"><div align="center" class='ContentText'>Roll</div></th></tr><tr>
<?php
include('includes/connection.php');
$result = mysqli_query($connection,"SELECT * FROM students WHERE tutor_1='$user' ORDER BY last_name ASC")
or die(mysqli_error($connection));
$i=0;
while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$i++;
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$week = "2";
$term = "1";
$year = "2010";
echo "<td align='left'><p><strong>".$last_name . "</strong> " . $first_name ."</p></td>";
echo "<td align='center' width='40'>";
echo "<input type='hidden' name='first_name[$i]' value='$first_name' />";
echo "<input type='hidden' name='last_name[$i]' value='$last_name' />";
echo "<input type='hidden' name='week[$i]' value='$week' />";
echo "<input type='hidden' name='term[$id]' value='$term' />";
echo "<input type='hidden' name='year[]' value='$year' />";
echo "<input type='hidden' name='id[]' value='$id' />";
echo "<input type='checkbox' name='attendance[]' value='1' />";
echo "</td>";
echo "</tr>";     
}
?>
</table>
<p> </p    
<p align="center"><input name="Submit" type="submit" value="Submit Roll" /></p>
</form>
<!-- End roll form --> 
</html>

and process page to

<?php require_once("includes/tutor_session.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php

foreach ($_POST['id'] as $i => $id) {

//I know I am escaping data that came only from the database - I didn't bother editing my copy and pasted snippet!

$first_name = mysqli_real_escape_string($connection,$_POST['first_name'][$i]);
$last_name = mysqli_real_escape_string($connection,$_POST['last_name'][$i]);
$week = mysqli_real_escape_string($connection,$_POST['week'][$i]);
$term = mysqli_real_escape_string($connection,$_POST['term'][$i]);
$year = mysqli_real_escape_string($connection,$_POST['year'][$i]);
$attendance = $_POST['attendance'][$i];
mysqli_query($connection,"INSERT INTO roll (first_name, last_name, week, term, year, attendance)
VALUES ('$first_name', '$last_name', '$week', '$term', '$year','$attendance')")or trigger_error("SQL", E_USER_ERROR);
}
header("Location: tutorcp.php");
exit;

?>

not tested

Still not working.

 

I am uploading an image of my unstyled example form.

 

I chose Rolf Harris and Homer simpson from the list (2 & 4 on the list)

 

See the database image. One of the fields is now blank, and the "1" is next to the wrong person.

 

 

 

[attachment deleted by admin]

i didnt scroll to the end of form and don't made all changes

<!-- begin roll form --> 
<form id="roll" name="roll" method="post" action="roll_submit_process.php">
   <table width="300" border="1" cellpadding="5" class="shop" align="center">
                    <tr>
                    <th><div align="center" class='ContentText'>Student Name</div></th><th width="40"><div align="center" class='ContentText'>Roll</div></th></tr><tr>
<?php
include('includes/connection.php');
$result = mysqli_query($connection,"SELECT * FROM students WHERE tutor_1='$user' ORDER BY last_name ASC")
or die(mysqli_error($connection));
$i=0;
while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$i++;
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$week = "2";
$term = "1";
$year = "2010";
echo "<td align='left'><p><strong>".$last_name . "</strong> " . $first_name ."</p></td>";
echo "<td align='center' width='40'>";
echo "<input type='hidden' name='first_name[$i]' value='$first_name' />";
echo "<input type='hidden' name='last_name[$i]' value='$last_name' />";
echo "<input type='hidden' name='week[$i]' value='$week' />";
echo "<input type='hidden' name='term[$i]' value='$term' />";
echo "<input type='hidden' name='year[$i]' value='$year' />";
echo "<input type='hidden' name='id[$i]' value='$id' />";
echo "<input type='checkbox' name='attendance[$i]' value='1' />";
echo "</td>";
echo "</tr>";     
}
?>
</table>
<p> </p    
<p align="center"><input name="Submit" type="submit" value="Submit Roll" /></p>
</form>
<!-- End roll form --> 
</html> 

add array index to the reest of array elements

Excellent! It works, and I am happy and extremely grateful to you Sasa! I wish I could buy you a beer!  :D

 

Your code works perfectly, I get the "1" next to the correct student, and no longer get an extra blank entry in the database! Thanks again, I am very happy!  :D

 

 

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.