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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号