简体   繁体   中英

Php if statement in MySQL for a bulk update

This is my first post. on to the point

I am trying to make a bulk update in a MySQL table using a php if statement.but since i have no clue of mysql apart to connect to a database and update things 1 by 1.

So basically i am trying to use this if statement that is loooking for the word Casio in the name and avoids all prodcuts that have Bulgarian letters in the name on its own the code works,i just have 0 idea how to impliment it for MySQL would love some help to how to start.

<?php
    require("words.php");
    $product = "CASIO EF-513D-5AV MTP Chronograph";
    if (strstr($product,"CASIO",0)) {
        /*Watches for men*/
            if (strstr($product,$gs,0) || strstr($product,$edifice,0) || strstr($product,$mtp,0) || strstr($product,$mrw,0)) {
                if ($product == substr($avoid[0],0) || $product == substr($avoid[1],0) || $product == substr($avoid[2],0)) {
                    echo $product;
                    } 
                    elseif(strstr($product,$gs,0)) {
                        echo str_replace($gs,$gsW,$product);
                    }
                    elseif(strstr($product,$edifice,0)) {
                        echo str_replace($edifice,$edificeW,$product);
                    }
                    elseif(strstr($product,$mtp,0)) {
                        echo str_replace($mtp,$mtpW,$product);
                    }
                    elseif(strstr($product,$mrw,0)) {
                        echo str_replace($mrw,$mrwW,$product);
                    }
                }
            }
        /*Watches for women*/
            elseif (condition) {
                # code...
        }
        else{

        }
    ?>

all the variables that the if statement above contains

<?php 
/*Real words that we have*/
$gs = "G-SHOCK";
$casio = "CASIO";
$edifice = "EDIFICE";
$mtp = "MTP";
$mrw = "MRW"; 
/*Words that will be added*/
$gsW = "Мъжки спортен G-SHOCK";
$casioW = "часовник CASIO";
$edificeW = "Мъжки часовник EDIFICE";
$mtpW = "Мъжки часовник MTP";
$mrwW = "Мъжки часовник MRW"; 

/*Avoid words*/
$avoid = array("Мъжки","часовник","спортен")
?>

update:My idea is to target a table "product" and access the sub table "name" and which ever name has the word Casio in it,to start doing the if statement for it

Ok so far i've been searching far and wide and the only thing that i am missing is and If else statement

I've figure out how to search inside a table for a string with

$sql = "UPDATE product_description SET name = REPLACE(name, 'CASIO', 'Мъжки часовник CASIO')";

I Just don't how to to tell the code If you see $avoid don't do anything to those names but for the rest add which ever thing i specify

Make a backup of the original $product string.

<?php
$originalProduct = $product;

// Then your changes are to be made here

// Having finished modifying 
$connection = mysqli_connect("localhost", "root", "", "database", 3306);

if($connection->connect_error)
{
    // error occured while trying to connect to the database
}
else
{
    $query = $connection->query("UPDATE `tablename` SET productName = '$product' WHERE productName = '$originalProduct' LIMIT 1;");
    if($query)
    {
        // Successfully modified record
    }
    else
    {
        // error occured while trying to modify
    }
}
?>

I think i found an answer to my own question

I could just use several of

$sql = "UPDATE product_description SET name = REPLACE(name, 'EDIFICE', 'Мъжки часовник EDIFICE');";

I am just not sure if it's a good idea to use several of those Replace statements

I think you just need to limit the SELECT by using a suitable WHERE clause.

WHERE name NOT LIKE '%Мъжки%' AND name NOT LIKE '%часовник%' AND name NOT LIKE '%спортен%'

You can construct the where clause in PHP along the lines of...
$where = "name NOT LIKE '%".implode("%' AND name NOT LIKE '%",$avoid).%'"

HTH

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