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

时间:2017-03-11 15:25:28

标签: php arrays forms

我需要一些关于此代码的帮助。

在我的代码中,我将以下隐藏的表单字段作为数组:

代码:

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

这些隐藏的表单字段位于名为reviewe.php的页面上,该页面是从名为order.php的上一页传递的。

我正在尝试从名为finel.php的页面插入这些表单字段的值,因为表单上的操作表示。

代码:

$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"]
   );

当此代码被执行时,我收到错误:

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

我知道这个错误意味着我有隐藏的表单字段,我试图将其作为数组传递但我试图将它们作为字符串插入。

但是,我不知道如何修改代码以将变量接受为数组。

有什么建议吗?三江源。

2 个答案:

答案 0 :(得分:0)

您可以使用implode函数将数组写为字符串,方法如下:

implode(&#34;,&#34;,$ array);

然后使用explode函数在这样的数组中读回它:

爆炸(&#34;,&#34;,$ array);

答案 1 :(得分:0)

通过简单地获取数组的第一个索引,您可以获取隐藏输入的值,而它们位于数组中。

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]
     );