问题
I have one table called Master
and it has four columns called id
, A
, B
and 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'";
回答1:
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);
?>
回答2:
You can use if statement:
$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'];
}}
if($column_name == 'A'){
UPDATE Master SET A='$A'+20 WHERE user_id='$id'";
} elseif($column_name == 'B'){
UPDATE Master SET B ='$B'+30 WHERE user_id='$id'";
}elseif($column_name == 'C'){
UPDATE Master SET C ='$C'+50 WHERE user_id='$id'";
}
}
来源:https://stackoverflow.com/questions/41519127/how-to-update-specific-single-column-with-multiple-columns-in-an-update-query-us