php - Entering checkbox values correctly in database -
i have simplified next illustration illustrate question.
i opinions logged-in users using checkboxes. instance, dynamic list displayed listing various fruits, 2 checkboxes next each kind of fruit, meaning "yes, this" , "no, don't this". user check either checkbox, or don't check 1 of checkboxes @ all. this:
[] [] apple
[] [] pear
[] [] orange
[ submit ]
it needs submitted mysql database, next structure. may not easiest setup, unfortunately can work @ moment. (opinion beingness numeric value, representing "yes" or "no")
id | userid | fruit | opinion
how go making this?
based on comments.
let's field opinion tinyint, 0=no , 1=yes.
you dynamically load 'fruits', don't know if it's array or if info comes out of database. might need alter code that, presume it's array. create form fruits , alternative yes , no
<?php $fruits = array( 'apple', 'pear', 'orange' ); echo '<form ...>'; foreach( $fruits $fruit ) { echo '<p>do ' . $fruit . '</p>'; echo '<input type="radio" name="fruits[' . $fruit . ']" value="0" /> no '; echo '<input type="radio" name="fruits[' . $fruit . ']" value="1" /> yes '; } echo '</form>'; ?> you'll need add together more code create functional.
next, we'll need process form.
<?php $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world'); if($_server['request_method'] == 'post' ) { foreach( $_post['fruits'] $fruit => $opinion) { $stmt = $mysqli->prepare("insert tablename (userid, fruit, opinion) values (?, ?, ?)"); $stmt->bind_param('isi', $userid, $fruit, $opinion); $stmt->execute(); $stmt->close(); } } ?> i presume know how phone call info database display it. sentiment can utilize like:
<?php $opinion = ( $row['opinion'] == 1 ) 'yes' : 'no'; echo 'user: ' . $row['userid'] . '<br />'; echo 'fruit: ' . $row['fruit'] . '<br />'; echo 'opinion: ' . $opinion . '<br />'; ?> the above chunks of code aren't complete, help on way. luck!
php mysql arrays checkbox
No comments:
Post a Comment