gpt4 book ai didi

php - 将 AJAX 连接到 MySQL (PHP)

转载 作者:行者123 更新时间:2023-11-28 23:44:10 25 4
gpt4 key购买 nike

我有一项与 PHP、MySQL、AJAX 和 JQuery 相关的作业到期。目前我遇到脚本问题,希望有人能帮助我。该脚本用于更新 MySQL 中的字段。该脚本似乎不起作用。这是脚本:

function updatedata(id) {
var id = id;
var name = $('#name' + id).val() ;
var url = $('#url' + id).val() ;
var imageurl = $('#imageurl' + id).val() ;
var description = $('#description' + id).val() ;
var datas = "name=" + name + "&url=" + url + "&imageurl=" + imageurl + "&description=" + description;

$.ajax({
type: "POST",
url: "update.php?id=" + id;
data: datas;
}).done(function (data) {
$('#info').html(data);
viewdata();
});
}

以下是“update.php”文件中包含的代码:

<?php
include ('header.php');
if(isset($_GET['id'])) {
$stmt = $connection->prepare("UPDATE news_source SET name=?, url=?, description=?, imageurl=? where id=?");
$stmt->bind_param('sssss', $full_name, $url_1, $descript_ion, $image_url, $id);
$full_name = mysqli_real_escape_string($connection, $_POST ['name']);
$url_1 = mysqli_real_escape_string($connection, $_POST ['url']);
$descript_ion = mysqli_real_escape_string($connection, $_POST ['description']);
$image_url = mysqli_real_escape_string($connection, $_POST ['imageurl']);
$id = $_GET ['id']);

if($stmt->execute()))
{ ?>
<div class = "alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true"> &times; </span>
</button>
<strong>Success!</strong>
Record has been added.
</div>
<?php } else { ?>
<div class = "alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true"> &times; </span>
</button>
<strong>Error!</strong>
Record failed to add.
</div>
<?php }
include ('footer.php');
?>

编辑:该脚本在同一个 php 文件“maintain.php”中被调用为:

<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" onclick="updatedata('<?php echo $rows['id']; ?>')" class="btn btn-primary">Save changes</button>
</div>

编辑:

问题描述:好的,问题是我无法更新MySQL数据库中的字段。主文件应该允许用户编辑 Bootstrap Modal 中存在的数据,然后运行 ​​AJAX 以动态更新数据库而不刷新页面。目前,主文件仅显示可编辑的数据,但我无法更新数据库中的数据。单击“保存更改”按钮后,没有任何反应。

注意:我已尝试使用 How do I get PHP errors to display? 中存在的信息显示 php 错误,但不幸的是它什么也没显示。

最佳答案

您正在绑定(bind)参数然后转义它们。先获取参数并转义,再绑定(bind)而且您没有清理 ID。

$full_name = mysqli_real_escape_string($connection, $_POST ['name']);
$url_1 = mysqli_real_escape_string($connection, $_POST ['url']);
$descript_ion = mysqli_real_escape_string($connection, $_POST ['description']);
$image_url = mysqli_real_escape_string($connection, $_POST ['imageurl']);
$id = intval($_GET ['id']));

$stmt->bind_param(1, $full_name);
$stmt->bind_param(2, $url_1);
$stmt->bind_param(3, $descript_ion);
$stmt->bind_param(4, $image_url);
$stmt->bind_param(5, $id);

关于php - 将 AJAX 连接到 MySQL (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33868575/

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