gpt4 book ai didi

php - 无法从 PHP 中的 MySQL 表中的一行中删除内容?

转载 作者:行者123 更新时间:2023-11-27 23:37:12 26 4
gpt4 key购买 nike

这是我的代码:

$html = $_POST['html'];
$username = $_POST['username'];
$saving = $_POST['saving'];
$username = $connection->real_escape_string($username);
$html = $connection->real_escape_string($html);
$query = $connection->query("SELECT * from members WHERE username ='$username'");
$matches = mysqli_num_rows($query);
if ($matches == 1) {
$row = mysqli_fetch_array($query);
$all_articles = $row['readlater'];
if($saving == true){
$all_articles .= $html.'§';
$connection->query("UPDATE members SET readlater = '$all_articles' WHERE username ='$username'");
} else {
$all_articles = str_replace($html.'§','',$all_articles);
$connection->query("UPDATE members SET readlater = '$all_articles' WHERE username ='$username'");
}
}

这是 jQuery 代码:

if (localStorage.getItem(key) === null) {
localStorage.setItem(key, 'Saved');
$(this).css('color','#278F00');
$(this).closest('.item').find('.fa-bookmark-o').css('font-weight','bold');
$.ajax({
type: "POST",
url: '/savedarticle.php',
data: {
html: innerHTML,
username: '<?php echo $_SESSION['user']; ?>',
saving: true
}
});
}else {
localStorage.removeItem(key);
$(this).css('color','#000');
$(this).closest('.item').find('.fa-bookmark-o').css('font-weight','normal');
$.ajax({
type: "POST",
url: '/savedarticle.php',
data: {
html: innerHTML,
username: '<?php echo $_SESSION['user']; ?>',
saving: false
}
});
}

我在 HTML 中以 § 作为分隔符,因为这个字符不会出现在普通 HTML 中。如果一篇文章已经在行中并且用户点击了取消保存按钮,我想使用 str_replace 删除该文章,但它不会替换任何内容。

另外,当我只点击保存按钮时,文章被保存了两次。我可能稍后会解决这个问题。

编辑:我猜 $saving 变量总是传递值 true。这也会导致多次保存同一篇文章。

我需要有关删除未保存文章的帮助,也想知道是否有更好的方法来执行此操作。

这是示例 HTML block :

<h2>Nexus 5X Receiving Update Today to Build MDB08I<small class="label droidlife">Droid Life</small><button type="button" class="close bkmrk-btn" style="color: rgb(0, 0, 0);"><i class="fa fa-bookmark-o" style="font-weight: normal;"></i></button></h2><p>Nexus 5X devices that are arriving at the homes of Android fans today are receiving a small 42MB update almost immediately out of the box. The build is arriving as&nbsp;MDB08I, which is a build first seen as a factory image earlier in the week.&nbsp; Tough to know what’s new in it, but it’s likely...</p><p>Read More — <a data-id="1445529719-Unv5Og" data-toggle="modal" class="modal-link store-link" href="http://www.droid-life.com/2015/10/21/nexus-5x-receiving-update-today-to-build-mdb08i/" data-target="#myModal">Nexus 5X Receiving Update Today to Build MDB08I</a></p><hr>§

最佳答案

您处理错误的方法。与其将所有文章存储在由魔术字符分隔的单个字段中,不如有一个包含用户名和文本字段的单独表。然后您可以为每个用户任意添加和删除该表中的多行。当您想要获取某个用户的所有结果时,只需执行 SELECT * FROM new_table_name WHERE username = 'some_user_name'

关于php - 无法从 PHP 中的 MySQL 表中的一行中删除内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33386847/

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