I am trying to Insert data into Database using Ajax into MY SQL
Error Message
Quote:
POST http://localhost/Practice/insert.php 500 (Internal Server Error)
send @ jquery.min.js:2
ajax @ jquery.min.js:2
insert @ insertdata.php:20
onclick @ insertdata.php:9
What I have tried:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<input type="text" name="name" />
<input type="text" name="detail" />
<button type="button" onclick="insert()">Test</button>
</body>
<script type="text/javascript">
function insert() {
var name=$("#name").val();
var detail=$("#detail").val();
// AJAX code to send data to php file.
$.ajax({
type: "POST",
url: "insert.php",
data: {name:name,detail:detail},
dataType: "JSON",
success: function(data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function(err) {
alert(err);
}
});
}
</script>
</html>
<?php
include('dbcon.php');
$name=$_POST['name'];
$detail=$_POST['detail'];
$stmt = $DBcon->prepare("INSERT INTO demo(name,detail) VALUES(:name, :detail)");
$stmt->bindparam(':name', $name);
$stmt->bindparam(':detail', $detail);
if($stmt->execute())
{
$res="Testimonial Inserted Successfully:";
echo json_encode($res);
}
else {
$error="Not Inserted,Some Probelm occur.";
echo json_encode($error);
}
?>
<?php
$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "Pass@123";
$DB_name = "practice";
try
{
$DBcon = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "ERROR : ".$e->getMessage();
}
?>