Jump to content

checking query execution in foreach loops


Go to solution Solved by requinix,

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.

 

 

 

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?
  • Solution

Then you need transactions. Start a transaction at the start, monitor each operation for success and failure, then commit if everything was successful or rollback if anything failed.

  • Like 1

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

  • Like 1

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.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.