【问题标题】:AJAX to delete database entryAJAX 删除数据库条目
【发布时间】:2016-12-30 05:07:36
【问题描述】:

我是 AJAX 的新手(以及一般的网站开发),我在使用 jQuery 和 AJAX 删除 wordpress 中的数据库条目时遇到了麻烦。
用户选择表中的条目并单击按钮将其删除。 该表是使用数据表插件和大量编码创建的,以使其按我需要的方式工作。
jQuery 代码位于functions.php 中,实际查询位于服务器上的文件中。
我一直使用这个how to delete records from database with an Ajax 作为代码的基线。另外,请注意我正在使用 wordpress 我的问题是它似乎没有从数据库中删除条目。

这是functions.php中的代码

var GroupID2Del = dt.row({
        selected: true
    })
    .data()[23];
if (confirm('Are you sure you want to delete this row?')) {
    jQuery.ajax({
        type: 'POST',
        url: '/wp-content/AJAX/ViewTable_DeleteEntry.php',
        data: 'groupid=' + GroupID2Del,
        success: function(data) {
            dt.row({
                    selected: true
                })
                .remove();
            jQuery('#UserTable')
                .DataTable()
                .draw();
            alert(data);
        }
    });
}

GroupID2Del 是我获取表中选定行的 id 的方式。然后我从表本身中删除该行。该行确实被删除了。

这里是 ViewTable_DeleteEntry.php

<?php
    $dataPosted = $_POST['groupid'];
    $sql = ('delete from wp_piic_formmaker_submits WHERE form_id = 13 and group_id =' . $dataPosted);
    mysql_query($sql);
    $count = mysql_affected_rows();
    print $count; 
?>

function.php 中的警报提供了正确的 sql,但 $count 为空,因为它实际上并没有删除任何内容。

【问题讨论】:

  • 你可以echo delete from wp_piic_formmaker_submits WHERE form_id = 13 and group_id ='.$dataPosted;
  • 调试您的代码,检查您在$_POST 中得到的内容,如果您正确接收数据,然后按照@Akshay 的建议检查查询。
  • 我做了,我不确定我应该看到什么但是,我现在收到一个错误日志,即使我取出了 echo 语句 PHP 警告:mysql_query(): Access denied for user 'ubm'@'localhost'(使用密码:NO)在 /home1/rojectx3/public_html/wp-content/AJAX/ViewTable_DeleteEntry.php 第 8 行 PHP 警告:mysql_query():无法建立到服务器的链接/home1/rojectx3/public_html/wp-content/AJAX/ViewTable_DeleteEntry.php 在第 8 行我想我没有登录!在这个文件中使用用户名和密码会不会不安全?我该怎么做?
  • 我没有连接到数据库或服务,所以我添加了建立连接所需的线路并且它工作。我确实有一个后续问题:我是否应该担心文件中包含用户名和密码?有什么我可以做的吗?

标签: php jquery mysql ajax


【解决方案1】:

所以问题是我没有连接到数据库,所以我在php文件的开头添加了以下几行

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

    mysql_connect($servername, $username, $password, $dbname) or die("Connection failed");
    mysql_select_db($dbname) or die("No Database found.");
?>

【讨论】:

    【解决方案2】:
    function delete_row()
    {   
        $id=$_REQUEST['post_id'];
        $t_response = array('status'=>4,'msg'=>'Invalid Parameter');
        if( $id == false ) {
            $t_response = array('status'=>3,'msg'=>'Invalid ID');
    
        } else {
            global $wpdb;
             $remove = $wpdb->query("DELETE FROM wp_posts WHERE ID = ".$id);
            if($remove) {
                $t_response = array('status'=>1,'msg'=>'Record Deleted');
            } else {
                $t_response = array('status'=>2,'msg'=>'Error Occure While Deleting Your Record Deleted');
            }
        }
        echo json_encode($t_response);
        die();
    }
    

    【讨论】:

    • 请对您的代码提供一个小解释,而不仅仅是将代码粘贴到答案中!谢谢!
    【解决方案3】:

    Ajax 文件

    $.ajax({
        url: custom_js.ajax,
        type: 'POST',
        dataType: "json",
        data: {
            'action': 'my_action',
            'post_id': id
        },
        success: function(data) {
            if (data.status == 1) {
                console.log(data);
                s_row.remove();
            } else {
                alert(data.msg);
            }
        }
    });
    

    【讨论】:

      【解决方案4】:

      请试试这个:

       var myKeyVals = { groupid : GroupID2Del}
          jQuery.ajax(
            {
               type: 'POST',
               url: '/wp-content/AJAX/ViewTable_DeleteEntry.php',
               data: myKeyVals,
               success: function(data)
               {
                  dt.row({selected: true }).remove();                                             
                  jQuery('#UserTable').DataTable().draw();
                  alert(data);
               }
           });
      

      $dataPosted = $_POST['groupid'];
      if($dataPosted != ''){
      $sql = ('delete from wp_piic_formmaker_submits WHERE form_id = 13 and group_id ='.$dataPosted);
          mysql_query($sql); 
      
      $count = mysql_affected_rows();
      } else {
      echo 'Blank Record';
      }
      

      【讨论】:

      • 这不起作用 Uncaught ReferenceError: myKeyVals is not defined
      猜你喜欢
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2021-12-24
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多