mercredi 25 mars 2015

Insert values of multiple checkboxes into MySQL database

I have a form, with two textfields and four sets of checkboxes, all which values i need to have inserted to MySQL database, with each of the textfield's/checkbox's value inserted to a specific column, thus the database table has six columns. User need to select a least one of the checkboxes in each category/name (see form), and I need some PDO/spam security as well as 'emailchecker'-security,


I'm totally new to PHP, but i got the form and the MySQL database working.


This is my form:





<form action="assets/helpers/mysql.php" method="post">




<div class="row">
<div class="col-md-6 col-s-12">
<h2>Headline 2<strong>Bumper</strong>.</h2>
<p>Lorem ipsum ...
</p>
<br>
<br>
</div>

<div class="col-md-6 col-s-12">

<h2>Hvilken <strong>Bumper</strong><br>kunne du tænke dig?</h2>



<h3>iPhone model</h3>


<div class="btn-group" data-toggle="buttons">
<button type="button" class="btn btn-primary">
<input type="checkbox" name='iPhone[]' value="iPhone 5" id="iPhone 5">iPhone 5/5S</button>

<button type="button" class="btn btn-primary">
<input type="checkbox" name='iPhone[]' value="iPhone 6" id="iPhone 6">iPhone 6</button>

<button type="button" class="btn btn-primary">
<input type="checkbox" name='iPhone[]' value="iPhone 6 Plus" id="iPhone 6 Plus">iPhone 6 Plus</button>
</div>
<br>
<br>



<h3>Bumper model</h3
><p></p>

<div class="btn-group" data-toggle="buttons" >
<button type="button" class="btn btn-primary">
<input type="checkbox" name='Bumper[]' value="Firkantet" id="Firkantet">Firkantet</button>

<button type="button" class="btn btn-primary">
<input type="checkbox" name='Bumper[]' value="Rund" id="Rund">Rund</button>
</div>


<br>
<br>
<h3>Lås</h3>
<p></p>

<div class="btn-group" data-toggle="buttons">
<button type="button" class="btn btn-primary">
<input type="checkbox" name='Laas[]' value="Flet" id="Flet">Flet</button>

<button type="button" class="btn btn-primary">
<input type="checkbox" name='Laas[]' value="Skruer" id="Skruer">Skruer</button>
</div>

<br>
<br>
<h3>Farve</h3>
<p></p>

<div class="btn-group" data-toggle="buttons">
<button type="button" class="btn btn-primary">
<input type="checkbox" name='Farve[]' value="Sort" id="Sort">Sort</button>

<button type="button" class="btn btn-primary">
<input type="checkbox" name='Farve[]' value="Sølv" id="Sølv">Sølv</button>

<button type="button" class="btn btn-primary">
<input type="checkbox" name='Farve[]' value="Grå" id="Grå">Grå</button>

<button type="button" class="btn btn-primary">
<input type="checkbox" name='Farve[]' value="Champagne" id="Champagne">Champagne</button>

</div>


</div>
</div>
<br>
<br>
<div class="row row-centered">
<div class="col-md-12 col-s-12 col-centered signup">
<div class="item">
<div class="content">
<h2>Skriv <strong>mig</strong> op!</h2>
<input type='text' name='name' placeholder="Navn" />
<br>
<br>
<input type='text' name='email' placeholder="Email" />

<br>
<br><strong><input type='submit' class="btn btn-danger" name='Submit' value='Send' /></strong>
</div>
</div>
</form>



This is mysql.php:





<?php


define('DB_NAME', 'myname');
define('DB_USER', 'myuser');
define('DB_PASSWORD', 'mypassword');
define('DB_HOST', 'myhost');


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
die('Cannot use ' . DB_NAME . ': ' . mysql_error());
}

?>






Aucun commentaire:

Enregistrer un commentaire