Php 是否将数据库数组作为隐藏表单值插入?
我需要一些关于这个代码的帮助 在我的代码中,我有以下隐藏的表单字段作为数组: 代码:Php 是否将数据库数组作为隐藏表单值插入?,php,arrays,forms,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;
<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>
$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
当执行此代码时,我得到以下错误:
<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>
$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
我知道这个错误意味着我有隐藏的表单字段,我试图以数组的形式传递,但我试图以字符串的形式插入它们。
但是,我不知道如何修改代码以接受变量作为数组。
有什么建议吗?谢谢。您可以使用函数将数组写入字符串,方法如下:
内爆(“,”,$array)
然后使用函数将其读回数组,如下所示:
分解(“,”,$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]
);
为什么要使用数组输入名称
name=“spoosename[]”
看起来这些都是单个值,那么为什么不干脆name=“spoosename”