I am trying to insert values into MySQL database but have not been successful. I have tried to edit the SQL statement several times without success. When I ran the same insert statement inside phpMyAdmin, it worked. I really don't know what is wrong with my code. The most frustrating part is there is no error. mysqli_error($conn)
returns nothing.
The following code is what I have been working on:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$servername = "XXXXXXXXXXXXXXX";
$database = "RRRRRRRRRRRRR";
$username = "DDDDDDDD";
$password = "ZZZZZZ";
$data = json_decode(file_get_contents('php://input'), true);
$uid = $data["id"];
$firstname = $data["firstname"];
$lastname = $data["lastname"];
$email = $data["email"];
$password = $data["password"];
$activationCode = $data["activationCode"];
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo "Connected successfully";
}
$sql = "INSERT INTO studentinfo (studentid, firstname, lastname, email, mypass, activationCode)
VALUES ('$uid', '$firstname', '$lastname', '$email', '$password', '$activationCode')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
print mysqli_error($conn);
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
</body>
</html>
The following is what was return from echo $SQL:
INSERT INTO studentinfo (studentid, firstname, lastname, email, mypass, activationCode) VALUES ('androidid', 'wendu', 'Chao', 'eeeee@yahoo.com', 'xxxxxxxx', '0ecccvvjdkfkf')