0
foreach($parentinfojson as $value) {
    if (!empty($parentinfojson )) {
        $stmt2 = $dbh -> prepare("INSERT INTO parentinfo (last_name,first_name,status) VALUES (:lastname,:firstname,:status)");
        $stmt2_ = $stmt2 -> execute(array(':firstname' => $value['firstname'], ':lastname' => $value['lastname'], ':status' => $status));
    } else {
        $stmt2_ = $stmt2 -> execute();
    }

}

if ($stmt2_ && $stmt3_ && $stmt1_ && $stmt_ && $stmt5_ && $stmt4_) {
    echo json_encode(array(
        'error' => false,
        'message' => "Added"
    ));
}

This is my execute in Inserting new data in the table. When i tested the adding of empty data(parentinfojson is empty) i get error that Notice: Undefined variable: stmt2_. What i did is i added an else statement and i initialize the variable still i get error. I tried to echo something in the else statement as well but i get error. Now I run out of idea on how to initialize the variable when the json is empty so that i dont get the error undefined variable

2
  • I don't understand why you set your execute in a var Commented May 21, 2015 at 8:45
  • i have many exec so i need to have them like that so i can check if they all run that is the time i show it is added Commented May 21, 2015 at 8:47

2 Answers 2

1

You just defined $stmt2 inside the loop, if $parentinfojson is empty it'll certainly get undefined. Why not define/initialize it.

// initialize up top
$stmt_ = $stmt1_ = $stmt2_ = $stmt3_ = $stmt4_ = $stmt5_ = false;

$stmt2 = $dbh->prepare("INSERT INTO parentinfo (last_name,first_name,status) VALUES (:lastname,:firstname,:status)");
foreach($parentinfojson as $value) {
    $stmt2_ = $stmt2->execute(array(
        ':firstname' => $value['firstname'], 
        ':lastname' => $value['lastname'], 
        ':status' => $status
    ));
}

if ($stmt2_ && $stmt3_ && $stmt1_ && $stmt_ && $stmt5_ && $stmt4_) {
    echo json_encode(array(
        'error' => false,
        'message' => "Added"
    ));
}

Sidenote: Another way would be to build the query dynamically, including the placeholders and the values. So that in turn, you don't have to loop each batches of insert but instead, creating the SQL batch insert then binding all of the values into one single insert invocation:

$stmt_ = $stmt1_ = $stmt2_ = $stmt3_ = $stmt4_ = $stmt5_ = false;

if(!empty($parentinfojson)) {
    $base_query = 'INSERT INTO parentinfo (last_name, first_name, status) VALUES ';
    $placeholders = implode(',', array_map(function($batch){
        return '(' . implode(',', array_fill(0, count($batch), '?')) . ')';
    }, $parentinfojson));
    $base_query .= $placeholders;
    $parentinfojson = call_user_func_array('array_merge', array_map('array_values', $parentinfojson));
    $stmt2 = $dbh->prepare($base_query);
    $stmt2_-> $stmt2->execute($parentinfojson);
}
Sign up to request clarification or add additional context in comments.

4 Comments

$stmt2; this is actually what i tried. I put it before the foreach when i used your code and put it together with all initialization it worked. i dont understand what is the difference of your way and mine lol but it worked anyway
your updated answer that is exactly what i did.it worked. what is the difference of what you did and what i did can you explain a bit
@HogRider just intialized the variable, you see when $parentinfojson if empty, your variables inside will never be defined thus the undefined variable. and i just took off that prepare outside, you don't need to loop the ->prepare you only need it once, from there, loop the ->execute
i need put all other prepare outside of the for loop.lol thanks for clarification
0

There might be a chance that the json variable you are receiving is not empty, so you should also add a check for valid json, this is the function to check a valid json

function isJson($string) {
   json_decode($string);
   return (json_last_error() == JSON_ERROR_NONE);
}

2 Comments

i added print_r(); before and it returned Array ( ) so it is an empty array.
So then if it is an array, then add check as count ($parentinfojson) > 0)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.