如何使用ajax处理mysql更新错误



我在运行以下解散函数时遇到了一些问题,

所以我命名为notifications-success.php的带有警报引导的页面如下所示:

<?php 
$root = realpath(str_replace('\', '/', $_SERVER['DOCUMENT_ROOT']) ); 
include ($root . '/insights/ss/onix.php'); 
$result = mysqli_query($mysqli,"select * from notifications where seen = 0");
if ($result)
{      
if($result->num_rows) {
while($row = mysqli_fetch_assoc($result))
{?> 

<div class='alert alert-success alert-dismissible' role='alert' style='margin-left:-12px;'>
<button type="button" class="close" onClick="updateId('<?php echo $row['id'];?>')" data-dismiss="alert" aria-label="Close" style="float:left!important; border:0; background:none;"><span aria-hidden="true">&times;</span></button>
<strong><span class="text-success" style="margin-top:-50px;"><i class='fa fa-check'></i> &nbsp; &nbsp; &nbsp; File has been moved successfully</strong><br>To confirm reading this message please press x button </span></div>
<?php       }
}

}
?>

<script>
function updateId(id)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "dismisssuccess.php?id=" +id, true);
xmlhttp.send();
}
</script>

动作文件是解散成功。php,如下所示:

<?php 
if(isset($_GET['id']) && !empty($_GET['id']))
{
$id = $_GET['id'];
$ip = getenv('REMOTE_ADDR');
$root = realpath(str_replace('\', '/', $_SERVER['DOCUMENT_ROOT']) ); 
include ($root . '/insights/ss/onix.php'); 
$update = "UPDATE notifications SET seen = 1 , seenby = '$ip' WHERE id = '".$id."'";
if (mysqli_query($mysqli, $update))
{
echo "success";   


} 
else 
{
echo "There is some error";
}
die;
}
?>

现在当我按下x时,更新语句实际上并没有运行,同时,当我通过具有相关id的http打开驳回成功文件时,它可以正常工作,没有错误,并执行所需的更新,也可以正常工作,只有当我将表更改为更新时。

有人知道这个问题背后可能的原因吗?

提前谢谢你

调整PHP &HTML,这样嵌套是正确的,并为按钮分配一个新的数据集属性,而不是内联事件处理程序。

<?php
$root = realpath(str_replace('\', '/', $_SERVER['DOCUMENT_ROOT']) ); 
include ($root . '/insights/ss/onix.php'); 
$result = mysqli_query($mysqli,"select * from notifications where seen = 0");
if ($result){
if($result->num_rows) {
while($row = mysqli_fetch_assoc($result)){
?> 

<div class='alert alert-success alert-dismissible' role='alert' style='margin-left:-12px;'>
<button type="button" class="close" data-id="<?=$row['id'];?>" data-dismiss="alert" aria-label="Close" style="float:left!important; border:0; background:none;">
<span aria-hidden="true">&times;</span>
</button>
<strong>
<span class="text-success" style="margin-top:-50px;">
<i class='fa fa-check'></i>
&nbsp;&nbsp;&nbsp;File has been moved successfully
</span>
</strong>
<br>
To confirm reading this message please press X button 
</div>
<?php
}
}
}
?>

使用外部注册的事件处理程序,为什么不使用fetchapi呢~看起来稍微短一些,是一个更好的api。

<script>
function updateId(e){
e.stopPropagation();
let id=e.target!=e.currentTarget ? e.target.parentNode.dataset.id : e.target.dataset.id;
fetch( 'dismisssuccess.php?id='+id )
.then(r=>r.text())
.then(text=>console.log(text))
}
document.querySelectorAll('div[role="alert"] button[data-id]').forEach(bttn=>bttn.addEventListener('click',updateId))
</script>

在PHP中,你真的应该在处理用户提供的数据时使用prepared statement-否则你所有的努力工作都可能被一个恶意用户撤销!

<?php 
if( !empty( $_GET['id'] ) ){
$id = $_GET['id'];
$ip = getenv('REMOTE_ADDR');

$root = realpath(str_replace('\', '/', $_SERVER['DOCUMENT_ROOT']) );
include ($root . '/insights/ss/onix.php'); 
$sql='UPDATE `notifications` SET `seen`=1, `seenby`=? where `id`=?';

$stmt=$mysqli->prepare($sql);
$stmt->bind_param('ss',$ip,$id);
$stmt->execute();
$rows=$stmt->affected_rows;
$stmt->close();

exit( $rows ? 'Success' : 'There is some error' );
}
?>

最新更新