0


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']; ?> €&lt;/td>
        <td><?php echo $input['prix_article']*$textbox; ?> €&lt;/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!

4

1 回答 1

1

从您的代码的外观来看,$textbox 是一个充满 id 的数组,如下所示:

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

)

在您的 foreach 中,您需要使用文章 ID 引用该值,如下所示:

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

此外,您应该考虑防止 sql 注入。你 sql 中的 id 是直接从帖子传递的。

希望能帮助到你。

于 2013-03-05T22:11:56.730 回答