简体   繁体   English

PHP AJAX MySQL DELETE无法正常工作

[英]PHP AJAX MySQL DELETE not working

I am using Javascript (not JQuery) for this. 我为此使用Javascript(而非JQuery)。 I am attempting to delete records from a MySQL database that are selected using check boxes. 我正在尝试从MySQL数据库中删除使用复选框选中的记录。 The deletes do not happen using AJAX. 使用AJAX不会发生删除。 However, it I call the PHP AJAX deletion script manually with the same parameters the delete works fine. 但是,我使用相同的参数手动调用PHP AJAX删除脚本,删除工作正常。 However, when I run the AJAX PHP manually my Firefox browser reports a COR even though everything is on the same server and same domain. 但是,当我手动运行AJAX PHP时,即使所有内容都在同一服务器和同一域上,我的Firefox浏览器也会报告COR。

Here is the HTML, Javascript and AJAX PHP deletion script: 这是HTML,Javascript和AJAX PHP删除脚本:

This html is within an iframe if it matters: 如果重要,则此html位于iframe中:

<form name='frmDelFiles' method='POST' class='frmFileUpload' onClick='delChkdFiles("files2del")'>
<input type='submit' value='Delete Checked Files' name='delFiles' class='btnSubmit' />
</form>

Javascript: Javascript:

function delChkdFiles(theCBgrp) {
    var where_stmt = "";
    var names      = "";
    var elements = document.getElementsByName(theCBgrp);

   for (var i = 0, l = elements.length; i < l; i++)
   {
      if (elements[i].checked)
      {
        var response = confirm("Do you consent to delete the " + elements[i].value + " file?\n\nClick OK if Yes, otherwise Cancel.");
        if (response == true) {
            if (where_stmt == "") {
                where_stmt  = "id=" + elements[i].id;
                names       = elements[i].value;
            } else {
                   where_stmt += " OR id=" + elements[i].id;
                   names      += ", " + elements[i].value;
                }
        }
    }
    }

    if (where_stmt == "") {
        alert("Zero files have been selected for deletion.");
    } else {
        xmlhttp=getHTTPObject();
       if (xmlhttp==null) {
            alert ("Your browser does not support AJAX!");
            return;
       }

        var url="/php/del_mysql_rcd.php";
        url=url+"?table=ip_attachments";
        url=url+"&where_stmt="+where_stmt;
        url=url+"&names="+names;
        url=url+"&sid="+Math.random();

        xmlhttp.open("GET",url,true);
        xmlhttp.onreadystatechange = handleShowResponse;
        xmlhttp.send(null);
    }
}

function handleShowResponse() {
   if (xmlhttp.readyState==4) {
      var ajaxStr = xmlhttp.responseText;
      alert(ajaxStr);
   }
}

del_mysql_rcd.php AJAX PHP: del_mysql_rcd.php AJAX PHP:

<?php
$table      = $_GET["table"];
$where_stmt = $_GET["where_stmt"];
$names      = $_GET["names"];

require_once ('../php/mysql_connect.php'); // Connect to the db.

$query = "DELETE FROM $table WHERE $where_stmt";
$result = @mysql_query ($query) or die(mysql_error()); // Run the query.

if (mysql_affected_rows() > 0) {
    echo "Successfully deleted $names\n";
} else {
    echo "Failed to delete $names\n";
}
?>

The Firebug console shows the AJAX call in red. Firebug控制台以红色显示AJAX调用。

firebug shows ajax calls in red when there's an interval server error 间隔服务器错误时,firebug以红色显示ajax调用

This html is within an iframe if it matters: 如果重要,则此html位于iframe中:

However, when I run the AJAX PHP manually my Firefox browser reports a COR even though everything is on the same server and same domain. 但是,当我手动运行AJAX PHP时,即使所有内容都在同一服务器和同一域上,我的Firefox浏览器也会报告COR。

require_once ('../php/mysql_connect.php'); 

I'd say there's a path error, generating an interval server error when you try to require mysql_connect.php which will not happen when called directly from the browser, eg the iFrame might be messing with the original path. 我想说这是一个路径错误,当您尝试要求mysql_connect.php会生成一个间隔服务器错误,当直接从浏览器直接调用时不会发生,例如,iFrame可能与原始路径混淆。

In fact, it might not even get there if the iFrame messes up with you call 实际上,如果iFrame与您打扰,它甚至可能无法到达目的地

As for the sql injection problem, learn about mysqli or PDO. 至于sql注入问题,请了解mysqli或PDO。 I really like the object oriented way of pdo, here's a tutorial for mysql developpers 我真的很喜欢pdo的面向对象方式,这是mysql开发人员教程

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

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