Remove a certain value from multiple rows in a table?

Asked

Viewed 47 times

0

I’m having trouble creating an algorithm to scan and delete certain emails from email groups.

I have a table where company emails are registered that are displayed on the intranet, with these values are created groups of emails that are stored in another table, what I need to do is the following:

-When I delete an email from the email table, we also perform a search for the groups of emails that in the column gets this way ([email protected];[email protected];[email protected];[email protected]) and remove from the groups too, I’m trying to do this way:

$email_i =  $_POST["email-interno"];    
$buscagrupos    =   mysql_query("SELECT emails FROM grupoemail WHERE emails LIKE '%$email_i%'");
        while($lc1  =   mysql_fetch_array($buscagrupos)){
            $g_emails       =   $lc1["emails"];
            $g_emailsE  =   explode(";", $g_emails);
            $key = array_search($email_i, $g_emailsE);
            if($key!==false){
            unset($g_emailsE[$key]);
            $g_emails2  =   implode(";", $g_emailsE);
            $teste2 =   mysql_query("UPDATE grupoemail SET email='$g_emails2' WHERE emails LIKE '%$email_i%'");
            }}

But when I run this, the script ta mixing all the emails and adding all the existing emails from the lines that had the email deleted and added all of them equally in the same lines, then instead of occurring this:

Grupo 1:
[email protected];[email protected];[email protected]
Grupo2
[email protected];[email protected];[email protected]

Delete the example 3 from emails, these groups should be without it this way:

Grupo 1:
[email protected];[email protected];
Grupo2
[email protected];[email protected]

But no, this is happening:

Grupo 1:
[email protected];[email protected];[email protected]
Grupo2
[email protected];[email protected];[email protected]

Delete example 3 from emails, these groups look like this:

Grupo 1:   [email protected];[email protected];[email protected];[email protected];[email protected]
Grupo 2: [email protected];[email protected];[email protected];[email protected];[email protected]

I know my script is bad, I just don’t know how to adjust =(

  • This column is multivariate?

  • No, it is a TEXT column. Save the data in this way because it is easy to use later only using echo 'mailto:'. $emails;

  • There is more than one email in this column?

  • yes, emaisl looks like Dice in the example inside the column... [email protected];[email protected];[email protected];...etc

  • Have you tried the one REPLACE in the update? - UPDATE grupoemail SET email = REPLACE(REPLACE(email, '$email_i', ''), ';;', ';') WHERE emails LIKE '%$email_i%'"

  • If you can change the tables and it’s not too much trouble, you can do it like this => Normalize values separated by comma for new table

  • I did not know the REPLACE function in the query, I will try this way, I believe it will work =)

  • I managed to make it work using the REPLACE function, it worked perfectly, thanks to everyone who helped me.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.