SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    online survey tool - insert data problem

    Hi,

    I am currently developing an application for running online surveys. I have some groundwork done and so far have coded some of the admin area. Currently you can create a survey, add questions, add possible answers to each question and control what way it is displayed (ie: radio button, checkbox or select menu.)

    I have started work on a survey display page and i use some while statements to display the questions. This is working fine but, where I run into problems is when i want to enter the responses into teh DB.

    Here's some sample code for displaying a question:
    PHP Code:
    $qid $qrow['qid'];
            
    $q1 "SELECT * FROM answers WHERE sid = '$sid' AND qid = '$qid'";
            
    $r1 mysql_query($q1) or die(mysql_error());
            echo 
    "<br>";
            while(
    $q1r mysql_fetch_array($r1)){
                echo 
    $q1r['answer']." - <input type=\"radio\" name=\"".$qid."\" value=\"".$q1r['aid']."\"><br>\n";
            }
    //end radio button loop
            
    echo "<p>\n"
    This outputs a radio button type question. Say, I have 3 questions, each of type radio button. The name of each radio button will be set to the $qid which is the ID for the question as stored in the DB. The value of each different radio button will be set to $q1r['aid'] which is the answer ID as stored in the DB. The problem I have is, how can I insert the answers into a DB?

    The database table where the answers will go is like this:
    table:response(rid,qid,sid,response) In an insert statement I would insert $qid into qid, $sid into sid and the value of the selected radio button into 'response'. qid is the question ID and sid is the survey ID. Now, a standard insert for this is simple but the problem i have is because the number of questions is dynamic and the $qid is the name of the variable and the response is the value of that variable. How can I make it so that my insert code will loop through the passed variables and insert the variable name as the 'qid' and the variable value as the 'response'.

    Hopefully I explained that clearly enough! Any help (or alternative ideas) is appreciated.

    Thanks in advance,
    Martin

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would add a hidden input form element to the form.
    PHP Code:
    echo '<input type="hidden" name="qid" value="',$quid,'" />'
    The radio button name would change to something like answer_id. Doing this you can easily fetch that data and insert into the database.

  3. #3
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    I had thought of doing something like that but how could I work it in 1 form that has many different questions? For example, I could have 5 questions, each of these would have its own ID and these would all be in 1 form as I need to submit the data all at the same time. If I had an input field in the form called qid with a value of $qid there would be problems as there would be 5 questions, each with its own id.

    Any ideas on this?

    Thanks,
    Martin

  4. #4
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can drop that hidden form input when you do this:
    PHP Code:
    echo $q1r['answer'],' - <input type="radio" name="answer[',$qid,']" value="',$q1r['aid'],'"><br>',"\n"
    So $_POST['answer'] is an array where the index is the Question id and the value the answer id.
    PHP Code:
    foreach ($_POST['answer'] as $qid => $aid) {
        
    // Do the insert


  5. #5
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried that code like this:
    PHP Code:
    $qid $qrow['qid'];
            
    $q1 "SELECT * FROM answers WHERE sid = '$sid' AND qid = '$qid'";
            
    $r1 mysql_query($q1) or die(mysql_error());
            echo 
    "<br>";
            while(
    $q1r mysql_fetch_array($r1)){
                echo 
    $q1r['answer'],' - <input type="radio" name="answer[',$qid,']" value="',$q1r['aid'],'"><br>',"\n";
                
    //echo $q1r['answer']." - <input type=\"radio\" name=\"".$qid."\" value=\"".$q1r['aid']."\"><br>\n";
            
    }//end radio button loop
            
    echo "<p>\n"
    but when i hit submit I got this error:
    Warning: Invalid argument supplied for foreach() in c:\program files\nusphere\apache\nsdocs\surveytool\disp.php on line 2

    Here is the code from disp.php:
    PHP Code:
    <?php
    foreach ($_POST['answer'] as $qid => $aid) { 
        echo 
    $qid."<br>";

    ?>
    Any ideas? I am running php 4.06 with global variables turned on

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ouch, that's quite a old PHP version, there have been a lot of changes since then. Anyway
    PHP Code:
    foreach ($HTTP_POST_VARS['answer'] as $qid => $aid) {
        echo 
    $qid '<br>';

    should work.

  7. #7
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code worked fine.....thanks. So, would you recommend updating to the latest version of PHP? Are there many changes that would cause errors with old code I have written in other scripts?

  8. #8
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, would you recommend updating to the latest version of PHP?
    Yes, a lot of new functionaliy is available, bugs have ben fixed etc.

    Are there many changes that would cause errors with old code I have written in other scripts?
    The only one I can think of is that register_globals is off by default, which you can change in the PHP.ini.

  9. #9
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok...thanks a lot for you help, I appreciate it. You might see me posting here again with this project if I run into any more problems!

    cheers,
    Martin

  10. #10
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have run into another problem. Say in my survey I have 2 questions - 1 uses radio buttons and the other uses checkboxes...how can i pass these values to be inserted into the DB?

    I tried it by adding similar code to what you showed me:

    PHP Code:
    $qid $qrow['qid'];
            
    $q1 "SELECT * FROM answers WHERE sid = '$sid' AND qid = '$qid'";
            
    $r1 mysql_query($q1) or die(mysql_error());
            echo 
    "<br>";
            while(
    $q1r mysql_fetch_array($r1)){
                echo 
    $q1r['answer'].' - <input type="checkbox" name=answer[',$qid,'] value="',$q1r['aid'],'"><br>',"\n";
            }
    //end checkbox loop
            
    echo "<p>\n"
    However, when I echo out all of the value in the foreach statement it is giving the question with the checkboxes the value of the last checkbox selected. Any ideas on this?

  11. #11
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,692
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)
    However, when I echo out all of the value in the foreach statement it is giving the question with the checkboxes the value of the last checkbox selected. Any ideas on this?
    That sounds like a newbie problem I'd had long ago with a form (HTML) in using the same name for each checkbox element. Can you post the form's code for a quick check?

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  12. #12
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    name=answer[',$qid,'][]
    With that you should have an array of all choices.

  13. #13
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried that code but now when i try to echo out the values it gives me

    Code:
    Q - 1 : A - Array
    from this code:

    PHP Code:
    <?php
    foreach ($HTTP_POST_VARS['answer'] as $qid => $aid) { 
        echo 
    "Q - ".$qid." : A - ".$aid."<br>";

    ?>
    Any ideas?

    Thanks,
    Martin
    Last edited by mcrumlish; Dec 12, 2003 at 07:10.

  14. #14
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I said array.
    PHP Code:
    foreach ($_POST['answer'] as $qid => $aid) {
        echo 
    'Q - ' $qid ' : A - ' implode(', '$aid);


  15. #15
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gaheris
    I said array.
    PHP Code:
    foreach ($_POST['answer'] as $qid => $aid) {
        echo 
    'Q - ' $qid ' : A - ' implode(', '$aid);

    thanks again....that code works when i have checkboxes but now if i have a survey that has 2 questions - 1 with checkboxes and the other with radio buttons, there is an error when implode is used.
    PHP Code:
    WarningBad arguments to implode() in c:\program files\nusphere\apache\nsdocs\surveytool\disp.php on line 6 
    Is there some way i can do a check to see if there is an array that needs to be imploded?

    Cheers,
    Martin

  16. #16
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach ($_POST['answer'] as $qid => $aid) {
        echo 
    'Q - ' $qid ' : A - ';
        echo (
    is_array($aid)) ? implode(', '$aid) : $aid;


  17. #17
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that code. I have came upon another problem now though.

    I set up a survey with 2 questions, 1 was a select box and the other a text field. This is my dropdown code:
    PHP Code:
    $qid $qrow['qid'];
            
    $q1 "SELECT * FROM answers WHERE sid = '$sid' AND qid = '$qid'";
            
    $r1 mysql_query($q1) or die(mysql_error());
            echo 
    '<select name="'.$qid.'">';
            while(
    $q1r mysql_fetch_array($r1)){
                echo 
    "<option value=\"".$q1r['aid']."\">".$q1r['answer']."</option>\n";
            }
    //end dropdown loop
            
    echo "</select>\n"
    When I hit submit it gives this error:
    PHP Code:
    WarningInvalid argument supplied for foreach() in c:\program files\nusphere\apache\nsdocs\surveytool\disp.php on line 3 
    thanks,
    Martin

  18. #18
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's ok...I got it working

  19. #19
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have found a nw problem when inserting the data.

    i have this code in the insertion page:
    PHP Code:
    foreach ($HTTP_POST_VARS['answer'] as $qid => $aid) { 
        echo 
    'Q - ' $qid ' : A - '
        echo (
    is_array($aid)) ? implode(', '$aid)."<br>" $aid."<br>"
        
    $q "INSERT INTO response(rid,qid,sid,response) VALUES ('','$qid','$sid','$aid')";
        
    $r mysql_query($q) or die(mysql_error());

    When this runs it inserts the valus from the survey into the database table but, when I have an answer that is made of checkboxes it just enters 'Array' into the database. Any ideas?

    Thanks

  20. #20
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this
    PHP Code:
    foreach ($HTTP_POST_VARS['answer'] as $qid => $aid) {
        echo 
    'Q - ' $qid ' : A - ';
        echo (
    is_array($aid)) ? implode(', '$aid)."<br>" $aid."<br>";
        if (
    is_array($aid)) {
            foreach (
    $aid as $_id) {
                
    $q "NSERT INTO response (rid, qid, sid, response) VALUES ('', '$qid', '$sid',' $_id')";
                
    $r mysql_query($q) or die(mysql_error());
            }
        } else {
            
    $q "INSERT INTO response(rid, qid, sid, response) VALUES ('', '$qid', '$sid', '$aid')";
            
    $r mysql_query($q) or die(mysql_error());
        }


  21. #21
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I am still working on my survey tool and I have run into another problem (hopefully Gaheris can be a help here again! )

    I am currently working on dividing the survey into a certain number of questions per page. This presented the problem of passing the data from one page to another and so on.

    On its own, this is easy to do but when developing a scaable dynamic survey tool it is a lot more difficult. At the moment I use the code above to insert the data into a database. Is there a better way than hidden form fields for storing the data throughout a survey and only inserting it int the DB at the end. Maybe this could be done in sessions?

    if so, I could do with some pointers!

    Thanks in advance,
    Martin

  22. #22
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, sessions are the way to go. I don't know how you have divided the questions into pages, so I'm not sure about the structure and the variables which have to be passed through the different pages.

    PHP Code:
    // page 1
    session_start();
    $_SESSION['answer'] = $_POST['answer'];

    // page 2
    session_start();
    echo 
    '<pre>';
    print_r($_SESSION['answer']);
    echo 
    '</pre>'

  23. #23
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I divide the questions using LIMIT in the SQL call. Each survey has a $limit value and this is passed to each new page along with a variable called $start which in the SQL forms the statement like this: SELECT * FROM questions LIMIT $start, $limit

    I will have a play around with sessions to see what I can get working.

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •