我的UPDATE
查询正在更新jobResponsibilities
中的table_2
字段,只需插入jobResponsibilities = c00017
,但我想插入所有值,例如jobResponsibilities = C00001,C00302,C00303,C00287,C00286,C00285,C00017
,而不仅仅是obResponsibilities = C00017
}}
在一个记录中。
我的代码如下:
function mySQLSafe( $value, $quote = "'"){
// strip quotes if already in
$value = get_magic_quotes_gpc() ? stripslashes($value) : $value;
$value = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($value) : mysql_escape_string($value);
// Stripslashes
if (get_magic_quotes_gpc()){
$value = stripslashes($value);
}
// Quote value
if (version_compare(phpversion(), "4.3.0") == "-1") {
$value = mysql_escape_string($value);
} else{
$value = mysql_real_escape_string($value);
}
$value = $quote . $value . $quote;
return $value;
}
$comcode = "SELECT jobCode, compCode
FROM first_table";
$comcode_RS = mysql_query($comcode, $timespace) or die (mysql_error());
while($row_comcode_RS = mysql_fetch_assoc($comcode_RS))
{
$jobCode = $row_comcode_RS['jobCode']; // more than one record
$compCode = $row_comcode_RS['compCode']; // more than one record
echo $updateSQL = "UPDATE second_table SET jobResponsibilities=".mySQLSafe($compCode)." WHERE jobCode=".mySQLSafe($jobCode)."";
$Result1 = mysql_query($updateSQL, $timespace) or die(mysql_error());
}
答案 0 :(得分:0)
使用CONCAT
将新值附加到列的末尾。
$updateSQL = "UPDATE second_table
SET jobResponsibilities = IF(jobResponsibilities = '', " .
mSQLSafe($compCode) . ",
CONCAT(jobResponsibilities, ',', " . mSQLSafe($compCode) . "))
WHERE jobCode=".mySQLSafe($jobCode);
然而,这是糟糕的架构设计。具有逗号分隔列表的列使许多数据库操作变得困难。您应该使用单独的表来处理工作职责,并为jobCode
和compCode
的每个组合使用行。