我在運行以下關閉功能時遇到了一些問題,
因此,帶有我命名為 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">×</span></button>
<strong><span class="text-success" style="margin-top:-50px;"><i class='fa fa-check'></i> 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 時,更新陳述句實際上并沒有運行,同時,當我通過具有相關 ID 的 http 打開解除成功檔案時,它可以正常作業,沒有錯誤并且需要更新,只有當我將表更改為更新。
有沒有人知道這個問題背后可能的原因是什么?
先感謝您
uj5u.com熱心網友回復:
調整 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">×</span>
</button>
<strong>
<span class="text-success" style="margin-top:-50px;">
<i class='fa fa-check'></i>
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在處理用戶提供的資料時使用 a - 否則您的所有辛勤作業都可能被一個惡意用戶破壞!
<?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' );
}
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329487.html
上一篇:無法在CodeIgniter專案中訪問沒有index.php的url
下一篇:Paypalcurl_setopt($ch,CURLOPT_POSTFIELDSwithPHPvariables
