-3

我有这个显示名称文本框和产品价格的代码

$que = "SELECT * FROM additionals";
$res = mysql_query($que, $con);
   if(mysql_num_rows($res)>0){
     while($row = mysql_fetch_array($res)){ ?>
<tr>
<td><?php echo $row['name'] ?> :</td><td><input type="text" name="
<?php echo $row['name']; ?>"  /></td><td>* <?php echo $row['price'];?></td>
</tr>

现在我需要存储并添加它们的所有值示例 textbox1=1 textbox2=2 $result=3 我需要动态存储它们并且它们添加它们任何人都可以帮助我

4

3 回答 3

1

我想你可能想要这个

$que = "SELECT * FROM additionals";
$res = mysql_query($que, $con);
   if(mysql_num_rows($res)>0){
while($row = mysql_fetch_array($res)){ ?>
<tr>
<td><?php echo $row['name'] ?> :</td><td><input type="text" name="
<?php echo $row['name']; ?>" value="<?php echo $row['price'];?" /></td>
</tr>
<?php }?>
于 2013-02-21T12:35:09.810 回答
0

如果您想添加每行价格的值,那么您只需要一个 sub_total 变量,如下所示:

<?php
$que = "SELECT * FROM additionals";
$res = mysql_query($que, $con);
$output = '<table>';
$sub_total = 0;

if(mysql_num_rows($res) > 0){
    while($row = mysql_fetch_array($res)){
        $sub_total += $row['price'];

        $output .= '<tr>';
        $output .= '<td>'.$row['name'].'</td><td><input type="text" name="';
        $output .= $row['name'].'"  /></td><td>* '.$row['price'].'</td>';
        $output .= '</tr>';
    }
}

$output .= '</table>';
echo $output;
?>

我不确定你想在哪里写那些 textbox1、2、3 等。出来,请在此答案的评论部分注明,我会根据您的需要更新答案。

于 2013-02-21T12:33:11.913 回答
0

您可以尝试以下方法:

<?php echo $row['name']; ?> : <input type="text" name="data[<?php echo $row['id']; ?>]" value="<?php echo $row['price'];?>"  />

然后进行后期处理

$data=$_POST['data'];
while(list($key,$value)=each($data))
{
    $sql="UPDATE additionals SET price='".mysql_escape_string($value)."' WHERE id=".intval($key);
    mysql_query($sql, $con);
}

这假设您的表索引是“id”并且它是一个整数。使用它作为输入的一部分允许您访问索引和值作为一对。

于 2013-02-21T12:36:02.503 回答