duangong1979 2017-03-11 15:25
浏览 30
已采纳

将数据库数组作为隐藏表单值插入?

I need some help with this code.

In my code, I have the following hidden form fields as array:

Code:

<form action='final.php' method = 'POST'>
<input type="hidden" name="employeename" value="<?php echo $employeename; ?>">
<input type="hidden" name="ttitle" value="<?php echo $ttitle; ?>">
<input type="hidden" name="sourcename[]" value="<?php echo $_POST['sourcename' . $id]; ?>">
<input type="hidden" name="sourceaddress[]" value="<?php echo $_POST['sourceaddress' . $id]; ?>">
<input type="hidden" name="income[]" value="<?php echo $_POST['income' . $id]; ?>">
<input type="hidden" name="spousename[]" value="<?php echo $_POST['spousename' . $id]; ?>">
<input type="hidden" name="spouseAddress[]" value="<?php echo $_POST['spouseAddress' . $id]; ?>">
<input type="hidden" name="spouseIncome[]" value="<?php echo $_POST['spouseIncome' . $id]; ?>">
</form>

These hidden form fields are on a page called reviewe.php passed from pervious page called order.php.

I am trying to insert the values of these form fields from a page called finel.php as the action on the form indicates.

Code:

$sql = 'INSERT INTO `myDB`.`wp_myTable` ( `employeeID`'
     . ', `sourcename`, `sourceaddress`, `income`,`spousename`,`spouseAddress`,`spouseincome` )'
     . ' VALUES ( ? , ? , ? , ? , ? , ? , ? )';

if( $sth = mysqli_prepare($conn,$sql) ) {
   mysqli_stmt_bind_param($sth,'sssssss'
      ,$last_id
      ,$_POST["sourcename"]
      ,$_POST["sourceaddress"]
      ,$_POST["income"]
      ,$_POST["spousename"]
      ,$_POST["spouseAddress"]
      ,$_POST["spouseIncome"]
   );

When this code is excuted, I get this error:

Notice: Array to string conversion in c:\xampp\folder\final.php

I know this error means that I have hidden form fields I am trying to pass as an array but I am trying to insert them as string.

However, I don't know how to modify the code to accept the variables as array.

Any advice ? Thankyou.

  • 写回答

2条回答 默认 最新

  • dongtaijue1578 2017-03-11 15:33
    关注

    You can get the value of the hidden inputs, while they are in an array, by simply getting the first index of the array.

    if( $sth = mysqli_prepare($conn,$sql) ) {
         mysqli_stmt_bind_param($sth,'sssssss'
           ,$last_id
           ,$_POST["sourcename"][0]
           ,$_POST["sourceaddress"][0]
           ,$_POST["income"][0]
           ,$_POST["spousename"][0]
           ,$_POST["spouseAddress"][0]
           ,$_POST["spouseIncome"][0]
         );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?