1

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 database which is passed from ajax using following code. But I am little bit confused where to implement if condition. If Data has been submitted, 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=$query->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

1 Answer 1

1

You have pretty much everything, you just need to wrap it in a function like this:

function insert() {
    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";
        }   

    }
}

And then you call it like this:

$query =$con->query('SELECT * FROM section');
while($row=$query->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();
}
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks it works, but echo message is looping more than once. How to echo outside of loop. So it would be echo only once
just put it outside the for loop.
Oh no, I just see database, It has inserted same data twice and more. I think problem is caused calling function within while loop. How to solve ?
Instead of querying for all the records, you should use a WHERE clause comparing class_id, section_name. If it return records you know it already exists, if not then you do the insert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.