根据更改列名将静态 SQL 更新更改为动态

发布于 2024-12-02 16:17:56 字数 1821 浏览 0 评论 0原文

好的,我昨晚问了一个问题,收到了很多非常好的回复。因为这是我第一次使用 StackOverflow,所以我非常高兴。

希望大家能帮忙做一个新的。希望在未来,我能够回报一些新人的帮助。

我在 php 文件中有以下代码:

  $sql = "";
  $now=date("Y-m-d h:i:s");
  $updatedRecords = $json1->{'updatedRecords'};
  foreach ($updatedRecords as $value){
     $sql = "update `acea` set ".
      "`ACEA_A1`='".$value->ACEA_A1 . "', ".
      "`ACEA_A2`='".$value->ACEA_A2 . "', ".
      "`ACEA_A3`='".$value->ACEA_A3 . "', ".
      "`ACEA_A4`='".$value->ACEA_A4 . "', ".
      "`ACEA_A5`='".$value->ACEA_A5 . "', ".
      "`ACEA_B1`='".$value->ACEA_B1 . "', ".
      "`ACEA_B2`='".$value->ACEA_B2 . "', ".
      "`ACEA_B3`='".$value->ACEA_B3 . "', ".
      "`ACEA_B4`='".$value->ACEA_B4 . "', ".
      "`ACEA_B5`='".$value->ACEA_B5 . "', ".
      "`ACEA_E1`='".$value->ACEA_E1 . "', ".
      "`ACEA_E2`='".$value->ACEA_E2 . "', ".
      "`ACEA_E3`='".$value->ACEA_E3 . "', ".
      "`ACEA_E4`='".$value->ACEA_E4 . "', ".
      "`ACEA_E5`='".$value->ACEA_E5 . "', ".
      "`ACEA_E7`='".$value->ACEA_E7 . "' ".
      "where `acea_id`=".$value->acea_id;
      if(mysql_query($sql)==FALSE){

        $errors .= mysql_error();
      }
  }

“ACEA_XX”部分与“acea”数据库表中的列相关(显然),但程序员静态设置它们。不幸的是,需要定期添加这些列,并创建与引入的新 ACEA 规范相关的新列。

结果,这段代码变得过时了。

每次添加新列时,无需进入并更新此代码,如何重新设计此代码,以便它动态更新自身以包含新列?我整个早上都在努力让它工作,我可以设置它,以便我可以动态地将实际的列名插入到更新语句中,但是,我似乎无法动态地动态获取关联的值(我认为我获取和插入列名称的方法有点复杂)。

我的实际数据库表列当前为:

acea_id ACEA_A1 ACEA_A2 ACEA_A3 ACEA_A4 ACEA_A5 ACEA_B1 ACEA_B2 ACEA_B3 ACEA_B4 ACEA_B5 ACEA_E1 ACEA_E2 ACEA_E3 ACEA_E4 ACEA_E5 ACEA_E6 ACEA_E7 ACEA_E9 oil_data_id

第一列和最后一列永远不会改变将继续是第一列和最后一列。任何新列都将添加到两者之间的某个位置,但不一定紧接在“oil_data_id”列之前。

我尝试了多种方式修改代码以正确获取值,但就是无法使其工作。

有人可以对代码进行简洁的修改来完成我想要的事情吗?我们将不胜感激。

Ok, I asked a question last night and received a number of really great responses. Since that was my first time using StackOverflow I was really pleased.

I'm hoping you guys can help with a new one. Hopefully, down the road, I'll be able to repay the favor to some NEW newbies.

I have the following code in a php file:

  $sql = "";
  $now=date("Y-m-d h:i:s");
  $updatedRecords = $json1->{'updatedRecords'};
  foreach ($updatedRecords as $value){
     $sql = "update `acea` set ".
      "`ACEA_A1`='".$value->ACEA_A1 . "', ".
      "`ACEA_A2`='".$value->ACEA_A2 . "', ".
      "`ACEA_A3`='".$value->ACEA_A3 . "', ".
      "`ACEA_A4`='".$value->ACEA_A4 . "', ".
      "`ACEA_A5`='".$value->ACEA_A5 . "', ".
      "`ACEA_B1`='".$value->ACEA_B1 . "', ".
      "`ACEA_B2`='".$value->ACEA_B2 . "', ".
      "`ACEA_B3`='".$value->ACEA_B3 . "', ".
      "`ACEA_B4`='".$value->ACEA_B4 . "', ".
      "`ACEA_B5`='".$value->ACEA_B5 . "', ".
      "`ACEA_E1`='".$value->ACEA_E1 . "', ".
      "`ACEA_E2`='".$value->ACEA_E2 . "', ".
      "`ACEA_E3`='".$value->ACEA_E3 . "', ".
      "`ACEA_E4`='".$value->ACEA_E4 . "', ".
      "`ACEA_E5`='".$value->ACEA_E5 . "', ".
      "`ACEA_E7`='".$value->ACEA_E7 . "' ".
      "where `acea_id`=".$value->acea_id;
      if(mysql_query($sql)==FALSE){

        $errors .= mysql_error();
      }
  }

The "ACEA_XX" portions relate to columns in the "acea" database table (obviously), but the programmer set them statically. Unfortunately, these columns need to be added to periodically, with new columns being created related to the new ACEA specs that are introduced.

As a result, this code becomes outdated.

Without having to go in and update this code each time I add a new column, how can I redesign this code so that it updates itself dynamically to include the new columns? I've been trying all morning to make it work, and I can set it so that I can dynamically insert the actual column names into the update statement, but, I can't seem to dynamically grab the associated values dynamically (and I think my method for grabbing and inserting the column names is a little convoluted).

My actual database table columns are currently:

acea_id ACEA_A1 ACEA_A2 ACEA_A3 ACEA_A4 ACEA_A5 ACEA_B1 ACEA_B2 ACEA_B3 ACEA_B4 ACEA_B5 ACEA_E1 ACEA_E2 ACEA_E3 ACEA_E4 ACEA_E5 ACEA_E6 ACEA_E7 ACEA_E9 oil_data_id

The first and last columns will never change and will continue to be the first and last columns. Any new columns will be added somewhere in between, but not necessarily immediately preceding the "oil_data_id" column.

I've tried revising the code numerous ways in order to properly grab the values, but just can't make it work.

Anyone have a concise modification to the code to do what I want? It would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演出会有结束 2024-12-09 16:17:56

看起来 Doug Kress 的方法会出现一些错误,所以这是我的镜头:

$errors = array();
foreach($json1->updatedRecords as $record)
{
    $fields = array();

    foreach($record as $field => $value)
    {
        if(substr($field, 0, 5) === 'ACEA_')
        {
            $fields[] = $field.' = '.mysql_real_escape_string($value);
        }
    }

    // Check if there are any fields set to be updated.
    if(isset($fields[0]))
    {
        // I'm assuming $record->acea_id is an integer. If not,
        // replace '%d' with '%s'.
        $sql = "UPDATE `acea` SET %s WHERE `acea_id` = '%d';";
        $sql = sprintf($sql, implode(',', $fields), $record->acea_id);

        if(mysql_query($sql) === false)
        {
            $errors[] = mysql_error();
        }
    }
}

Seems like Doug Kress' method spits some errors out so here is my shot:

$errors = array();
foreach($json1->updatedRecords as $record)
{
    $fields = array();

    foreach($record as $field => $value)
    {
        if(substr($field, 0, 5) === 'ACEA_')
        {
            $fields[] = $field.' = '.mysql_real_escape_string($value);
        }
    }

    // Check if there are any fields set to be updated.
    if(isset($fields[0]))
    {
        // I'm assuming $record->acea_id is an integer. If not,
        // replace '%d' with '%s'.
        $sql = "UPDATE `acea` SET %s WHERE `acea_id` = '%d';";
        $sql = sprintf($sql, implode(',', $fields), $record->acea_id);

        if(mysql_query($sql) === false)
        {
            $errors[] = mysql_error();
        }
    }
}
终陌 2024-12-09 16:17:56

首先,我强烈建议将其放入一个单独的表中(如果愿意,请将数据“横向”转换)。假设这是不可行的:

$sql = "";
$updatedRecords = $json1->{'updatedRecords'};
foreach ($updatedRecords as $values){
    $flist = array();
    $params = array();
    foreach ($values as $key => $value) {
        if (preg_match('/^ACEA_[A-Z]+\d+$/', $key)) {
            $flist[] = $key .'="%s"';
            $params[] = mysql_real_escape_string($value);
        }
    }

    $sql = "update `acea` set ". implode(', ', $flist) .
        "WHERE `acea_id`='%s'";
    $params[] = mysql_real_escape_string($value->acea_id);
    $sql = sprintf($sql, $params);

    if(mysql_query($sql)==FALSE){
        $errors .= mysql_error();
    }
}

First, I would highly recommend making that into a separate table (turning the data 'sideways', if you will). Assuming that's not feasible:

$sql = "";
$updatedRecords = $json1->{'updatedRecords'};
foreach ($updatedRecords as $values){
    $flist = array();
    $params = array();
    foreach ($values as $key => $value) {
        if (preg_match('/^ACEA_[A-Z]+\d+$/', $key)) {
            $flist[] = $key .'="%s"';
            $params[] = mysql_real_escape_string($value);
        }
    }

    $sql = "update `acea` set ". implode(', ', $flist) .
        "WHERE `acea_id`='%s'";
    $params[] = mysql_real_escape_string($value->acea_id);
    $sql = sprintf($sql, $params);

    if(mysql_query($sql)==FALSE){
        $errors .= mysql_error();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文