Friday, September 27, 2013

How to store multiply check boxes in to database with PDO Connection

How to store multiply check boxes in to database with PDO Connection

I have the following problem which I can't resolve in which I've tried
everything and nothing is working. The problem is that I want to store
multiply checkboxes values into database, into one column one record
inside it. Here is the Form code which I am using:
<label for="newsletter1">PRIMARIA:</label>
<input type="checkbox" name="field021[]" value="1" /> 1
<input type="checkbox" name="field021[]" value="2" /> 2
<input type="checkbox" name="field021[]" value="3" /> 3
for the PHP I am using a class file with the following storring parameteres:
public function __construct( $data = array() ) {
if( isset( $data['field021'] ) ) $this->field021 = stripslashes(
strip_tags( $data['field021'] ) );
public function register() {
$correct = false;
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO users(field021) VALUES(:field021)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "field021", $this->field021, PDO::PARAM_STR );
$stmt->execute();
right now nothing is getting writen in the database in the cell field021 ,
what I want to do is, to be able to register there multiply checkboxes
devided by comma. If anyone can help, it will be much appreciated. Thanks

No comments:

Post a Comment