0

I am submitting html data array through ajax. My form look like:

<form id="myForm">
    <label for="sel1">Select Class:</label>
    <select class="form-control" name="class_id" id="class_id">
        <?php
            $query=$con->query("SELECT * FROM class ORDER BY id ASC") or die($con->error);
            while($row=$query->fetch(PDO::FETCH_ASSOC)){?>
            <option value="<?php echo $row['id']?>"><?php echo $row['class_name']?></option>
        <?php } ?>
    </select>
    <br />
    <label>Add Section: </label>
    <label class="checkbox-inline"><input type="checkbox" name="section_name" value="A">A</label>
    <label class="checkbox-inline"><input type="checkbox" name="section_name" value="B">B</label>
    <label class="checkbox-inline"><input type="checkbox" name="section_name" value="C">C</label>
    <br />
</form>

Array and input has been passed as following way to php

<script>
    $("#submit").click(function(){
        var class_id = $("#class_id").val();
        var section_name = [];
        $("input[name='section_name']:checked").each(function(){
            section_name.push(this.value);
        });
        $.ajax({
                url: 'insert_section.php',
                type: 'post',
                data: {class_id:class_id,section_name:section_name},
                success: function(data){
                    alert(data);
                    $('#myForm')[0].reset();
                }
            });
    }); 
</script>

I am trying to echo message in case of row exist or not. But it doesn't work. What is error in following code. Maybe foreach clause caused problem. Please help

if(isset($_POST["section_name"])){
foreach ($_POST["section_name"] AS $key => $item) {               
$query =$con->prepare("SELECT class_id, section_name FROM section WHERE class_id= ':class_id' && section_name ':section_name')");
$query->bindParam(':section_name',$_POST["section_name"][$key]);
$query->bindParam(':class_id', $_POST["class_id"]);
$query->execute();
$count = $query->fetchColumn();
    if ($count === 1)
    {
        echo "Section found";
    }
    else 
    {
        echo "Section is not found";
    }
}
}

My database looks like

enter image description here

Dipak
  • 931
  • 14
  • 33

1 Answers1

1

Don't include quotes ' in query. Also you can use directly $item varaible instead of $_POST["section_name"][$key] in loop. Change your code as below:

if(isset($_POST["section_name"])){
foreach ($_POST["section_name"] AS $key => $item) {               
$query =$con->prepare("SELECT class_id, section_name FROM section WHERE class_id= :class_id && section_name = :section_name)");
$query->bindParam(':section_name',$item);
$query->bindParam(':class_id', $_POST["class_id"]);
$query->execute();
$count = $query->fetchColumn();
    if ($count === 1)
    {
        echo "Section found";
    }
    else 
    {
        echo "Section is not found";
    }
}
}

As you have to count your result. Change your query as below:

$query =$con->prepare("SELECT COUNT(class_id) as total_ids FROM section WHERE class_id= ':class_id' && section_name ':section_name')");
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • Also `section_name :section_name` is wrong. – u_mulder Apr 21 '18 at 09:08
  • Yes just missed it. corrected @u_mulder – B. Desai Apr 21 '18 at 09:09
  • @B.Desai. Thanks for your concern. But it doesn't work. Still it echo section is not found, though data is in database. It seems problem is in my database or input form. Please see updated question. – Dipak Apr 21 '18 at 09:36
  • check update @DipakOjha – B. Desai Apr 21 '18 at 09:51
  • @B.Desai. When I add array to checkbox, now it return null and insert statement also doesn't work. Before than, insert worked, only if row exist condition was not working. I am re-coding to find from where I am suffering, so I replied to your comment a bit later. Please see my ajax code once. I think problem start from there. Thanks for your continuous support. – Dipak Apr 21 '18 at 10:35
  • ok I have just note that you done it using ajax. And in jquery you are creating array so array nit needed in html name – B. Desai Apr 21 '18 at 10:38
  • In alert what are your getting? – B. Desai Apr 21 '18 at 10:48
  • @B.Desai. it return 'Section is not found', though data is in database (it had to return 'Section Found', in case of data is in database) – Dipak Apr 21 '18 at 10:56
  • check my updated answer – B. Desai Apr 21 '18 at 11:04