[英]Insert multiple rows from array
I need help to insert data into mysql.我需要帮助将数据插入 mysql。 I am unable to find the problem in the code below why data is not inserted into database.我在下面的代码中找不到数据未插入数据库的问题。 When I am submitting the form this message comes : Notice: Undefined variable: asd in...
当我提交表单时,此消息出现: Notice: Undefined variable: asd in...
<?php
$q = mysql_query("select `cus_id`, `date`, `mobile`,
least(`t1`, `t2`, `t3`, `t4`) as min
from `table1`");
while($r = mysql_fetch_assoc($q)){
$asd = array(`cus_id` =>$r['cus_id'],
`mobile` =>$r['mobile'],
`date` =>$r['date'],
`credit` =>$r['min']);
}
$sql = array();
foreach((array)$asd as $row){
$sql[] = '("'.mysql_real_escape_string($row['cus_id']).'",
'.$row['mobile'].',
'.$row['date'].',
'.$row['min_total'].'
)';
}
$stmt = mysql_query('INSERT INTO `single` (`cus_id`, `mobile`, `date`, `credit`)
VALUES '.implode(',', $sql));
if(!$stmt){
echo "error". mysql_error();
}else{
$_SESSION['s']="Payment successfully saved";
header('location:final.php');
}
?>
Thanks for any help.谢谢你的帮助。
If I convert the above query into mysqli, then will the code be below:如果我将上面的查询转换为 mysqli,那么代码如下:
<?php
$link = mysqli_connect('localhost', 'root', '', 'mumbai');
$q = "select `cus_id`, `date`, `mobile`,
least(`t1`, `t2`, `t3`, `t4`) as min
from `table1`";
$asd = array();
$result = mysqli_query($link, $q);
while($r = mysqli_fetch_assoc($result)){
$asd = array(`cus_id` =>$r['cus_id'],
`mobile` =>$r['mobile'],
`date` =>$r['date'],
`credit` =>$r['min']);
}
$sql = array();
foreach((array)$asd as $row){
$sql[] = '("'.mysqli_real_escape_string($row['cus_id']).'",
'.$row['mobile'].',
'.$row['date'].',
'.$row['min_total'].'
)';
}
$stmt = mysqli_prepare($link, "INSERT INTO single
(`cus_id`, `mobile`, `date`, `time`, `credit`) VALUES (?,?,?,?,?)");
mysqli_stmt_bind_param($stmt, 'isssi', implode(',', $sql));
mysqli_stmt_execute($stmt);
if(!$stmt){
echo "error". mysqli_error($link);
}else{
$_SESSION['s']="Payment successfully saved";
header('location:final.php');
}
?>
before your while loop add an $asd
variable.在你的 while 循环之前添加一个$asd
变量。
$asd = array();
while($r = mysql_fetch_assoc($q)){
....
then in your foreach you can remove the (array)
然后在您的 foreach 中,您可以删除(array)
foreach($asd as $row){
....
no more Notice: Undefined variable: asd in...
不再Notice: Undefined variable: asd in...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.