繁体   English   中英

PHP if语句在MySQL中进行批量更新

[英]Php if statement in MySQL for a bulk update

这是我的第一篇文章。 指向重点

我正在尝试使用php if语句在MySQL表中进行批量更新。但是由于我没有mysql的线索,无法连接到数据库并按1方式更新事物。

所以基本上我正在尝试使用这个if语句来寻找名称中的Casio单词,并避免所有带有名称中带有保加利亚字母的产品本身都能正常工作,我只是有0个想法如何在MySQL中隐含它喜欢一些有关如何开始的帮助。

<?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{

        }
    ?>

以上if语句包含的所有变量

<?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("Мъжки","часовник","спортен")
?>

更新:我的想法是针对表“产品”并访问子表“名称”,并且在其中有单词Casio的名称,开始对其执行if语句

好的,到目前为止,我一直在广泛搜索,而我唯一缺少的是if语句

我想出了如何在表中搜索字符串

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

我只是不告诉代码如果您看到$avoid不要对这些名称做任何事情,但其余的请添加我指定的任何内容

备份原始的$ product字符串。

<?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
    }
}
?>

我想我找到了自己问题的答案

我可以用几个

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

我只是不确定使用其中一些替换语句是否是一个好主意

我认为您只需要使用合适的WHERE子句来限制SELECT

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

您可以按照以下方式在PHP中构造where子句:
$where = "name NOT LIKE '%".implode("%' AND name NOT LIKE '%",$avoid).%'"

HTH

暂无
暂无

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

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