mysql查询错误-没有为prepared语句中的参数提供数据

z0qdvdin  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(342)

我对mysql还比较陌生,我正在尝试将insert和update语句结合起来,就像我在其他地方看到的那样。对于以下代码,

$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\u参数,所以不应该说我没有提供参数数据。我知道rack有时可以是空的,但是在这种情况下它不就可以设置为空吗?我知道oracle查询是有效的,因为print语句显示:
cnvldat00,ta12,ad,196641,在役,196,,2,,7117115,11l5
那么,为什么没有正确提供参数呢?我觉得还可以。
我看了插入值,我想我做得很好。
我的table是这样的:
nau dslam\u card:索引n\u别名shelf\u pt\u num card\u pt\u num card\u pt\u描述
谢谢你的帮助。。。

q5iwbnjs

q5iwbnjs1#

sql文本中有八个绑定占位符。我们需要提供八个值。
bind_参数仅提供四个值。
但是,我们可以修改sql文本,将update子句中的占位符替换为 VALUES() 函数,以引用本应插入到列中的值。

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

相关问题