php - Query Checklist and post multiple results to table in mysql -
hello guys im trying post several checklist values single table in database takes 1 single value table...
im doing this:
the form:
<?php foreach($users $user): ?> <input type="checkbox" name="tipoinsertos" value="<?= $user['tipoinsertom']; ?>"> <?= $user['tipoinsertom']; ?> <?php endforeach; ?>
and post form:
$data = array('tipoinsertos' => $_post['tipoinsertos']); try { $dbh = new pdo("mysql:host=$servername;dbname=$dbname", $username, $password); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); $query = "insert barrasinternas ( tipoinsertos ) values (:tipoinsertos )"; $sth = $dbh->prepare($query); $sth->execute($data); echo "¡añadida exitosamente!"; } catch(pdoexception $e) { echo $sql . "<br>" . $e->getmessage(); } $dbh = null; ?>
check prints:
so checked 3 values 1 in table.
firstly, use array input name tipoinsertos[]
<input type="checkbox" name="tipoinsertos[]" ...
then don't show how you're getting $data
need iterating , executing query somehow in loop. either building 1 big query of values, or executing statement 1 value on , over. here's possible solution:
<?php $users = [ ['tipoinsertom' => 'jonatan'], ['tipoinsertom' => 'jeff'], ['tipoinsertom' => 'joe'], ]; ?> <form method="post"> <?php foreach($users $user): ?> <input type="checkbox" name="tipoinsertos[]" value="<?= $user['tipoinsertom']; ?>"> <?= $user['tipoinsertom']; ?> <?php endforeach; ?> <input type="submit" value="submit"/> </form> <?php // returns intance of pdo // https://github.com/jpuck/qdbp $dbh = require __dir__.'/tipoin_dxa05i_a.pdo.php'; if( !empty( $_post['tipoinsertos'] ) ){ // prepare query once $query = "insert barrasinternas (tipoinsertos ) values (:tipoinsertos )"; $sth = $dbh->prepare($query); // iterate through list foreach($_post['tipoinsertos'] $tipoinsertos){ $sth->execute(['tipoinsertos' => $tipoinsertos]); } // success echo "¡añadida exitosamente!"; $dbh = null; }
Comments
Post a Comment