如何使用PHP和jquery在mysql中插入数据 [英] How to insert data in mysql using PHP and jquery

查看:71
本文介绍了如何使用PHP和jquery在mysql中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ajax将数据插入数据库到MY SQL





错误消息



I am trying to Insert data into Database using Ajax into MY SQL


Error Message

Quote:

POST http://localhost/Practice/insert.php 500(内部服务器错误)

发送@jquery.min.js:2

ajax @ jquery.min.js:2

insert @ insertdata.php:20
onclick @ insertdata.php:9

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





我尝试过:



< html>

< head>

< script src =https://ajax.googleapis.com/ ajax / libs / jquery / 3.3.1 / jquery.min.js>< / script>

< / head>

< body>

< input type =textname =name/>

< input type =textname =detail/>



< button type =buttononclick =insert()>测试< / button>

< / body>



< script type =text / javascript>



fun ction insert(){



var name = $(#name)。val();

var detail = $( #detail)。val();



//将数据发送到php文件的AJAX代码。

$ .ajax({

类型:POST,

url:insert.php,

数据:{name:name,detail:detail},

dataType:JSON,

成功:函数(数据){

$(#message)。html(data); < br $>
$(p)。addClass(alert alert-success);

},

错误:function(err){

警报(错误);

}

});



} < br $> b $ b

< / script>



< / html>





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();
 }
?>

推荐答案

(#name)。val();

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


(#detail)。val();



// AJAX代码将数据发送到php文件。
("#detail").val();

// AJAX code to send data to php file.


.ajax({

类型:POST,

url: insert.php,

数据:{name:name,detail:detail},

dataType:JSON,

成功:功能(数据){
.ajax({
type: "POST",
url: "insert.php",
data: {name:name,detail:detail},
dataType: "JSON",
success: function(data) {


这篇关于如何使用PHP和jquery在mysql中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆