douzhicai7148 2017-07-09 06:10
浏览 56
已采纳

将组合值插入数据库

I fetch total_amount value and insert into another table but i want to insert total_amount in one field and seperated by ",". How to do that?

enter image description here

I know Mysqli is latest version . but here mysql is working properly.

<?php
include('database/db.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$sql1="Insert into test(`total_amount`) values ('{$_POST['i']}')";
$result1=mysql_query($sql1);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Page</title>
</head>
<body>
<form name="" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<table>
<tr>
<td>Total Amount</td>
</tr>
<?php
$sql="Select * from tes1";
$result=mysql_query($sql);
while($dtset=mysql_fetch_array($result))
{
?>
<tr>
<td><input type="hidden" name="i" value="<?php echo $dtset['total_amount']; 
?>"><?php echo $dtset['total_amount']; ?></td>
</tr>
<?php } ?>
</table>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dqsw7529 2017-07-09 06:46
    关注
    <?php
    $sql="Select * from tes1";
    $result=mysql_query($sql);
    // Empty array
    $amounts = [];
    while($dtset=mysql_fetch_array($result))
    {
        // Push the amounts to the array.
        $amounts[] = $dtset['total_amount'];
    }
    // Implode will create a string from your array and seperates it with
    // the chosen character
    $combined = implode(",",$amounts);
    ?>
    <tr>
        <td>
            <input type="hidden" name="i" value="<?php echo 
            $combined; ?>">
        <?php echo $combined; ?>
        </td>
    </tr>
    

    The while loop is now not part of the table row, but instead creates the array with all values first and then you can add the string created by implode to your hidden form field.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊