我有这个数据库:
表用户:用户名,密码
表产品:产品ID、产品名称
在我的 admin.php 中,如果我单击 id btn 的按钮,我想在 id show 的 div 中显示 insert.php
admin.php:
<?php
session_start();
include("connection.php");
?>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btnInsert").click(function(){
$("#show").load("insert.php");
});
$("#btnEdit").click(function(){
$("#show").load("edit.php");
});
$("#btnAdd").click(function(){
$("#show").load("insertbrg.php");
});
});
</script>
</head>
<body>
<div id = "menu">
<button id = "btn">Insert</button><BR>
</div>
<div id = "show">
</div>
</body>
</html>
在 insert.php 中:
<?php
session_start();
include("connection.php");
?>
Product ID : <input type = "text" name = "txtID"><BR>
Product Name : <input type = "text" name = "txtName"><BR>
<input type = "button" name = "btnAdd" id = "btnAdd" value = "Add Item to DB">
如果我点击btnAdd,admin.php中的jquery将加载insertbrg.php以将项目添加到数据库
插入brg.php:
<?php
if(isset($_REQUEST['btnAdd'])){
$newID= $_REQUEST['txtID'];
$newName= $_REQUEST['txtName'];
$query = "Select * from Product";
$result = mysql_query($query,$conn);
$idExist = false;
if($result){
while ($row= mysql_fetch_array($result))
{
if ($row["productID"] == $newID){
$idExist = true;
}
}
}
if ($idExist){
echo "ID Exist ";
}else{
$query2="insert into product values('$newID','$newName')";
$result = mysql_query($query2,$conn);
header("location:insert.php");
}
}
?>
如果单击 btnAdd 按钮,我无法将项目插入到我的数据库
我也收到这个警告:
event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
每次我点击 admin.php 中的 btn 按钮
有人知道这是怎么回事吗? 我是 JQuery AJAX 的新手。请帮忙...
最佳答案
更新您的 jQuery 文件,并确保您正确传递要插入的数据,以便 php 可以抓取它们并将其插入数据库。
可以通过get或post方式发送数据,最好使用jQuery中的ajax函数。
您可以在这里找到相关详细信息:http://api.jquery.com/jQuery.ajax/
关于javascript - 无法使用 PHP 和 JQuery Ajax 将新记录插入到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20485206/