gpt4 book ai didi

javascript - ajax发布到php并没有真正写入mysql表

转载 作者:行者123 更新时间:2023-11-28 07:18:48 26 4
gpt4 key购买 nike

$(document).ready(function() {
$('#save').click(function() {

var rows = [];

for (i = 1 ; i < document.getElementById("schedule").rows.length ; i++) {
var name;
var tuesday;
var wednesday;
var thursday;
var friday;
var saturday;
var sunday;

name = document.getElementById("schedule").rows[i].cells[0].firstChild.value;
tuesday = document.getElementById("schedule").rows[i].cells[1].firstChild.value;
wednesday = document.getElementById("schedule").rows[i].cells[2].firstChild.value;
thursday = document.getElementById("schedule").rows[i].cells[3].firstChild.value;
friday = document.getElementById("schedule").rows[i].cells[4].firstChild.value;
saturday = document.getElementById("schedule").rows[i].cells[5].firstChild.value;
sunday = document.getElementById("schedule").rows[i].cells[6].firstChild.value;
monday = document.getElementById("schedule").rows[i].cells[7].firstChild.value;

rows[i-1] = "name=" + name + "&tuesday=" + tuesday + "&wednesday=" + wednesday + "&thursday=" + thursday + "&friday=" + friday + "&saturday=" + saturday + "&sunday=" + sunday + "&monday=" + monday;
}

for (i = 0 ; i < rows.length ; i++) {
$.ajax({
type: "POST",
url: "save-schedule.php",
data: rows[i],
success: function () {
alert("POST successful");
}
});
}
});
});

javascript 数组工作正常并显示正确的格式,并且 ajax 成功函数正在执行。它显示警报“POST Success”,但检查 mysql 表后它是空的。

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

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$conn->select_db("little_caesar");

$name=$_POST["name"];
$tuesday=$_POST["tuesday"];
$wednesday=$_POST["wednesday"];
$thursday=$_POST["thursday"];
$friday=$_POST["friday"];
$saturday=$_POST["saturday"];
$sunday=$_POST["sunday"];
$monday=$_POST["monday"];

$conn->query("INSERT INTO schedule (name,tuesday,wednesday,thursday,friday,saturday,sunday,monday) VALUES('$name','$tuesday','$wednesday','$thursday','$friday','$saturday','$sunday','$monday')");

$conn->close();
?>

这是 save-schedule.php 任何帮助都会很棒!预先感谢!

最佳答案

从ajax数据更改中删除“&”符号,而不是像,这样的符号

rows[i-1] = "{name=" + name + ",tuesday=" + tuesday + ",wednesday=" + wednesday + ",thursday=" + thursday + ",friday=" + friday + ",saturday=" + saturday + ",sunday=" + sunday + ",monday=" + monday+"}";

关于javascript - ajax发布到php并没有真正写入mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30534957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com