将数组隐藏表单值插入数据库的问题

时间:2017-02-24 15:46:59

标签: php arrays

我们在review.php页面上隐藏了表单字段,我们将这些隐藏的表单字段作为数组传递给名为final.php的处理页面。

<?php
error_reporting(E_ALL);
echo "DEBUG POST DATA: <pre>".print_r($_POST, 1)."</pre>";
if(isset($_POST['employeename']))
$employeename = $_POST['employeename'];
if(isset($_POST['email']))
$email = $_POST['email'];
if(isset($_POST['ttitle']))
$ttitle = $_POST['ttitle'];
$rowIDs = $_POST['rowIDs'];
$row2IDs = $_POST['row2IDs'];

echo $employeename .'<br>';
echo $ttitle .'<br> <hr width=400 align=left>';


$rowIDs = $_POST['rowIDs'];

foreach ($rowIDs as $id) {
    $sourcename = $_POST['sourcename' . $id];
    $sourceaddress = $_POST['sourceaddress' . $id];
    $income = $_POST['income' . $id];

    echo 'Name:     '. $sourcename . '<br />';
    echo 'Address:   '. $sourceaddress . '<br />';
    echo 'Income:   '. $income . '<br /><br>';
}

foreach ($row2IDs as $id) {
    $spousename = $_POST['spousename' . $id];
    $spouseAddress = $_POST['spouseAddress' . $id];
    $spouseIncome = $_POST['spouseIncome' . $id];

    echo 'Name:     '. $spousename . '<br />';
    echo 'Address:   '. $spouseAddress . '<br />';
    echo 'spouseIncome:   '. $spouseIncome . '<br /><br>';
     echo 'Your email: '. $email . '<br /><br>';
 }
?>

<body>
<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 $sourcename; ?>">
<input type="hidden" name="sourceaddress[]" value="<?php echo $sourceaddress; ?>">
<input type="hidden" name="income[]" value="<?php echo $income; ?>">
<input type="hidden" name="spousename[]" value="<?php echo $spousename; ?>">
<input type="hidden" name="spouseAddress[]" value="<?php echo $spouseAddress; ?>">
<input type="hidden" name="spouseIncome[]" value="<?php echo $spouseIncome; ?>">
<input type="submit" value="submit" />
</form>

当我运行代码时,我得到: 注意:第70行的C:\ xampp \ htdocs \ folder \ forms \ final.php中的数组到字符串转换

错误指向此行:

if( mysqli_stmt_execute($sth) ) {...

是以下插入语句的一部分:

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

我相信我收到此错误,因为review.php上的隐藏表单字段是一个数组,但我尝试插入数据库的变量是否。

有谁知道如何将变量转换为数组?

我的意思是那些隐藏的表单字段被指定为array []?

的变量

0 个答案:

没有答案