Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1658 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Fetch data from checkoxes and store in database (Need help NOW!)

Thread title: Fetch data from checkoxes and store in database (Need help NOW!)
     
    Thread tools Search this thread Display Modes  
Prev Previous Post   Next Post Next
12-03-2009, 01:58 PM
#1
.Cyanide is offline .Cyanide
Status: Senior Member
Join date: Mar 2006
Location: Stockholm,Sweden
Expertise: Web Design, Coding
Software: Phoshop CS3
 
Posts: 757
iTrader: 4 / 100%
 

.Cyanide is an unknown quantity at this point

Send a message via MSN to .Cyanide

  Old  Fetch data from checkoxes and store in database (Need help NOW!)

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

Attached Images
File Type: jpg tabell.JPG (86.3 KB, 4 views)

Reply With Quote
     


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed