Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
 }
?>
Posted
Comments
Richard Deeming 3-Aug-18 14:17pm    
500 (Internal Server Error) means there was an error executing your PHP code.

You need to find out what that error is, and then fix it.

Use your browser's developer tools to examine the response from the AJAX request, or check the server logs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900