I am having a problem when I try to insert data into mysql. Doing post request and having the data inside php file. No problem at that part. I can iterate the data inside php file and see the results... But when I try to insert data into mysql. then it's giving the 500 (Internal Server Error)
$.ajax({
type: "POST",
url: "insert.php",
data: {
fetch: tasksJson
},
success: function (data) {
console.log("The ajax request succeeded!");
console.log(data);
},
error: function (err) {
console.log(err + "The request failed");
}
});
insert.php file:
$servername = "localhost";
$database = "databaseName";
$username = "example";
$password = "example";
$conn = new mysqli_connect($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST)){
if(isset($_POST['fetch'])){
$array = json_decode($_POST['fetch'],true);
foreach ($array as $key => $val) {
// prepare and bind
$prep = $conn->prepare("INSERT INTO tasks (gid, project, title) VALUES (?, ?, ?)");
$prep->bind_param("sss", $gid, $project, $title);
$gid = $val["gid"];
$project = $val["project_name"];
$title = $val["title"];
$prep->execute();
echo "New records created successfully";
}
}
}
$prep->close();
$conn->close();
Do I missing something here?