简体   繁体   English

php mysql一次更新一条记录中的多个字段

[英]php mysql UPDATE multiple fields in one record at once

I am trying to UPDATE every field (16 fields plus the key) in a record in a MySQL database with some new data from a form using php. 我正在尝试使用php从MySQL表单中更新一些新数据来更新MySQL数据库中记录中的每个字段(16个字段加键)。 It works fine with INSERT but when I try to change to UPDATE it won't do it. 它可以与INSERT一起正常工作,但是当我尝试更改为UPDATE时,它不会这样做。 I also feel this is a very long way to do it and there is probably a more iterative solution, I would really appreciate some help please: 我也觉得这是一个很长的路要走,可能还有一个更迭代的解决方案,请多多帮助:

<?php
require_once 'login.php';
$con=mysqli_connect($hh,$un,$pw,$db);
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
echo 'Connected successfully';

$sql = "UPDATE PiBQ_Config 
        SET (upButton, dnButton, stepCore1, stepCore2, stepCore3, stepCore4, limSwitch, waitTime, maxPosn, pidKp, pidKi, pidKd, intMax, intMin, sleepTime, progRun) =
        ('$_POST[upButton]', '$_POST[dnButton]', '$_POST[stepCore1]', '$_POST[stepCore2]', '$_POST[stepCore3]', '$_POST[stepCore4]', '$_POST[limSwitch]', '$_POST[waitTime]', '$_POST[maxPosn]', '$_POST[pidKp]', '$_POST[pidKi]', '$_POST[pidKd]', '$_POST[intMax]', '$_POST[intMin]', '$_POST[sleepTime]', '$_POST[progRun]')
        WHERE tableKey = 1";

mysqli_query($con,$sql);

echo "1 record added";
header ('location: ../settings.php');

mysql_close($con)
?>

Set each field individually for example: 分别设置每个字段,例如:

"UPDATE PiBQ_Config 
        SET upButton = '$_POST[upButton]',
       dnButton ='$_POST[upButton]', 
      stepCore1 = '$_POST[dnButton]', 
    .
    . 
    .//the rest of variables
    WHERE tableKey = 1"

This method creates a query from three parameters: the table name, an associative array which has the name of the columns as keys and another whitch has the name of the column you want to update as keys for the where statement. 此方法从三个参数创建查询:表名称,一个关联数组,该关联数组将列的名称作为键,而另一个变量将要更新的列的名称作为where语句的键。

public function update($table, $data, $where)
{
    $query='UPDATE `'.$table.'` SET ';
    foreach($data as $key => $value)
    {
        $query .= '`'.$key.'`=:'.$key.','; 
    }
    $query = substr($query, 0, -1);
    $query .= ' WHERE ';
    foreach($where as $key => $value)
    {
        $query .= '`'.$key.'`=:'.$key.','; 
    }
    $query = substr($query, 0, -1);

    $data += $where;

    $update = $this->db->prepare($query);
    $update->execute($data);
}

It will build a query and execute it. 它将建立一个查询并执行它。 You should use PDO and prepared statements for more security. 您应该使用PDO和准备好的语句来提高安全性。

Example: 例:

for example you want to update the name of a user in the database. 例如,您要更新数据库中的用户名。

$firstName = 'newFirstName';
$lastName = 'newLastName';
$id = idOfUserYouWantToUpdate;

$table = 'users';
$data = array('user_firstname'=>$firstName, 'user_lastname'=>$lastName);
$where = array('user_id'=>$id)

update($table, $data, $where);

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

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