【问题标题】:how to handle mysql update error using ajax如何使用ajax处理mysql更新错误
【发布时间】:2021-12-08 01:51:06
【问题描述】:

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

因此,带有我命名为 notification-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>

dismisssuccess.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 时,更新语句实际上并没有运行,同时,当我通过 http 打开具有相关 id 的dismisssuccess 文件时,它可以正常工作且没有错误并且是否需要更新,只有当我更改要更新的表。

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

提前谢谢你

【问题讨论】:

  • 您尝试过什么来解决问题?你被困在哪里了?另外,请注意您的 UPDATE 查询对 SQL 注入开放
  • 您生成的 HTML 标记嵌套不正确.. 一部分以 &lt;strong&gt;&lt;span&lt; 开头但以 &lt;/strong&gt;&lt;br&gt;To confirm reading this message please press x button &lt;/span&gt; 结尾
  • 另外——你的 ajax 函数对任何返回的内容都不做任何事情,那么为什么还要在 php 中生成任何内容呢?
  • 警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your dataEscaping is not enough!
  • @NicoHaase 我尝试过的是 1. 使用 http 和目标 id 运行操作文件和更新 stmt 仅与通知文件分开工作 2. 更改要更新的目标表也使我的代码成为充分发挥作用

标签: php mysql ajax


【解决方案1】:

调整 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
           }
        }
    }
?>

使用外部注册的事件处理程序,为什么不使用fetch api ~ 看起来稍微短一些,是一个更好的 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' );
    }
?>

【讨论】:

  • 同样的事情,意外没有通过通知点php文件更新
  • 您是否在开发者控制台中看到任何错误?您能否确认 AJAX 请求正在到达指定的端点并且它正在按预期发送 ID?!
  • OK - 刚刚运行了一个小测试,button 中的 span 元素主要传播事件,因此需要对 Javascript 函数进行一些调整。我的错-对不起伙计。
  • 是的,在截断 span 标签后它现在可以工作了!谢谢你为我节省了时间
猜你喜欢
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
相关资源
最近更新 更多