0

I'm trying post 3 different values of check box using this code. If I post different values, the same value is getting posted into 3 of them.

<input type="checkbox" name="s_status[]" value="absent" /></td>
<input type="checkbox" name="s_status[]" value="present" /></td>
<input type="checkbox" name="s_status[]" value="leave" /></td>

<?php
require 'conn/Session.php';
require 'conn/MySQL.php';
require_once("includes/generalFunction.php");
require_once("classes/class.SiteManager.php");
$dbcon =  new MySQL();
$siteObj =  new SiteManager();
require 'conn/checkSession.php';
if($_POST['submit']) 
{
    $content=array("student_id"=>$_POST['student_id'],"parent_id"=>$_POST['parent_id'],"student_name"=>$_POST['student_name'],"s_status"=>$_POST['s_status'],"class"=>$_POST['class'],"section"=>$_POST['section']);
    $content1=array("student_id"=>$_POST['student_id'],"parent_id"=>$_POST['parent_id'],"student_name"=>$_POST['student_name'],"s_status"=>$_POST['s_status'],"class"=>$_POST['class'],"section"=>$_POST['section']);
    $content2=array("student_id"=>$_POST['student_id'],"parent_id"=>$_POST['parent_id'],"student_name"=>$_POST['student_name'],"s_status"=>$_POST['s_status'],"class"=>$_POST['class'],"section"=>$_POST['section']);


    $dbcon->insert_query("tbl_attandence",$content);
    $dbcon->insert_query("tbl_attandence",$content1);
    $dbcon->insert_query("tbl_attandence",$content2);
    $mess="Record created successfully.";
    $url="all_attandence.php?mess=".base64_encode($mess);
    redirectPage($url);
}
?>
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107

3 Answers3

0

I have changed your code. Please check:-

require 'conn/Session.php';
require 'conn/MySQL.php';
require_once("includes/generalFunction.php");
require_once("classes/class.SiteManager.php");

$dbcon =  new MySQL();

$siteObj =  new SiteManager();

require 'conn/checkSession.php';

if($_POST['submit']) 
{
    $content=array("student_id"=>$_POST['student_id'],"parent_id"=>$_POST['parent_id'],"student_name"=>$_POST['student_name'],"s_status"=>$_POST['s_status'][0],"class"=>$_POST['class'],"section"=>$_POST['section']);
    $content1=array("student_id"=>$_POST['student_id'],"parent_id"=>$_POST['parent_id'],"student_name"=>$_POST['student_name'],"s_status"=>$_POST['s_status'][1],"class"=>$_POST['class'],"section"=>$_POST['section']);
    $content2=array("student_id"=>$_POST['student_id'],"parent_id"=>$_POST['parent_id'],"student_name"=>$_POST['student_name'],"s_status"=>$_POST['s_status'][2],"class"=>$_POST['class'],"section"=>$_POST['section']);

    $dbcon->insert_query("tbl_attandence",$content);
    $dbcon->insert_query("tbl_attandence",$content1);
    $dbcon->insert_query("tbl_attandence",$content2);
    $mess="Record created successfully.";
    $url="all_attandence.php?mess=".base64_encode($mess);
    redirectPage($url);
}
Bizley
  • 17,392
  • 5
  • 49
  • 59
Webdev
  • 617
  • 6
  • 24
0

When you retrieve s_status on the server side, it's an array. You have to iterate over this array or query it with an index:

<?php
require 'conn/Session.php';
require 'conn/MySQL.php';
require_once("includes/generalFunction.php");
require_once("classes/class.SiteManager.php");
$dbcon =  new MySQL();
$siteObj =  new SiteManager();
require 'conn/checkSession.php';
if($_POST['submit']) 
{
    $content=array(
        "student_id"=>$_POST['student_id'],
        "parent_id"=>$_POST['parent_id'],
        "student_name"=>$_POST['student_name'],
        "s_status"=>$_POST['s_status'][0], //First
        "class"=>$_POST['class'],
        "section"=>$_POST['section']
    );
    $content1=array(
        "student_id"=>$_POST['student_id'],
        "parent_id"=>$_POST['parent_id'],
        "student_name"=>$_POST['student_name'],
        "s_status"=>$_POST['s_status'][1], //Second
        "class"=>$_POST['class'],
        "section"=>$_POST['section']
    );
    $content2=array(
        "student_id"=>$_POST['student_id'],
        "parent_id"=>$_POST['parent_id'],
        "student_name"=>$_POST['student_name'],
        "s_status"=>$_POST['s_status'][2], //Third
        "class"=>$_POST['class'],
        "section"=>$_POST['section']
    );


    $dbcon->insert_query("tbl_attandence",$content);
    $dbcon->insert_query("tbl_attandence",$content1);
    $dbcon->insert_query("tbl_attandence",$content2);
    $mess="Record created successfully.";
    $url="all_attandence.php?mess=".base64_encode($mess);
    redirectPage($url);
}
?>

As suggested by David JorHpan, you can optimize your code by eliminating the duplicate code (I'm asssuming here your MySQL class return true or false depending on the success of the query):

<?php
require 'conn/Session.php';
require 'conn/MySQL.php';
require_once("includes/generalFunction.php");
require_once("classes/class.SiteManager.php");
$dbcon =  new MySQL();
$siteObj =  new SiteManager();
require 'conn/checkSession.php';
if($_POST['submit']) 
{
    $success = true;
    foreach ($_POST['s_status'] as $s_status)
    {
        $content=array(
            "student_id"    =>  $_POST['student_id'],
            "parent_id"     =>  $_POST['parent_id'],
            "student_name"  =>  $_POST['student_name'],
            "s_status"      =>  $s_status,
            "class"         =>  $_POST['class'],
            "section"       =>  $_POST['section']
        );
        $success = $dbcon->insert_query("tbl_attandence",$content) and $success;
    }
    if($success)
    {
        $mess = "Records created successfully.";
    }
    else
    {
        $mess = "Error during the records creation.";
    }
    $url = "all_attandence.php?mess=".base64_encode($mess);
    redirectPage($url);
}
?>

Notice:

I don't know what's inside your MySQL.php file but it looks like you pass the $_POST values directly to MySQL. I strongly advise you to use PDO or similar to protect from SQL injection.

Community
  • 1
  • 1
Veve
  • 6,643
  • 5
  • 39
  • 58
0

Your current checkbox html is the best for access data in action file ,try query with foreach

if($_POST['submit']) 
{
$send = false;
foreach ($_POST['s_status'] as $sts) {
    $content= array("student_id"=>$_POST['student_id'],"parent_id"=>$_POST['parent_id'],"student_name"=>$_POST['student_name'],"s_status"=>$sts,"class"=>$_POST['class'],"section"=>$_POST['section']);
    $send   = $dbcon->insert_query("tbl_attandence",$content);
}
if($send){
$mess="Record created successfully.";
}
else {
$mess = "Error";
}
$url="all_attandence.php?mess=".base64_encode($mess);
redirectPage($url);                        
}
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52