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,471
There are 993 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!)
Reply    
    Thread tools Search this thread Display Modes  
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
12-03-2009, 06:27 PM
#2
Jeff Andersen is offline Jeff Andersen
Status: Superstar
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 4,449
iTrader: 10 / 100%
 

Jeff Andersen is on a distinguished road

  Old

Could you not just have individually named checkboxes for Become Member, Newsletter etc... and then in the PHP forget the Foreach and just intval() the checkbox value? Or does that not work with what you're after.

Reply With Quote
12-03-2009, 07:14 PM
#3
.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

Originally Posted by Jeff Andersen View Post
Could you not just have individually named checkboxes for Become Member, Newsletter etc... and then in the PHP forget the Foreach and just intval() the checkbox value? Or does that not work with what you're after.
I solved the problem by giving them different names. Thanks for the help!

Reply With Quote
Reply    


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

  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