mysql - How do I insert multiple value if there is only one value in form -


i need know how can insert or update value in db invoice form if value of input need save 1 time?

i have invoice form select product in every row, , have input(user_id) value need save rest of inputs

like per example, choose in invoice:

10 tomatoes   5 garlics  2 beans 

and there id (user_id, not id of productos table unique)

id=1 

here schema of table, , how save or update until now:

| cantidad   | nombre del producto |   id  |  +------------+---------------------+-------+ |     10     |   tomatoes          |     1 | |      5     |   garlics           |     0 | |      2     |   beans             |     0 | 

here need save or update:

| cantidad   | nombre del producto |   id  |  +------------+---------------------+-------+ |     10     |   tomatoes          |     1 | |      5     |   garlics           |     1 | |      2     |   beans             |     1 | 

here code:

            $conn->begintransaction();             $sql = "insert productos             (cantidad, nombreprod, id)              values ";             $insertquery = array();             $insertdata = array();             foreach ($_post['cantidad'] $i => $cantidad) {                 $insertquery[] = '(?, ?, ?)';                 $insertdata[] = $_post['cantidad'][$i];                 $insertdata[] = $_post['nombreprod'][$i];                 $insertdata[] = $_post['id'][$i];             }             if (!empty($insertquery)) {                 $sql .= implode(', ', $insertquery);                 $stmt = $conn->prepare($sql);                 $stmt->execute($insertdata);             }             $conn->commit(); 

thank you

so have single userid want insert each product record. can provide better answer if post output of print_r($_post), in foreach $post loop, keep user id static:

foreach ($_post['cantidad'] $i => $cantidad) {   $insertquery[] = '(?, ?, ?)';   $insertdata[] = $_post['cantidad'][$i];   $insertdata[] = $_post['nombreprod'][$i];   $insertdata[] = $_post['id']; //or $insertdata[] = $_post['id'][0], depending on $_post array             } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -