$sql = "select * from buddyup where kid_id=:kid_id"; $sth = $conn->prepare($sql); $sth->bindValue('kid_id', $kid); try { $sth->execute(); } catch (Exception $e) { } $result = $sth->fetchAll(); if (count($result)) { $success = '1'; $msg = "Buddy Code For Kid"; $code = $result[0]['buddy_code']; } else { $success = '1'; $msg = "Buddy Code For Kid"; $code = DataClass::generateBuddyCode(8); $sql = "INSERT INTO `buddyup` (`id`, `user_id`, `kid_id`,`buddy_code`,`created_on`) VALUES (DEFAULT, :user_id, :kid_id,:buddy_code,NOW())"; $sth = $conn->prepare($sql); $sth->bindValue('buddy_code', $code); $sth->bindValue('kid_id', $kid); $sth->bindValue('user_id', $uid); try { $sth->execute(); } catch (Exception $e) { } } } // +-----------------------------------+ // + STEP 4: send json data + // +-----------------------------------+ if ($success == '1') {