将组合值插入数据库

将组合值插入数据库

问题描述:

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>

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