query("SELECT id, username, enabled FROM app_users WHERE BINARY username = '$username' AND enabled = 1"); $queryres = $query1->fetch_array(); $userID = $queryres[0]; // Check if the contact is already in the 'contacts' table $query2 = $mysqli->prepare("SELECT id, user_id, contact_name FROM contacts WHERE user_id=? AND contact_name=?"); $query2->bind_param("is", $userID, $contactName); $query2->execute(); $fetchInfo = $query2->get_result(); $contactdata = $fetchInfo->fetch_row(); $contactExists = $contactdata[0]; $currentTime = date('Y-m-d H:i:s'); if ($contactExists == '') { $query3 = $mysqli->prepare("INSERT INTO contacts (user_id, contact_name, contact_desc, extension_number, contact_mobile, contact_num1, contact_num2, contact_email, date_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); $query3->bind_param("issssssss", $userID, $contactName, $contactDesc, $extensionNumber, $contactMobile, $contactNum1, $contactNum2, $contact_email, $currentTime); if ($query3->execute()) { $messagetosend = 'success'; } else { $messagetosend = 'An error occurred while attempting to save the contact to the database!'; } } else { $messagetosend = 'Error! A contact with the same name is already in the database!'; } $response = array('result' => $messagetosend); echo json_encode($response); } else { header("Location: roundpin-login.php"); } ?>