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

报告相同问题?