gpt4 book ai didi

javascript - 数据库表值未通过 AJAX 和 Jquery 更新

转载 作者:行者123 更新时间:2023-12-03 11:08:08 24 4
gpt4 key购买 nike

我的test.php页面如下:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h3>Setting</h3>
<form>
<p>Name:<input type="text" id="updatetherone1"/></p>
<p>Email:<input type="text" id="updateotherone2"/></p>
<p><input id="updateone" type="button" value="Save"/></p>
</form>
<span id="updateotherotherone"></span>
<script src="js/jquery.js"></script>
<script src="js/ajax.js"></script>
</body>
</html>

我的ini.php页面如下:

<?php
session_start();
$_SESSION['state'] ='2';
$conn=new mysqli('localhost','root','','people');
?>

我的test1.php页面如下:

<?php
include 'ini.php';

if (isset($_POST['name'], $POST['email'])){
$name = mysqli_real_escape_string(htmlentities($POST['name']));
$email = mysqli_real_escape_string(htmlentities($POST['email']));

$update = mysqli_query("UPDATE state SET Name='$name', email='$email' WHERE Id=".$_SESSION['state']);

if($update === true){
echo 'Setting have been updated.';
}else if ($update === false){
echo 'There was an error updating your setting.';
}
}
?>

我的ajax.js页面如下:

$('[id=updateone]').click(function(){
var name=$('[id=updateotherone1]').val();
var email=$('[id=updateotherone2]').val();
$('[id=updateotherotherone]').text('Loading...');

$.post('test1.php',{name:name,email:email},function(data){
$('[id=updateotherotherone]').text(data);
});
});

最终代码无法正常工作,也没有显示任何错误,我怀疑 test1.php 页面有问题,有人可以指导吗:

最佳答案

请注意 mysqli_query() 的程序界面,第一个参数需要连接。

$update = mysqli_query($conn, "UPDATE state SET Name='$name', email='$email' WHERE Id=".$_SESSION['state']);

如果这些是拼写错误 $POST ,那么它应该在您的代码中修复。它应该读作 $_POST 。 (除非问题上有拼写错误。)它是 superglobal .

我建议你只使用面向对象的接口(interface)。这样您就不需要每次都添加它:

<?php
include 'ini.php';

if (isset($_POST['name'], $_POST['email'])){
$name = $conn->real_escape_string(htmlentities($_POST['name']));
$email = $conn->real_escape_string(htmlentities($_POST['email']));

$update = $conn->query("UPDATE state SET Name='$name', email='$email' WHERE Id = " . $_SESSION['state']);

if($conn->affected_rows > 0) {
echo 'Setting have been updated.';
} else {
echo 'There was an error updating your setting.';
}
}
?>

不妨使用准备好的语句,因为 mysqli 支持它:

if (isset($_POST['name'], $_POST['email'])){
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);

$sql = 'UPDATE state SET Name = ?, email = ? WHERE Id = ?';
$update = $conn->prepare($sql);
$update->bind_param('ssi', $name, $email, $_SESSION['state']);
$update->execute();

if($update->affected_rows > 0) {
echo 'Setting have been updated.';
} else {
echo 'There was an error updating your setting.';
}
}

关于 JS 部分:

您的表单 id 上也有拼写错误。和 JS:

<p>Name:<input type="text" id="updatetherone1"/></p> <!-- missing o -->
var name=$('[id=updateotherone1]').val();

应该是:<p>Name:<input type="text" id="updatetherone1"/></p>

旁注:

如果您想避免这些愚蠢的拼写错误,只需正确标识它们并为其添加标签即可。示例:

<p>Name:<input type="text" id="name_field"/></p>
<p>Email:<input type="text" id="email_field"/></p>

关于javascript - 数据库表值未通过 AJAX 和 Jquery 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27743558/

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