繁体   English   中英

无法使用PHP和JQuery Ajax向mysql数据库插入新记录

[英]Can't insert new record to mysql db with PHP and JQuery Ajax

我有这个数据库:

表用户:用户名,密码

表产品:productID,productName

在我的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">
         &nbsp;
     </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以将Item添加到数据库

insertbrg.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的新手。 请帮忙...

这是因为没有发送任何内容

$("#show").load("insertbrg.php");

它必须接受诸如的参数

$("#show").load("insertbrg.php?txtid="+txtid);

更新您的jQuery文件,并确保您传递正确插入的数据,以便php可以抓取它们并插入数据库。

你可以通过get或post发送数据,最好在jQuery中使用ajax函数。

你可以在这里找到相关的细节: http//api.jquery.com/jQuery.ajax/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM