Jump to content

checking query execution in foreach loops


coderscoven

Recommended Posts

I have an issue that is bothering me here. I have a php script with several queries which stores information into mysql database. But before doing so, i first check to see whether their execution is okay. For example;

$query1 = $conn->prepare("insert into student(name) values('$stud_name')");
$check1 = $query1->execute();

foreach($subject as $key=>$n){
$query2 = $conn->prepare("insert into subjects(subject_id, title) values('$n', '$title[$key]')");
$check2 = $query2->execute();
}
foreach($teacher as $key=>$n){
$query3 = $conn->prepare("insert into subjects(teacher_id, teacher_name)values('$n', '$teacher[$key]')");
$check3 = $query3->execute();
}

if($check1 && $check2 && $check3){
    echo "Saved";
}
else{
    echo "Error";
}

Now as you see am checking a normal insertion with an array which seems "abnormal". If am to do the checking within the foreach loop, the message echoing would be repeated basing on the number of inputs. Any assistance is welcome.

 

 

 

Link to comment
Share on other sites

What bothers me is that you've totally missed the point of prepared statements.

 

- Prepare a statement once before the loop that uses it.

- Don't put values directly into the query. Use placeholders.

- Execute the query with the values for the placeholders.

$query1 = $conn->prepare("insert into student(name) values(:name)");
$check1 = $query1->execute(["name" => $stud_name]);

$query2 = $conn->prepare("insert into subjects(subject_id, title) values(:subject_id, :title)");
foreach($subject as $key=>$n){
$check2 = $query2->execute(["subject_id" => $n, "title" => $title[$key]]);
}

$query3 = $conn->prepare("insert into subjects(teacher_id, teacher_name)values(:teacher_id, :teacher_name)");
foreach($teacher as $key=>$n){
$check3 = $query3->execute(["teacher_id" => $n, "teacher_name" => $teacher[$key]]);
}

if($check1 && $check2 && $check3){
    echo "Saved";
}
else{
    echo "Error";
}
As for your question, you have to decide what you want to do in case of failure. Ignore and keep inserting? Stop inserting and reverse all the additions? Reverse just the additions to the affected table?
Link to comment
Share on other sites

To be honest here, I had never heard of transactions in PHP. Its time I learned something new. Thanks

 

 

That's because there are no Transaction in Php. That is a Mysql function. Here are a couple links to get you going.

 

http://www.mysqltutorial.org/mysql-transaction.aspx

 

https://www.w3resource.com/mysql/mysql-transaction.php

 

And of course you want to RTFM

 

https://dev.mysql.com/doc/refman/5.7/en/commit.html

Link to comment
Share on other sites

Yes, you need transactions. But, just from a logic perspective the way you were checking for errors was faulty. If $teacher, for example, has five records, the value of $check3 only contains the result of the query for the last record. You could have checked the result of the execution and increment the number of errors on each execution.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.