dtzjvj3915 2013-03-05 21:31
浏览 13
已采纳

too long


I am having an issue with a form. I have a database, and with a form I get all the data from the clients with the php function foreach.

On the action page (invoice.php), I can display all the selected items, but every time I put the quantity for each items, it only displays the last text box I filled.

I don't know how to explain it any more, so here's my code:

form.php :

    <input type="text" name="txtbox[<?php echo $input->id_article; ?>]" value="<?php echo $input->id_article; ?>" placeholder="1" style="text-align: center; width:30px;"></input>

        <input
            type="checkbox" 
            name="checked_articles[]"
            value="<?php echo $input->id_article; ?>">
        <?php echo "(".
                        ($input->ref_article).")".
                        ($input->nom_article)." (".
                        ($input->prix_article)." €)"; ?><br>
  <?php endforeach; ?>


invoice.php (header with the mysql data queries):

if(isset($_POST['checked_articles'])){
foreach($_POST['checked_articles'] as $chkbx){
$sql_articles = 'SELECT * FROM articles WHERE id_article="'.$chkbx.'"';
$req_articles = mysql_query($sql_articles) or die('Erreur SQL !<br>'.$sql_articles.'<br>'.mysql_error());
$data_articles[] = mysql_fetch_assoc($req_articles);
}}


$textbox = $_POST['txtbox'][$chkbx];


invoice.php (where the datas are displayed):

<?php foreach ($data_articles as $input) : ?>
    <tr>
        <td><?php echo $input['ref_article']; ?></td>
        <td><?php echo $input['nom_article']; ?></td>
        <td><?php echo $textbox; ?></td> <!-- Where I want the quantity to be displayed -->
        <td><?php echo $input['prix_article']; ?> €</td>
        <td><?php echo $input['prix_article']*$textbox; ?> €</td>
    </tr>
    <?php endforeach; ?>




I want to display, for each selected items, the quantity the users put on the text box.

Any help would be highly apprecied !
Thank you!

  • 写回答

1条回答 默认 最新

  • douhe6255 2013-03-05 22:11
    关注

    From the looks of your code, $textbox is an array filled with ids, like this:

    Array
    (
        [txtbox] => Array
            (
                [1] => 1
                [2] => 1
                [3] => 1
            )
    
    )
    

    In you foreach you need to reference the value with the article id, like this:

    <td><?php echo $_POST['txtbox'][$input['id_article']]; ?></td>
    

    Also, you should look into preventing sql injection. The id in you sql is passed directly from the post.

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。