Help Inserting multiple Values into a Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eatspinach
Forum Newbie
Posts: 20
Joined: Mon Apr 20, 2009 8:50 am

Help Inserting multiple Values into a Database

Post by eatspinach »

Hi

I have a web page that has 4 input fields with the option of adding 3 more, so the number of information to be entered can vary. I wrote a function that looks after the insert, but instead of inserting the the contents of the variable it inputs the name of the variable. For example $question = "how are you"; instead of inserting "how are you" it inserts $question.

my code is as follows,

Code: Select all

 
<?php
 
 
function insertQuestions($ques1, $ques2, $ques3, $ques4, $ques5, $ques6, $ques7, $questionSet)
{
    $query= "INSERT INTO questions (question, questionSet, questionType) VALUES";
    
    $loop = 8;
 
    if (strlen($ques7)<1)
        $loop = 7;
        
    if (strlen($ques6)<1)
        $loop = 6;
        
    if (strlen($ques5)<1)
        $loop = 5;
        
    if (strlen($ques4)<1)
        $loop = 4;
        
    if (strlen($ques3)<1)
        $loop = 3;
    
    for($row = 1; $row < $loop; $row++ )
    {   
        $question =  '$ques';
        $question .=  "$row";
        
        //Debug
        //print $question;
        if($row != $loop-1)
        {
            $query.= "('$question', '$questionSet', ''),";  
        }
        else
            $query.= "('$question', '$questionSet', '')"; 
    }
    
    //Debug
    print $query;
 
    //insertMySQL($query);
}
 
?>
 
I am open to any suggestions and Thanks for any help in advance I appreciate it.

Dave
Last edited by Benjamin on Fri May 29, 2009 10:29 am, edited 1 time in total.
Reason: Changed code type from text to php.
eatspinach
Forum Newbie
Posts: 20
Joined: Mon Apr 20, 2009 8:50 am

Re: Help Inserting multiple Values into a Database

Post by eatspinach »

Fantastic Programming.

That second method worked a treat!!!
Thank You. Thank You. Thanks You!

Awesome

:D
Post Reply