PHP在重复更新不起作用

时间:2018-06-21 11:06:21

标签: php mysql on-duplicate-key

我正在使用C#,PHP和mysql使用超市应用程序。在本地和在线数据库中(具有25个以上的表)具有相同的架构。以下php脚本用于将json({app_sample.json)中的记录导入到在线数据库中,以同步本地和在线datanase。 json文件包含通过added_on值过滤last_updatedDATETIME字段的记录。 id是关键字段。 json文件包含新的id记录和旧的id记录(它们是更新的字段记录)。当我运行此php脚本插入在线数据库时,出现以下错误,并且记录没有更新或插入。

Error message

php脚本

<?php
    try
    {
        $connect = mysqli_connect("localhost", "fine", "password", "fine_dbsync"); 
        $query = '';
        $table_data = '';
        $filename = "app_sample.json";

        $data = file_get_contents($filename);
        $array = json_decode($data, true); 

        foreach($array as $set) 
        {
            if(sizeof($set['rows']) > 0) 
            {
                $query = '';
                $tblName = $set['tableName'];
                $colList = array();
                $valList = array();
                //  Get list of column names
                foreach($set['rows'][0] as $colName => $dataval) 
                {
                    $colList[] = "`".$colName."`";
                }
                $query .= "INSERT INTO `".$tblName."` ";
                $query .= "(".implode(",",$colList).") VALUES ";
                //  Go through the rows for this table.
                foreach($set['rows'] as $idx => $row) 
                {
                    $colDataA = array();
                    //  Get the data values for this row.
                    foreach($row as $colName => $colData) 
                    {
                        $colDataA[] = "'".$colData."'";
                    }
                    $valList[] = "(".implode(",",$colDataA).")";
                }
                //  Add values to the query.
                $query .= implode(",",$valList);


                //  If id column present, add ON DUPLICATE KEY UPDATE clause
                if(in_array("id", $colList)) 
                {
                    $query .= " ON DUPLICATE KEY UPDATE ";
                    $tmp = array();
                    foreach($colList as $idx => $colName) 
                    {
                        $tmp[] = $colName." = new.".$colName." ";
                    }
                    $query .= implode(",",$tmp);
                }
                echo "<p>Insert query:<pre>$query</pre></p>";
                mysqli_query($connect, $query);  
                echo "<h1>Rows appended in $tblName</h1>";
            } 
            else 
            {
                echo "<p>No rows to insert for $tblName</p>";
            }
        }
    } 

    catch(Exception $e)
    {   
        echo $e->getMessage();  
    }
?>

带有表记录的json文件的一部分

[
  {
    "tableName": "cashdrawer_open_time",
    "rows": []
  },
  {
    "tableName": "counters",
    "rows": [
      {
        "id": "2",
        "name": "B",
        "description": "SAKTHY",
        "added_on": "2018-06-21T12:49:30",
        "last_updated": "2018-02-18T12:49:40",
        "department": "1"
      },
      {
        "id": "5",
        "name": "E",
        "description": "SAKTHY2",
        "added_on": "2018-06-21T12:50:21",
        "last_updated": "2018-06-21T14:52:18",
        "department": "1"
      }
    ]
  }
]

Full Json file

Table create schema

1 个答案:

答案 0 :(得分:1)

使用new.column名称是引用插入语句中提供的值的错误方法。使用VALUE()函数代替:

<?php
    try
    {
        $connect = mysqli_connect("localhost", "fine", "password", "fine_dbsync"); 
        $query = '';
        $table_data = '';
        $filename = "app_sample.json";

        $data = file_get_contents($filename);
        $array = json_decode($data, true); 

        foreach($array as $set) 
        {
            if(sizeof($set['rows']) > 0) 
            {
                $query = '';
                $tblName = $set['tableName'];
                $colList = array();
                $valList = array();
                //  Get list of column names
                foreach($set['rows'][0] as $colName => $dataval) 
                {
                    $colList[] = "`".$colName."`";
                }
                $query .= "INSERT INTO `".$tblName."` ";
                $query .= "(".implode(",",$colList).") VALUES ";
                //  Go through the rows for this table.
                foreach($set['rows'] as $idx => $row) 
                {
                    $colDataA = array();
                    //  Get the data values for this row.
                    foreach($row as $colName => $colData) 
                    {
                        $colDataA[] = "'".$colData."'";
                    }
                    $valList[] = "(".implode(",",$colDataA).")";
                }
                //  Add values to the query.
                $query .= implode(",",$valList);


                //  If id column present, add ON DUPLICATE KEY UPDATE clause
                if(in_array("id", $colList)) 
                {
                    $query .= " ON DUPLICATE KEY UPDATE ";
                    $tmp = array();
                    foreach($colList as $idx => $colName) 
                    {
                        $tmp[] = $colName." = VALUE(".$colName.") ";    //  Changed this line to get value from current insert row data
                    }
                    $query .= implode(",",$tmp);
                }
                echo "<p>Insert query:<pre>".$query."</pre></p>";
                mysqli_query($connect, $query);  
                echo "<h1>Rows appended in  ".$tblName."</h1>";
            } 
            else 
            {
                echo "<p>No rows to insert for ".$tblName."</p>";
            }
        }
    } 

    catch(Exception $e)
    {   
        echo $e->getMessage();  
    }
?>