PHP AJAX MySQL DELETE not working
PHP AJAX MySQL DELETE not working
我使用的是Javascript(而不是JQuery)。我正在尝试从MySQL数据库中删除使用复选框选中的记录。使用AJAX不会进行删除。然而,如果我使用相同的参数手动调用PHPAJAX删除脚本,删除操作会很好。然而,当我手动运行AJAX PHP时,即使所有内容都在同一服务器和同一域上,我的Firefox浏览器也会报告COR。
以下是HTML、Javascript和AJAX PHP删除脚本:
如果重要的话,这个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:
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:
<?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";
}
?>
Firebug控制台以红色显示AJAX调用。
firebug在出现间隔服务器错误时以红色显示ajax调用
如果重要的话,这个html在iframe中:
然而,当我手动运行AJAX PHP时,即使所有内容都在同一服务器和同一域上,我的Firefox浏览器也会报告COR。
require_once ('../php/mysql_connect.php');
我想说有一个路径错误,当你试图要求mysql_connect.php
时会产生一个间隔服务器错误,这在直接从浏览器调用时不会发生,例如iFrame可能会干扰原始路径。
事实上,如果iFrame搞砸了,它甚至可能无法到达那里,你可以调用
至于sql注入问题,请了解mysqli或PDO。我真的很喜欢pdo的面向对象方式,这里有一个mysql开发人员
相关文章:
- $rootScope not working
- jQuery document.ready not working
- Javascript getElementsByTagName not working?
- JQuery.val( ) not working
- Tomcat websocket is not working
- Javascript JSON.parse not working
- NodeJS Multer is not working
- Regex not working
- Ember run.debounce not working
- AngularJs 1 not working
- document.getElementById.style.backgroundImage not working
- Rxjs5, distinct() is not working
- createOscillator noteOn not working
- Javascript - .getHours() not working
- Javascript setTimeout not working | onkeydown
- MongoDB $pull not working
- Express router.use not working
- JSONP not working
- window.opener not working in chrome & IE
- Backbone + RequireJS: Collection.each() is not working