gpt4 book ai didi

html - 无法使用PDO和ajax插入数据

转载 作者:行者123 更新时间:2023-11-29 20:10:49 25 4
gpt4 key购买 nike

模态表单运行良好,但是在尝试插入数据时,代码无法运行,也没有显示任何错误。我正在使用ajax在数据库中进行数据插入操作。请帮我修复这个错误。

index.php

    <!DOCTYPE>
<html>
<head>
<title>Student Information</title>
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="assets/css/style.css"/>
</head>
<body>
<div class="container">
<p></p>
<p></p>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary " data-toggle="modal" data-target="#myModal">
Add Record
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="myModalLabel">Add New Record</h4>
</div>
<form>
<div class="modal-body">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Full Name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email Address">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone Number">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" name="address" placeholder="Address">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" onClick="saveData()">Save changes</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!--------->
<script src="assets/js/jquery-3.1.1.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script>
function saveData(){
var name = $('#name').val();
var email = $('#email').val();
var phone = $('#phone').val();
var address = $('#address').val();
$.ajax({
type: "POST",
url: "server.php?p=add",
data: "nm="+name+"&em="+email+"&ph="+phone+"$ad="+address,
success:function(msg){
alert('Success insert data');
}
});
}
</script>
</body>
</html>


server.php
----------

<?php
$servername = "localhost";
$username = "root";
$password = "";

try {
$conn = new PDO("mysql:host=$servername;dbname=db_student", $username, $password);

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$page = isset($_GET['p'])?$_GET['p']:'';
if($page=='add'){
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$stmt = $db->prepare("insert into student (name, email, phone, address) values(?,?,?,?)");
$stmt->bindParam(1,$name);
$stmt->bindParam(2,$email);
$stmt->bindParam(3,$phone);
$stmt->bindParam(4,$address);
if($stmt->execute()){
echo "Saved";
}else{
echo "Not Saved";
}
}else if($page=='edit'){

}else if($page=='del'){

}
?>

最佳答案

$stmt = $db->prepare('您的查询') 更改为 $stmt = $conn->prepare('您的查询')

关于html - 无法使用PDO和ajax插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com