Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将组合值插入数据库_Php_Mysql - Fatal编程技术网

Php 将组合值插入数据库

Php 将组合值插入数据库,php,mysql,Php,Mysql,我获取total_amount值并插入到另一个表中,但我想在一个字段中插入total_amount,并用“,”分隔。怎么做 我知道Mysqli是最新版本。但这里mysql工作正常 <?php include('database/db.php'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $sql1="Insert into test(`total_amount`) values ('{$_POST['i']}')"; $result1=my

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

我知道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>


我认为您应该使用PDO,mysqli是一种旧方法,mysql是不推荐使用的。使用准备好的语句,您的代码对SQL注入和XSS攻击是开放的。您能说得更清楚些吗?您的意思是:我想在一个字段中插入总金额,并用“,”分隔?您想在表或表单的一列中插入多个值,如4429.61155.2吗?是的,我想在一个字段中同时插入多个值。@Jonshon:不要在一个字段中插入多个逗号分隔的值。这是一种坏习惯。也许还有另一种解决方案,但这需要你向我们解释你试图实现的目标。
<?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>