如何使用php在更新查询中使用多个列更新特定的单个列? [英] How to update specific single column with multiple columns in an update query using php?

查看:64
本文介绍了如何使用php在更新查询中使用多个列更新特定的单个列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Master的表,它有四个名为idABC的列.

I have one table called Master and it has four columns called id, A, B and C.

更新表时,需要修改用户选择的特定单列.例如,如果用户选择了列A,则数据库中将仅更新A记录.如果用户选择C,则仅C个记录将被更新.

When I update the table, I need to modify the specific single column that a user selected. For example, if a user selected column A then only A records will get updated in the database. If the user selects C then only C records will updated.

下面是我尝试的代码,但它正在更新所有列.您能帮我吗?

Below is the code I tried, but it is updating all columns. Would you help me in this?

$column_name=$row['column_name'];//A,B,C
if (isset($result->num_rows) > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $A=$row['A'];
        $B=$row['B'];
        $C=$row['C'];
      }}

 UPDATE Master SET $column_name='$A'+20, $column_name='$B'+30, $column_name='$C'+50 WHERE user_id='$id'";

推荐答案

尽管您的要求对我来说还不是很清楚,所以我认为 将帮助您满足您的要求.这是一个动态的解决方案.如果不是,您可以澄清您的要求,我将尽力解决它.感谢您的询问.

Although your requirement is not fully clear to me so then i think it will be helped you to meet your requirement. It is a dynamic solution. If it's not you can clarify your requirement i will must be try my best to resolve it. Thanks for asking.

* table schema is stakcoverflow
* table name is master_tbl
* fields name 
id | a | b | c


<?php 
$myHost = "localhost"; // use your real host name ex. myDomainName.com
$myUserName = "root";   // use your real login user name ex. myUserName
$myPassword = "";   // use your real login password ex. myPassword
$myDataBaseName = "stakcoverflow"; // use your real database name ex. myDataBaseName

$con = mysqli_connect( "$myHost", "$myUserName", "$myPassword", "$myDataBaseName" );

if( !$con ) // == null if creation of connection object failed
{ 
    // report the error to the user, then exit program
    die("connection object not created: ".mysqli_error($con));
}

if( mysqli_connect_errno() )  // returns false if no error occurred
{ 
    // report the error to the user, then exit program
    die("Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());
} 

$sql="SELECT a,b,c,id FROM master_tbl ORDER BY id";
$affectedrowsno  = 0;
if ($result=mysqli_query($con,$sql))
  {
    // Get field information for all fields
    while ($fieldinfo=mysqli_fetch_field($result))
    {
        $column_name = $fieldinfo->name;
        // Get field information for all fields

        while ($row=mysqli_fetch_assoc($result))
        {
            $a = $row['a'];
            $b = $row['b'];
            $c = $row['c'];
            $id = $row['id'];
            $sql = "UPDATE master_tbl SET $column_name=$a+20, $column_name=$b+50, $column_name=$c+80 WHERE id=$id";
            mysqli_query($con,$sql);

            $affectedrowsno += count(mysqli_affected_rows($con));
        }

   }
   echo "Affected rows = " . $affectedrowsno;
   // Free result set
    mysqli_free_result($result);

}

mysqli_close($con);

?>

这篇关于如何使用php在更新查询中使用多个列更新特定的单个列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆