将javascript中的数据源发送到mysql数据库

hlswsv35  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(324)

我在脚本中编写了以下代码,用于将数据源事件保存到数据库中,但什么也没有发生,数据库中没有保存任何值:

$.ajax({
    url:"insert.php",
    type:"POST",
    data:{id:event.id, name:event.name, startDate:event.startDate, endDate:event.endDate},
    success:function(){
        alert("Added Successfully");
    }
})

文件insert.php:

<?php

$connect = new PDO('mysql:host=localhost;dbname=recuperation', 'root', '');
$query = "INSERT INTO events (id, name, startDate, endDate) VALUES (:id, :name, :startDate, :endDate)";
$statement = $connect->prepare($query);
$statement->execute( array(':id'  => $_POST['event.id'], ':name'  => $_POST['event.name'], ':startDate' => $_POST['event.startDate'],':endDate' => $_POST['event.endDate']));

?>
olhwl3o2

olhwl3o21#

您需要在命名json对象时获取发布的值:

{
    id: event.id,
    name: event.name,
    startDate: event.startDate,
    endDate: event.endDate
}

因此,您的post值将位于以下键中: id , name 等等。。e、 g.变更 $_POST['event.name']$_POST['name'] . 试试这个:

$statement->execute( array(':id'  => $_POST['id'], ':name'  => $_POST['name'], ':startDate' => $_POST['startDate'],':endDate' => $_POST['endDate']));

相关问题