简体   繁体   English

如何使用php loop在mysql中将数据更新为null?

[英]How to update data as null in mysql using php loop?

I have a table named users with a column called user_subs. 我有一个名为users的表,其中的列名为user_subs。 It looks like this. 看起来像这样。

用户表显示user_subs中的用户

In user_subs I have stored the specific users session username. 在user_subs中,我存储了特定用户的会话用户名。 Lets say this specific users name is James. 可以说这个特定的用户名为James。

Now how would I loop through a specific user_subs looking for "James" and remove him from that specific user_subs without removing all the other names. 现在,我将如何在特定的user_subs中循环查找“ James”,并将其从该特定的user_subs中删除,而不删除所有其他名称。

This is what I have so far and the only problem is, its deleting all the usernames in user_subs instead of just "James". 到目前为止,这是我所拥有的,唯一的问题是,它删除了user_subs中的所有用户名,而不仅仅是“ James”。

if(isset($_GET['p_id'])) {
  $the_post_id = $_GET['p_id'];
  $the_post_author = $_GET['author'];
}

if(isset($_POST['delete_sub'])) {
  $username = $_SESSION['username'];

  $query = "SELECT user_subs FROM users WHERE username = '{$username}' ";
  $select_users_by_id = mysqli_query($connection, $query); 

  while ($row = mysqli_fetch_array($select_users_by_id)) {
    $user_subs = explode(',', $row['user_subs']);

    foreach($user_subs as $out) {

      $query = "UPDATE users SET user_subs = null WHERE username = '{$the_post_author}' ";

       $unsubscribe_user = mysqli_query($connection, $query);

       echo "Unsubscribed";
    }
  }
}

THIS IS JUST IN TEST, PREPARED STATEMENTS WILL BE USED BEFORE GOING LIVE 这只是出于测试目的,在上线之前将使用准备好的语句

Thank you for your time. 感谢您的时间。

I second the other user's comment about moving this column to a different table. 我赞同其他用户关于将该列移至其他表的评论。 In the meanwhile, if you want to achieve what you are asking for, you can try removing the user name from the column value and update it with the remaining text. 同时,如果您想实现所需的功能,则可以尝试从列值中删除用户名,并用剩余的文本进行更新。

if(isset($_POST['delete_sub'])) {
  $username = $_SESSION['username'];

  $query = "SELECT user_subs FROM users WHERE username = '{$username}' ";
  $select_users_by_id = mysqli_query($connection, $query); 

  while ($row = mysqli_fetch_array($select_users_by_id)) {
    $user_subs = str_replace($username . ',', '', $row['user_subs']);

$query = "UPDATE users SET user_subs = '{$user_subs}' WHERE username = '{$the_post_author}' ";

       $unsubscribe_user = mysqli_query($connection, $query);
       echo "Unsubscribed";

  }
}

OPTION-2 OPTION-2

$user_subs = explode(',', $row['user_subs']);
$user_subs_new = [];
foreach($user_subs as $out) {
    if ($out !== $username) {
        $user_subs_new[] = $out;
    }
}
$user_subs = implode(',',user_subs_new);
  $query = "UPDATE users SET user_subs = '{$user_subs}' WHERE username = '{$username}' ";

   $unsubscribe_user = mysqli_query($connection, $query);

   echo "Unsubscribed";
}

Let's start over. 让我们重新开始。 Let's start here, in fact... 实际上,让我们从这里开始...

DROP TABLE IF EXISTS users;

CREATE TABLE users
(user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,email VARCHAR(20) NOT NULL UNIQUE
);

DROP TABLE IF EXISTS user_subs;

CREATE TABLE user_subs
(user_id INT NOT NULL
, sub_id INT NOT NULL
, active TINYINT NOT NULL DEFAULT 1
, PRIMARY KEY(user_id,sub_id)
);

INSERT INTO users VALUES 
(1,'b.smyth634@gmail.com'),
(2,'james@gmail.com'),
(3,'f@gmail.com'),
(4,'sally@gmail.com'),
(5,'thomas@gmail.com');

INSERT INTO user_subs (user_id,sub_id) VALUES
(1,5),
(1,2),
(1,1),
(1,4),
(2,1),
(2,2),
(2,4);


SELECT * FROM users;
+---------+----------------------+
| user_id | email                |
+---------+----------------------+
|       1 | b.smyth634@gmail.com |
|       2 | james@gmail.com      |
|       3 | f@gmail.com          |
|       4 | sally@gmail.com      |
|       5 | thomas@gmail.com     |
+---------+----------------------+

SELECT * FROM user_subs;
+---------+--------+--------+
| user_id | sub_id | active |
+---------+--------+--------+
|       1 |      5 |      1 |
|       1 |      2 |      1 |
|       1 |      1 |      1 |
|       1 |      4 |      1 |
|       2 |      1 |      1 |
|       2 |      2 |      1 |
|       2 |      4 |      1 |
+---------+--------+--------+

SELECT u.*
     , GROUP_CONCAT(us.sub_id) subs 
  FROM users u 
  JOIN user_subs us 
    ON us.user_id = u.user_id 
 GROUP 
    BY u.user_id;
+---------+----------------------+---------+
| user_id | email                | subs    |
+---------+----------------------+---------+
|       1 | b.smyth634@gmail.com | 1,2,4,5 |
|       2 | james@gmail.com      | 1,2,4   |
+---------+----------------------+---------+

From here we have a choice. 从这里我们可以选择。 We can either DELETE subs we no longer wish to consider, or simply UPDATE them as 'inactive'. 我们可以DELETE我们不再希望考虑的潜艇,或者只是将其UPDATE为“非活动”。

Either way, we just need a DELETE or an UPDATE . 无论哪种方式,我们只需要DELETEUPDATE So no SELECT needed. 因此不需要SELECT In fact a SELECT would, as I mentioned, be counterproductive - because a user may modify the data set in between the execution of the SELECT and the execution of the UPDATE/DELETE . 正如我所提到的,实际上SELECT会适得其反-因为用户可以在SELECT的执行和UPDATE/DELETE的执行之间修改数据集。 This is known as a 'race condition'. 这就是所谓的“比赛条件”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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