-1

I want insert employee attendance. when I checked more then one checkbox it take two checkbox value 1 and other checkbox value 0 .so how can i solve this please help me. this is my form code

<form action="coll.php" method="post" name="create_grading" id="create_grading">  
        <table width="30%" border="0" cellpadding="2" cellspacing="3" class="mainTable">
            <tr>
                <th><input type="checkbox" id="selectall" /></th>
                <th>name</th>
            </tr>
            <?php
            $sql = "select * from employee";
            $query = mysqli_query($con, $sql);
            while ($row = mysqli_fetch_array($query)) {
                ?>   
                <tr>
                    <td><input type="hidden" name="eid[]" value="<?php echo $row['eid']; ?>"/>
                        <input name="status[]" class="case" type="checkbox" value="1" /><input name="status[]" class="case" type="hidden" value="0" /></td>
                    <td align="center"><?php echo $row['employee_name'] ?></td>
                </tr>
            <?php }; ?>

            <tr>
                <td></td>
                <td><input type="submit" name="Submit" id="Submit" value="Submit" /></td>       
            </tr>
        </table>
    </form>

this is my insert code

$host = "localhost";
$user = "root";
$pass = "";
$db = "multiple_row_insert";
$con = mysqli_connect($host, $user, $pass, $db);


if (isset($_POST['Submit'])) {
    $eid = $_POST['eid'];
    $count = count($eid);
    for ($i = 0; $i < $count; $i++) {
        $status = $_POST['status'][$i];
        $eid2 = $_POST['eid'][$i];
        $query = "INSERT INTO time(eid,status) VALUES ('$eid2','$status')";
        $query = mysqli_query($con, $query);
    }
}
Balraj Allam
  • 611
  • 6
  • 24
  • You just need to know if 1 checkbox is checked, and that's all, you don't need two checkboxes in this case. – Asfo Nov 17 '16 at 16:59
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! [Don't believe it?](http://stackoverflow.com/q/38297105/1011527) – Jay Blanchard Nov 17 '16 at 17:21

1 Answers1

0
if(isset($_POST['checkbox']))
//then is checked...
else
//is not checked

So basically you only need 1 checkbox, you can change the name of your inputs. And then in your backend use something like:

$_POST['status_'.$i]

On each case and detect if its checked with the "isset" for this $_POST.

Edited:

Your code should look something like this (not tested, sorry if there is any syntax error, you can fix them anyways):

$host = "localhost";
$user = "root";
$pass = "";
$db = "multiple_row_insert";
$con = mysqli_connect($host, $user, $pass, $db);


if (isset($_POST['Submit'])) {
    $eid = $_POST['eid'];
    $count = count($eid);
    for ($i = 0; $i < $count; $i++) {
        $status = 0;
        if(isset($_POST['status_'.$i])) //check if checkbox is setted (value 1 / checked)
          $status = 1;
        $eid2 = $_POST['eid'][$i];
        $query = "INSERT INTO time(eid,status) VALUES ('$eid2','$status')";
        $query = mysqli_query($con, $query);
    }
}

And your frontend code:

<form action="coll.php" method="post" name="create_grading" id="create_grading">  
        <table width="30%" border="0" cellpadding="2" cellspacing="3" class="mainTable">
            <tr>
                <th><input type="checkbox" id="selectall" /></th>
                <th>name</th>
            </tr>
            <?php
            $sql = "select * from employee";
            $query = mysqli_query($con, $sql);
            $i = 0;
            while ($row = mysqli_fetch_array($query)) {
                ?>   
                <tr>
                    <td><input type="hidden" name="eid[]" value="<?php echo $row['eid']; ?>"/>
                        <input name="status_<?php echo $i; ?>" class="case" type="checkbox" value="0" /></td>
                    <td align="center"><?php echo $row['employee_name']; ?></td>
                </tr>
            <?php $i++; } ?>

            <tr>
                <td></td>
                <td><input type="submit" name="Submit" id="Submit" value="Submit" /></td>       
            </tr>
        </table>
    </form>             
Asfo
  • 413
  • 10
  • 20
  • I can't solve this so if give me the full code .....it will help me so much – Md.Shapan Hossain Nov 17 '16 at 18:36
  • Please help me cause I can't under stand what have to be do for solve this problem – Md.Shapan Hossain Nov 17 '16 at 18:42
  • Just use your logic dude, I don't know why AbraCadaver deleted his comment, but basically inside your for, $status = $_POST['status'][$i]; <--At this line, just change this to if(isset($_POST['status_'.$i])){ ---code---} and inside your while change your line to ...finally don't forget to include a $i = 0; and $i++; for your while. – Asfo Nov 17 '16 at 22:51