array execute in php if empty pdo php

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

Source: php

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.