[英]Unable to delete content from a row in MySQL table in PHP?
這是我的代碼:
$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 塊:
<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>§
你在處理錯誤的事情。 與其將所有文章存儲在由魔術字符分隔的單個字段中,您還應該擁有一個包含用戶名和文本字段的單獨表。 然后,您可以為每個用戶在該表中任意添加和刪除多行。 當您想獲取用戶的所有結果時,只需執行SELECT * FROM new_table_name WHERE username = 'some_user_name'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.