[英]Unable to delete content from a row in MySQL table in PHP?
Here is my code:这是我的代码:
$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'");
}
}
Here is the jQuery code:这是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
}
});
}
I am HTML to a row with §
as separator because this character won't occur in normal HTML.我是 HTML 到以
§
作为分隔符的一行,因为这个字符不会出现在普通的 HTML 中。 If an article is already in the row and user clicks on the unsave button I wanted to remove the article using str_replace
, but it does not replace anything.如果文章已经在行中并且用户点击了取消保存按钮,我想使用
str_replace
删除文章,但它不会替换任何内容。
Also, when I click on the save button only once the article gets saved twice.此外,当我仅在文章被保存两次后单击保存按钮时。 I can probably work it out later.
我以后可能会解决。
Edit: I guess the $saving
variable always passes the value true.编辑:我猜
$saving
变量总是传递值 true。 This also results in saving same article multiple times.这也导致多次保存同一文章。
I need help with the removal of unsaved article and also wanted to know if there is a better way to do this.我需要关于删除未保存文章的帮助,也想知道是否有更好的方法来做到这一点。
Here is sample HTML block:这是示例 HTML 块:
<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 MDB08I, which is a build first seen as a factory image earlier in the week. 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>§
You are approaching things wrong.你在处理错误的事情。 Rather than storing all the articles in a single field separated by a magic character, you should have a separate table with a username and a text field.
与其将所有文章存储在由魔术字符分隔的单个字段中,您还应该拥有一个包含用户名和文本字段的单独表。 Then you can arbitrarily add and delete multiple rows to that table for each user.
然后,您可以为每个用户在该表中任意添加和删除多行。 When you want to get all results for a user, you just do
SELECT * FROM new_table_name WHERE username = 'some_user_name'
.当您想获取用户的所有结果时,只需执行
SELECT * FROM new_table_name WHERE username = 'some_user_name'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.