我需要帮助才能将数据插入mysql。我无法在下面的代码中找到为什么数据没有插入数据库的问题。当我提交表单时,会显示以下消息: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');
}
?>
感谢您的帮助。
答案 0 :(得分:1)
如果我将上述查询转换为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');
}
?>
答案 1 :(得分:0)
在while循环之前添加$asd
变量。
$asd = array();
while($r = mysql_fetch_assoc($q)){
....
然后在您的foreach中,您可以删除(array)
foreach($asd as $row){
....
不再Notice: Undefined variable: asd in...