将组合值插入数据库

时间:2017-07-09 06:10:18

标签: php mysql

我获取total_amount值并插入另一个表但我想在一个字段中插入total_amount并用“,”分隔。怎么做?

enter image description here

我知道Mysqli是最新版本。但是这里mysql工作正常。

<?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 个答案:

答案 0 :(得分:0)

<?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>

while循环现在不是表行的一部分,而是先创建包含所有值的数组,然后将implode创建的字符串添加到隐藏的表单字段中。