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.
我试图使用php if语句在MySQL表中进行批量更新。但是因为我不知道mysql除了连接到数据库并逐步更新内容。
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.
所以基本上我正在尝试使用这个if语句,在名称中为卡西欧这个词而闲聊,并避免所有在名称中都有保加利亚字母的产品代码工作,我只想知道如何为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{
}
?>
all the variables that the if statement above contains
上面的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("Мъжки","часовник","спортен")
?>
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
更新:我的想法是定位一个表“product”并访问子表“name”,并且其名称中包含单词Casio,以开始为它执行if语句
Ok so far i've been searching far and wide and the only thing that i am missing is and If else statement
好到目前为止,我一直在寻找广泛的,我唯一缺少的是和If else声明
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
我只是不知道如何告诉代码如果你看到$ avoid不对这些名称做任何事情,但其余的添加我指定的任何东西
3 个解决方案
#1
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).%'"
我认为您只需要使用合适的WHERE子句来限制SELECT。名称不喜欢'%Мъжки%'和名称不喜欢'%часовник%'和名称不喜欢'%спортен%'您可以在PHP中构建where子句... $ where =“name NOT LIKE” %“。implode(”%'和名称不喜欢'%',$ avoid)。%'“
HTH
#2
Make a backup of the original $product string.
备份原始的$ 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
}
}
?>
#3
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
我只是不确定使用其中几个替换语句是否是个好主意
#1
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).%'"
我认为您只需要使用合适的WHERE子句来限制SELECT。名称不喜欢'%Мъжки%'和名称不喜欢'%часовник%'和名称不喜欢'%спортен%'您可以在PHP中构建where子句... $ where =“name NOT LIKE” %“。implode(”%'和名称不喜欢'%',$ avoid)。%'“
HTH
#2
Make a backup of the original $product string.
备份原始的$ 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
}
}
?>
#3
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
我只是不确定使用其中几个替换语句是否是个好主意