我是MySQL的新手,我正在尝试将我的插入和更新语句组合起来,就像我在别处看到的那样。对于以下代码,
$query = "this is not the problem";
$ora_stmt = oci_parse($conn, $query);
$result = oci_execute($ora_stmt);
$stmt1 = $mysqli1->prepare("INSERT INTO na_dslam_card (n_alias, shelf_pt_num, card_pt_num, card_pt_description)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE n_alias=?, shelf_pt_num=?, card_pt_num=?, card_pt_description=?");
while($row = oci_fetch_array($ora_stmt, OCI_ASSOC+OCI_RETURN_NULLS)) {
print $row['DSLAM'] . "," . $row['MODEL'] . "," . $row['VENDOR'] . "," . $row['BROADBAND_CIRCUIT_ID'] . "," . $row['SVC_N'] . "," . $row['PORT_ADDR_STATUS']. "," . $row['BROADBAND_CIRCUIT_ID'] . "," . $row['RACK'] . "," . $row['SHELF'] . "," . $row['SLOT'] . "," . $row['PORT'] . "," . $row['SHELF_PT_NUMBER'] . "," . $row['CARD_PT_NUMBER'] . "," . $row['CARD_PT_DESCRIPTION'] . "\n";
$stmt1->bind_param("ssss", $row['DSLAM'],$row['SHELF_PT_NUMBER'],$row['CARD_PT_NUMBER'],$row['CARD_PT_DESCRIPTION']);
if(!$stmt1->execute())
{
$tempErr = "Error setting card info: " . $stmt1->error;
printf($tempErr . "\n"); //show mysql execute error if exists
$err->logThis($tempErr);
}
} //while
我收到此错误消息:
设置卡信息时出错:没有为准备好的参数提供数据 声明
我使用了bind_parameters,所以不应该说我没有提供参数数据。我知道机架有时可能为空,但在这种情况下它不会只将它设置为空吗?我知道oracle查询有效,因为print语句显示了这个:
CNVLDAT00,TA12,Ad,196,641,In Service,196,,2,7,117,115,11L5
那么为什么没有正确提供参数?它看起来不错。
我看了insert values,我觉得我做得那么好。
我的表格如下:
na_dslam_card: 指数 n_alias shelf_pt_num card_pt_num card_pt_description
感谢您的帮助......
答案 0 :(得分:1)
SQL文本中有八个绑定占位符。我们需要提供八个值。
bind_param只提供四个值。
但是我们可以修改SQL文本以使用VALUES()
函数替换UPDATE子句中的占位符,而不是提供8个值,以引用已插入到列中的值。
INSERT INTO na_dslam_card
( n_alias
, shelf_pt_num
, card_pt_num
, card_pt_description
)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY
UPDATE n_alias = VALUES(alias)
, shelf_pt_num = VALUES(shelf_pt_num)
, card_pt_num = VALUES(card_pt_num)
, card_pt_description = VALUES(card_pt_description)
现在只有四个占位符需要值。
摘自MySQL参考手册:
在
ON DUPLICATE KEY UPDATE
子句中的赋值表达式中,您可以使用VALUES(col_name)
函数来引用INSERT
语句的INSERT ... ON DUPLICATE KEY UPDATE
部分中的列值。换句话说,VALUES(col_name)
子句中的ON DUPLICATE KEY UPDATE
指的是将插入的col_name
的值,没有发生重复键冲突。
参考文献:
https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values