Click here to Skip to main content
15,606,752 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

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:

<script src=""></script>
<input type="text" name="name" />
<input type="text" name="detail" />

<button type="button" onclick="insert()">Test</button>

<script type="text/javascript">

function insert() {

var name=$("#name").val();
var detail=$("#detail").val();

// AJAX code to send data to php file.
type: "POST",
url: "insert.php",
data: {name:name,detail:detail},
dataType: "JSON",
success: function(data) {
$("p").addClass("alert alert-success");
error: function(err) {






$stmt = $DBcon->prepare("INSERT INTO demo(name,detail) VALUES(:name, :detail)");

$stmt->bindparam(':name', $name);
$stmt->bindparam(':detail', $detail);
  $res="Testimonial Inserted Successfully:";
  echo json_encode($res);
else {
  $error="Not Inserted,Some Probelm occur.";
  echo json_encode($error);



$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "Pass@123";
$DB_name = "practice";
     $DBcon = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
 catch(PDOException $e)
     echo "ERROR : ".$e->getMessage();
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