简体   繁体   English

使用PHP更新具有相同ID的多个值

[英]Update multiple values with same id using php

I'm trying to change my values in database.. In my database it saved like this 我正在尝试更改数据库中的值。在数据库中,这样保存

ID                  COLOR
1                    RED
1                    BLUE
1                    GREEN

I want to update all id which is 1 in my color category 我想更新我的颜色类别中的所有ID(即1)

Now i want to update it like this 现在我想这样更新

 Color1 : RED  to BLACK
 Color2 : BLUE to YELLOW
 Color3: GREEN To WHITE

But when i click update.. The result become like this 但是当我单击更新时。结果变成这样

Color1 : WHITE 
Color2 : WHITE
Color3:  WHITE

How am i able to save the fix that? 我如何保存该修复程序?

Here's my code. 这是我的代码。

HTML FORM HTML表格

<div class='control-group'>
   <div style='margin-left: 25px; margin-top:-5px;'>
    <label style='display: inline-block !important; vertical-align: middle; float: left; font-weight: bold; margin-left:40px;'> Color Combination $x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>

   <div class='controls'>
   <select name='color1[]' class='form-control'  id='color1' onchange='getcolor()'><option value=''>$test</option>
    ";

 $statement = $db->prepare("SELECT * FROM color order by colorName ASC");
   $statement->execute();

     while($rows = $statement->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" .$rows['colorName']. "'>" . $rows['colorName'] . " - " . $rows['colorCode'] ."</option>";
        }

echo "</select>";

MY SUBMIT CODES 我的提交代码

if (isset($_POST['submit'])) {

$color1 = $_POST['color1'];
$count = count($color1);

 for ($x = 0; $x <=$count; $x++) {

    if($color2[$x] == "")
    {   
     $savecolor = $color1[$x];

$stmt = $db->prepare('UPDATE productcolor set colorName = :color WHERE productinformationID=:productinformationID');
$stmt->execute(array(':color' => $savecolor, ':productinformationID' => $prodID));
    }
}   

} 

But when i echo $savecolor = $color1[$x]; 但是当我回声$ savecolor = $ color1 [$ x]; it display the color that i chose but when it comes in saving its not.. 它显示我选择的颜色,但是保存时不显示。

You will need AND condition to specify which color should be updated. 您将需要AND条件来指定应更新的颜色。

For example: 例如:

UPDATE productcolor
SET colorName = :color
WHERE productinformationID=:productinformationID
AND colorName = :colorName

:colorName should be the color to be changed. :colorName应该是要更改的颜色。

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

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