vendredi 20 avril 2018

Implement If condition in PHP array

I am new to PHP function. I think following problem can be solved using function. Here I am able to store html form data in mysql which is passed from ajax using following code. But I am little bit confused where to implement if condition. Data has been submitted, now I want to stop data replication. My working php code

if(isset($_POST["section_name"])){
 $section_name = $_POST["section_name"];
 $class_id = $_POST["class_id"];

 for($count = 0; $count<count($section_name); $count++)
 {
     $query =$con->prepare('INSERT INTO section(class_id, section_name) VALUES (:class_id, :section_name)');
     $query->bindParam(':class_id', $class_id);
     $query->bindParam(':section_name', $section_name[$count]);
     $query->execute();
     echo "Section has been assigned";
     }   

     }

Now, I want to include above code in following else condition.

 $query =$con->query('SELECT * FROM section');
    while($row=$result->fetch(PDO::FETCH_ASSOC)){
    if(($_POST["class_id"]==$row["class_id"])&&($_POST["section_name"]==$row["section_name"])){
        echo "Section has already assigned in this class ";

    }
    else{
        // insert...
    }
    }

When I try to merge code, I can't handle. Please help me

Aucun commentaire:

Enregistrer un commentaire