Hey,
I'm doing a project for school, which has a deadline set to monday next week, and I have a problem that has to be solved NOW if I will have time to complete the task before deadline. So I'm EXTREMELY greatful for your help!
I have a form with checkboxes, each representing an offer, like "Become a member" or "Recieve our newsletter". There are three checkboxes, and I have a script where I want to check if any of those were checked, and if so, declare a variable with the value 1 (one), and if it was not checked, declare a variable with the value 0 (zero), and then store all the varibles in a database.
The problem with my script, I think, is that it does not check if multiple boxes are checked, it just checks if the first box is checked, then skip the others. Because if the first box is not checked, it will store only zeros in the database.
The error i get when i run the script is:
Incorrect integer value: '' for column 'Medlem' at row 1
The column varies depending on whether i checked the first box or not. If i didn't, then the column name in the error message will be "Månadsbrev".
Here's the script:
PHP Code:
// Checkboxar - Välj erbjudande(n)
$chooseOffer = $_POST['chooseOffer'];
$k=1;
foreach($chooseOffer as $erbjudande) {
if($erbjudande == $k) {
$becomeMember = 1;
}
else {
$becomeMember = 0;
}
if($erbjudande == $k) {
$newsletter = 1;
}
else {
$newsletter = 0;
}
if($erbjudande == $k) {
$contest = 1;
}
else {
$contest = 0;
}
$k++;
var_dump($erbjudande);
}
$antalPlatser = 0;
// Definiera och kör sql-fråga
$sql = "INSERT INTO persondata (antalPlatser, Förnamn, Efternamn, Telefonnummer, Lösenord, Epost, Medlem, Månadsbrev, Tävling) VALUES ('$antalPlatser', '$fornamn', '$efternamn', '$telefonnummer', '$password', '$epost', '$becomeMember', '$newsletter', '$contest')";
mysql_query($sql, $connection) or die(mysql_error());
Here's the form:
PHP Code:
<form action="<?php $PHP_SELF;?>" method="post" id="registerForm">
Förnamn:<br />
<input type="text" name="fornamn" class="input" /><br />
Efternamn:<br />
<input type="text" name="efternamn" class="input" /><br />
Telefonnummer:<br />
<input type="text" name="telenr" class="input" /><br />
Lösenord:<br />
<input type="password" name="pass" class="input" /><br />
E-postadress:<br />
<input type="text" name="epost" class="input" /><br />
<p class="box">
<strong>Erbjudanden</strong><br /><br />
<input type="checkbox" name="chooseOffer[]" value="1" class="checkOffer" /> Ja, jag vill bli medlem<br />
<input type="checkbox" name="chooseOffer[]" value="2" class="checkOffer" />Ja, jag vill vara med på e-postlistan och få månadsbrevet<br />
<input type="checkbox" name="chooseOffer[]" value="3" class="checkOffer" />Ja, jag vill vara med i tävlingen <strong>"Vinn en resa till Glasriket - för två"</strong><br />
</p>
<input type="submit" name="register" value="Registrera dig!" class="register" />
</form>
My database-table looks like the file attached.
Thank you guys