php 重复更新不起作用
php ON DUPLICATE UPDATE does not work
我正在使用 c#、php 和 mysql 开发超市应用程序。在本地和在线有相同模式的数据库(超过 25 tables)。以下 php 脚本用于将记录从 json(app_sample.json
) 导入在线数据库,作为本地和在线数据库的同步。 json 文件包含按 DATETIME
值过滤 added_on
和 last_updated
字段的记录。 id
是关键字段。 json 文件包含新的id记录和旧的id记录(这是更新的字段记录)。当我 运行 这个 php 脚本插入在线数据库时,我得到以下错误并且记录没有更新或插入。
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 文件有 table 条记录
[
{
"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"
}
]
}
]
使用 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();
}
?>
我正在使用 c#、php 和 mysql 开发超市应用程序。在本地和在线有相同模式的数据库(超过 25 tables)。以下 php 脚本用于将记录从 json(app_sample.json
) 导入在线数据库,作为本地和在线数据库的同步。 json 文件包含按 DATETIME
值过滤 added_on
和 last_updated
字段的记录。 id
是关键字段。 json 文件包含新的id记录和旧的id记录(这是更新的字段记录)。当我 运行 这个 php 脚本插入在线数据库时,我得到以下错误并且记录没有更新或插入。
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 文件有 table 条记录
[
{
"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"
}
]
}
]
使用 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();
}
?>