如何将字段(输入类型=数字)与复选框

时间:2017-09-27 14:08:55

标签: php mysql forms checkbox foreach

我有什么

我有这张桌子:TABLE EXAMPLE 其中的字段由mysql数据库填充。表TABELLA的名称

HTML

<form action='inserisci.php' method='post'
   <input type='checkbox' name='selected[]' value='{$row['id']}'/>
   <input type='number' min='1' max='99' autocomplete='off' name='quantita[]' value='".substr($row['quantita'],3)."'/>
<input style="width:100px;" type="submit" value="Inserisci"/>

PHP INCOMPLETE但没有任何quantita结果

inserisci.php

if(!empty($_POST['selected'])) { //Se ci sono spunte
    foreach($_POST['selected'] as $check) {
        $query = $pdo->prepare("INSERT INTO tabella1 (nome, cognome, prodotto) VALUES ('$nome','$cognome', (SELECT prodotto FROM tabella WHERE ID=$check))");
        $query->execute();

我需要什么

我需要将选定的[](CHECKBOX)值与quantita []结合使用,并在TABELLA1中插入所有值。

我还希望TABELLA的值(quantita)显示在字段中...以便可以修改它们(如果需要)并与其他值一起包含在数据库中

FOR EXAMPLE(with quantita):

("INSERT INTO tabella1 (nome, cognome, prodotto, quantita) VALUES ('$nome','$cognome', (SELECT prodotto FROM tabella WHERE ID=$check), '$quantita')") 

我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我解决了!

$count = 0;
foreach ($pdo->query("SELECT * FROM tabella") as $row) {

    <input type='checkbox' name='seleziona[{$count}]' value='{$row['id']}'/><br /></td><td>
    <input type='number' min='1' max='99' autocomplete='off' name='quantita[{$count}]' id='{$row['id']}' value='".substr($row['quantita'],3)."'/>
            $count++;
}

inserisci.php

foreach($_POST['seleziona'] as $k=>$v){
        $query = $pdo->prepare("INSERT INTO tabella1 (nome, cognome, prodotto, quantita) VALUES ('$nome','$cognome', (SELECT prodotto FROM tabella WHERE ID=$v), '{$_POST['quantita'][$k]}')");
        $query->execute();
    }