首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将组合值插入数据库

问将组合值插入数据库
EN

Stack Overflow用户
提问于 2017-07-09 14:10:18
回答 1查看 31关注 0票数 0

我获取total_amount值并插入到另一个表中,但我想在一个字段中插入total_amount,并用",“分隔。如何做到这一点?

​

​

我知道Mysqli是最新版本。但在这里,mysql运行正常。

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-09 14:46:45

代码语言:javascript
复制
<?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循环现在不是表行的一部分,而是首先创建包含所有值的数组,然后您可以将通过内爆创建的字符串添加到隐藏的表单字段中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44993512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档