I want to save user's input in my chatbot to my database. I am trying to use POST API in PHP. This is the code:
<?php
if (isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$phoneCode = $_POST['phoneCode'];
$phone = $_POST['phone'];
if (!empty($username) || !empty($password) || !empty($gender) ||
!empty($email) || !empty($phoneCode) || !empty($phone)) {
$host = "localhost";
$dbUsername = "id8525516_anas";
$dbPassword = "12345678";
$dbname = "id8525516_testdb";
//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
} else {
$SELECT = "SELECT email From testTable Where email = ? Limit 1";
$INSERT = "INSERT Into testTable (username, password, gender, email, phoneCode, phone) values(?, ?, ?, ?, ?, ?)";
//Prepare statement
$stmt = $conn->prepare($SELECT);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($email);
$stmt->store_result();
$rnum = $stmt->num_rows;
if ($rnum==0) {
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("ssssii", $username, $password, $gender,
$email, $phoneCode, $phone);
$stmt->execute();
echo "New record inserted sucessfully";
} else {
echo "Someone already register using this email";
}
$stmt->close();
$conn->close();
}
} else {
echo "All field are required";
die();
}
}
else{
echo 'error occurred';
}
?>
the link in the json api card in chatfuel is:
https://httpsapi.000webhostapp.com/index.php?username= {{username}}&password={{password}}&gender={{gender}}&email={{email}}&phoneCode={{phoneCode}}&phone={{phone}}&submit=Submit
This gives error and the database is not updated. Please point out how can I make it work. Note: I'm very new to php code. Thanks,