Ajax post到PHP实际上没有写到mysql表

ajax post to php not actually writing to the mysql table

本文关键字:mysql 实际上 post PHP Ajax      更新时间:2023-09-26
$(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成功",但在检查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+"}";

可能是MySQL用户名错误。查看您的PHP代码:

$username = "";

你确定你的用户名是空的,因为MySQL默认用户名是'root'