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条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法