我尝试使用AJAX向我的数据库中的表添加数据,但我遇到了麻烦。由于我的脚本现在可以使用,所以只添加了一个字段,其他字段没有添加,我不确定原因。我知道我发布的SQL注入漏洞,但我只是想让这个功能适用于这个演示项目。
HTML
<div id="form_emergencies" class="form_view">
<div class="half" id="systems"></div>
<div class="half" id="malfunctions"></div>
</div>
的jQuery
var Alerts = {
apu: [["APU Power Fail", "APU[1]"], ["APU Power Fault", "APU[2]"], ["APU Generator Fail", "APU[3]"], ["APU High Oil Temperature", "APU[4]"], ["APU Hot Start", "APU[5]"], ["APU Loss Overspeed Protection", "APU[6]"], ["APU Starter Engaged", "APU[7]"], ["APU Fire", "APU[8]"], ["APU Fails Bite Check", "APU[9]"], ["APU Door Fails to Open", "APU[10]"], ["APU No Flame", "APU[11]"], ["Left Fire Bottle Discharge", "APU[12]"]],
avionics: [["ADS 1 Fail", "AVIONICS[1]"], ["ADS 2 Fail", "AVIONICS[2]"], ["ADS 3 Fail", "AVIONICS[3]"], ["AP 1 Fail", "AVIONICS[4]"], ["AP 2 Fail", "AVIONICS[5]"], ["Autopilots Fail", "AVIONICS[6]"], ["Baroset 1 Fail", "AVIONICS[7]"], ["Baroset 2 Fail", "AVIONICS[8]"], ["Baroset 3 Fail", "AVIONICS[9]"], ["CCD 1 Fail", "AVIONICS[10]"], ["CCD 2 Fail", "AVIONICS[11]"], ["Heading Comparison Monitor", "AVIONICS[12]"], ["Heading and Roll Comparison Monitor", "AVIONICS[13]"], ["Display Controller 1 Fail", "AVIONICS[14]"], ["Display Controller 2 Fail", "AVIONICS[15]"], ["IRS 1 Fail", "AVIONICS[16]"], ["IRS 2 Fail", "AVIONICS[17]"], ["IRS 3 Fail", "AVIONICS[18]"], ["Glideslope Antenna Fail", "AVIONICS[19]"], ["MAU 1A Fail", "AVIONICS[20]"], ["MAU 1B Fail", "AVIONICS[21]"], ["MAU 2A Fail", "AVIONICS[22]"], ["MAU 2B Fail", "AVIONICS[23]"], ["MAU 3A Fail", "AVIONICS[24]"], ["MAU 3B Fail", "AVIONICS[25]"], ["MRC 1 Fail", "AVIONICS[26]"], ["MRC 2 Fail", "AVIONICS[27]"], ["GPS Degrade", "AVIONICS[28]"], ["GPS #1 Fail", "AVIONICS[28]"], ["GPS #2 Fail", "AVIONICS[30]"], ["Display Unit 1 Fail", "AVIONICS[31]"], ["Display Unit 2 Fail", "AVIONICS[32]"], ["Display Unit 3 Fail", "AVIONICS[33]"], ["Display Unit 4 Fail", "AVIONICS[34]"], ["GPS - Unable RNP", "AVIONICS[35]"]]
}
var description, breaker;
for(var key in Alerts){
var system = key.toUpperCase();
$("#systems").append("<div class='systems' id='" +key +"'><div class='select_box'></div><h2>" +system +"</h2></div>");
}
$("#systems").on("click", ".systems", function(){
$("#malfunctions").children().remove();
$(this).find(".select_box").each(function(){
$(".selected").removeClass("selected");
$(this).addClass("selected");
})
$.each(Alerts[this.id], function(ind,item){
description = item[0];
breaker = item[1];
$("#malfunctions").append("<div class='systems' data-key='" +key +"' data-breaker='" +breaker +"' data-id='" +description +"'><div class='select_box'></div><p>" +description +"</p></div>");
})
})
$("#malfunctions").on("click", ".systems", function(){
var key = $(this).data("key");
var breaker = $(this).data("breaker");
var id = $(this).data("id");
$(this).find(".select_box").each(function(){
if($(this).hasClass("selected")){
$(this).removeClass("selected");
}
else{
$(this).addClass("selected");
$.post("../php/processing.php", {system: key, description: description, breaker: breaker}, function(response){
if(response.success == "1"){
}
})
console.log(key);
console.log(id);
console.log(breaker);
}
})
})
PHP标题
//在头文件
中 $create_table_malfunctions = "CREATE TABLE IF NOT EXISTS $malfunctions(scenario_id VARCHAR(100), system VARCHAR(10), description VARCHAR(50), breaker VARCHAR(10))";
//处理文件
ob_start();
require("../includes/header.php");
if($_POST["save"]){
header("Location: ../pages/instructor.php");
$instructor = $_POST["name"];
$initials = $_POST["initials"];
$date = $_POST["date"];
$center = $_POST["center_menu"];
$pair1 = $_POST["pair1"];
$pair2 = $_POST["pair2"];
$scenario = $pair1 .$pair2;
$id = $scenario .substr(strtoupper($center, 0,4)) .$date .$initials;
$system = $_REQUEST["system"];
$description = $_REQUEST["description"];
$breaker = $_REQUEST["breaker"];
ob_clean();
$insert_malfunctions = "INSERT INTO `$malfunctions`(`scenario_id`, `system`, `description`, `breaker`) VALUES('" .$id ."', '" .$system ."', '" .$description ."', '" .$breaker ."')";
mysqli_query($connect, $insert_malfunctions);
echo json_encode(array("success" => 1));
}
我在处理脚本中还有两个额外的查询,它们将数据添加到其他表中,但我不认为他们会创建问题。此外,让PHP检查提交按钮的帖子值是否会产生问题?现在可以使用,唯一添加到数据库的字段是scenario_id
字段。
这是我获得的网络响应。
答案 0 :(得分:0)
在插入后将标题放在末尾,因为标题在插入之前重定向页面,因此丢失了后期数据。
header("Location: ../pages/instructor.php");
同时改变这一点;
$insert_malfunctions = "INSERT INTO " . $malfunctions . "( scenario_id, system, description, breaker) VALUES('" .$id ."', '" .$system ."', '" .$description ."', '" .$breaker ."')";