简体   繁体   中英

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:

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. 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.

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. 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:

<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>§

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' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM