[英]PHP insert HTML form values notice array to string conversion
I have a notice when I'm try to insert form values to database: 当我尝试向数据库中插入表单值时,我有一条通知:
Array to string conversion in C:\\path\\rangking.inc.php on line 41
I've read this answer but I have a different array that showing with print_r
is: 我已经读过这个答案,但是我有一个与print_r
显示的数组:
Array ( [ia] => 6 [ik] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) [nn] => Array ( [0] => 80 [1] => 79 [2] => 79 [3] => 80 ) ) 1
any suggesting answer would be appreciate 任何建议的答案将不胜感激
form HTML : 表单HTML:
if($_POST){
include_once 'includes/rangking.inc.php';
$eks = new rangking($db);
$eks->ia = $_POST['ia'];
$eks->ik = $_POST['ik'];
$eks->nn = $_POST['nn'];
if($eks->insert2()){
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Berhasil Tambah Data!</strong> Tambah lagi atau <a href="rangking.php">lihat semua data</a>.
</div>
<?php
}
else{
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Gagal Tambah Data!</strong> Terjadi kesalahan, coba lagi.
</div>
<?php
}
}
?>
<form method="post">
<div class="form-group">
<label for="ia">Alternatif</label>
<select class="form-control" id="ia" name="ia">
<?php
$stmt3 = $pgn1->readAll();
while ($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)){
extract($row3);
echo "<option value='{$id_alternatif}'>{$nama_alternatif}</option>";
}
?>
</select>
</div>
<div class="form-group">
<?php
$stmt2 = $pgn2->readAll();
while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)){
extract($row2);
?>
<label for="ik"><?php echo $nama_kriteria; ?></label>
<input type="hidden" name="ik[]" id="ik" value=<?php echo $id_kriteria ?>>
<input type="text" class="form-control" id="nn" name="nn[]">
<?php
}
?>
</div>
<button type="submit" class="btn btn-primary">Simpan</button>
<button type="button" onclick="location.href='rangking.php'" class="btn btn-success">Kembali</button>
</form>
<?php (print_r($_POST)); ?>
code of rangking.inc.php
: rangking.inc.php
代码:
function insert2(){
$query = "insert into ".$this->table_name." values(?,?,?,'','')";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->ia);
$stmt->bindParam(2, $this->ik);
$stmt->bindParam(3, $this->nn);
if($stmt->execute()){
return true;
}else{
return false;
}
}
EDIT 编辑
mixing function code of _chris85_ and mine
: mixing function code of _chris85_ and mine
:
$query = "insert into ".$this->table_name." values(?,?,?,'','')";
foreach ($this->ik as $key => $value){
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->ia);
$stmt->bindParam(2, $value);
$stmt->bindParam(3, $this->nn[$key]);
if($stmt->execute()){
return true;
}else{
return false;
}
}
Since nn
and ik
are arrays you need to loop over them and bind each value. 由于nn
和ik
是数组,因此您需要遍历它们并绑定每个值。 Here's a rough example of how you could do that assuming nn
and ik
have a one to one relationship. 这是一个粗略的示例,假设nn
和ik
具有一对一关系,您将如何做到这一点。
$query = "insert into ".$this->table_name. " values";
error_log('IK' . print_r($this->ik, 1));
foreach($this->ik as $key => $value) {
error_log('In Loop');
$query_bits[] = '(?, ?, ?, "", "")';
$params[] = $this->ia;
$params[] = $value;
$params[] = $this->nn[$key];
}
$stmt = $this->conn->prepare($query . implode(',' $query_bits));
$stmt->execute($params);
This is a rough, untested sample of how it should work. 这是一个未经测试的粗略示例。
Don't put variables into $query_bits
or you will loose the purpose of preparing/binding. 不要将变量放入$query_bits
否则您将失去准备/绑定的目的。
This approach is making on large insert
statement with each row's data separated by a comma. 这种方法是在大型insert
语句上进行的,每行的数据用逗号分隔。
INSERT statements that use VALUES syntax can insert multiple rows. 使用VALUES语法的INSERT语句可以插入多行。 To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. 为此,请包括多个列值列表,每个列值括在括号内并用逗号分隔。
- https://dev.mysql.com/doc/refman/5.7/en/insert.html -https://dev.mysql.com/doc/refman/5.7/zh-CN/insert.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.