I am working on a open source script at the moment, but I am struggling on one final aspect of the script.

I am a bit of a newb to PHP at this level, I have a working knowledge of PHP and MySQL, but this seems to be a little beyond my knowledge.

I want the users to be able to answer a questionaire, this will be then stored in the MySQL database and displayed on the users profile. But when I press submit it is going into the database. The script is as follows:

Quesionaire PHP code:

<?php

$cVersion = '1.0';
$cType = 'widget';


    if ( $_SESSION['UserId'] =='' ) {

        header('location: signup.php');
        exit;

}   

$vCheck = $fDB->getOne ("SELECT count(*) from ! where name = ? and type = ?", array( FUSE_VERSIONS, $cName, $cType) );
if ($vCheck != '1') {
    $fDB->query( 'insert into ! (name, version, type, date) values (?, ?, ?)', array( FUSE_VERSIONS, htmlentities($cName), htmlentities($cVersion), htmlentities($cType)) );
} else {
    $fDB->query( "UPDATE ! SET version = ? WHERE name = ? and type = ?" ,array( FUSE_VERSIONS, htmlentities($cVersion), htmlentities($cName), htmlentities($cType) ) );
}

unset($cName, $cVersion, $vCheck, $cType);


    // Query to reterive records from KangaDate_questions table, sorted descending on mandatory: that is, mandatory fields should be displayed first
    $temp = $fDB->getAll( 'select id, question, mandatory, description, guideline, maxlength, control_type from ! where enabled = ? and question <> ? and gender in (?,?) order by mandatory desc, displayorder', array( QUESTIONS_TABLE, 'Y' , '',$_SESSION['gender'],'A') );

    $data = array();

    foreach( $temp as $index => $row ) {

        if ($_SESSION['opt_lang'] != 'english') {
            /* THis is made to adjust for multi-language */
            $lang_question = $_SESSION['profile_questions'][$row['id']]['question'];
            $lang_descr =   $_SESSION['profile_questions'][$row['id']]['description'];
            $lang_guide =   $_SESSION['profile_questions'][$row['id']]['guideline'];
            if ($lang_question != '') {
                $row['question'] = $lang_question;
            }
            if ($lang_descr != '') {
                $row['description'] = $lang_descr;
            }
            if ($lang_guide != '') {
                $row['guideline'] = $lang_guide;
            }
        }

        // reterive record from osdate_questionoptions table

        $options = $fDB->getAll( 'select * from ! where enabled = ? and questionid = ? order by displayorder', array( OPTIONS_TABLE, 'Y', $row['id'] ) ) ;

        $optsrs = array(); if ($_SESSION['opt_lang'] != 'english') { /* THis is made to adjust for multi-language */ foreach($options as $kx => $opt) { $lang_ansopt = $_SESSION['profile_questions'][$row['id']][$opt['id']]; if ($lang_ansopt != '') {$opt['answer'] = $lang_ansopt; } $optsrs[] = $opt; } } else {$optsrs = $options; }

        unset($options);

        $row['options'] = makeOptions ( $optsrs );

        unset($optsrs);

        $userprefrs = $fDB->getAll( 'select questionid, answer from ! where userid = ?', array( USER_PREFERENCE_TABLE, $userid, $row['id'], ) ) ;

        $row['userpref'] = makeAnswers ( $userprefrs );

        unset($userprefrs);

        $data [] = $row;
    }

    if ( isset( $_GET['errid'] ) ) {

        $t->assign( 'mandatory_question_error', get_lang('errormsgs',$_GET['errid']) );

    }

    $t->assign( 'sectionid', $sectionid );

    $t->assign('frmname', 'frm' . $sectionid );

    $t->assign( 'head', get_lang('myprofilepreferences')." - ".$sections[ $sectionid ] );

    $t->assign( 'data', $data );

    $t->assign('lang', $lang);

    unset($data, $temp, $sections);


    ?>

TPL code:

<font color="{lang mkey='error_msg_color'}">{$mandatory_question_error}</font>
<form name="{$frmname}" method="post" action="savequestion.php">
<input type="hidden" name="sectionid" value="{$sectionid}"/>
<table   width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="{$config.bgcolor}">
<tbody>
    <tr>
        <td colspan="2" class="title">{$head}</td>
    </tr>
    <tr>
        <td colspan="2" width="100%">
{*Outer Loop to traverse outer dimension data array*}
    {foreach item=questionrow from=$data}
        {if $questionrow.control_type == "select"}
            <table   width="250" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
            <tbody>
                <tr>
                    <td width="250">
                        <table width="250" border="0">
                        <tbody>
                            <tr>
                                <td width="250">
                                    {$questionrow.question}
                                    {if $questionrow.mandatory == 'Y'}
                                    <font class="required_info">{$smarty.const.REQUIRED_INFO}</font>
                                    {/if}
                                    <br/>
                                    {if $questionrow.description != NULL}
                                            {$questionrow.description}
                                    {/if}
                                </td>
                            </tr>
                        </tbody>
                        </table>
                    </td>
                    <td width="329">
                        <table   width="329" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
                        <tbody>
                            <tr>
                                <td width="329">
                                    <select name="{$questionrow.id}{$questionrow.mandatory}" class="select" style="width: 125px">
                                        <option value="0">{lang mkey='tell_later'}</option>
                                        {html_options options=$questionrow.options}
                                    </select>
                                </td>
                            </tr>
                        </tbody>
                        </table>
                    </td>
                </tr>
            </tbody>
            </table>
        {elseif $questionrow.control_type == "radio"}
            <table   width="100%" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
            <tbody>
                <tr>
                    <td width="250">
                        <table   width="250" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
                        <tbody>
                            <tr>
                                <td  align="left">
                                    {$questionrow.question}
                                    {if $questionrow.mandatory == 'Y'}
                                        <font class="required_info">{$smarty.const.REQUIRED_INFO}</font>
                                    {/if}
                                    <br/>
                                    {if $questionrow.description != NULL}
                                        {$questionrow.description}
                                    {/if}
                                </td>
                            </tr>
                        </tbody>
                        </table>
                    </td>
                    <td width="329">
                        <table   width="329" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
                        <tbody>
                            <tr>
                                <td>
                            {foreach name="iterator" key=key item=curropt from=$questionrow.options}
                                    <input name="{$questionrow.id}{$questionrow.mandatory}"  type="radio"  value="{$key}"  checked />{$curropt}
                                {if $smarty.foreach.iterator.iteration%2 == 0 }
                                    </td></tr><tr><td>
                                {else}
                                    </td><td>
                                {/if}
                            {/foreach}
                            </tr>
                        </tbody>
                        </table>
                    </td>
                </tr>
            </tbody>
            </table>

        {elseif $questionrow.control_type == "checkbox"}
            <table   width="700" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
            <tbody>
                <tr>
                    <td>
                        <table   width="250" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
                        <tbody>
                            <tr>
                                <td  align="left">
                                    {$questionrow.question}
                                    {if $questionrow.mandatory == 'Y'}
                                        <font class="required_info">{$smarty.const.REQUIRED_INFO}</font>
                                    {/if}
                                    <br/>
                                    {if $questionrow.description != NULL}
                                        {$questionrow.description}
                                    {/if}
                                </td>
                            </tr>
                        </tbody>
                        </table>
                    </td>
                    <td>
                        <table   width="450" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
                        <tbody>
                            <tr>
                                <td align="left">
                                    {html_checkboxes name=$questionrow.id|cat:$questionrow.mandatory   options=$questionrow.options separator=<br/>}
                                </td>
                            </tr>
                        </tbody>
                        </table>
                    </td>
                </tr>
            </tbody>
            </table>
        {elseif $questionrow.control_type == "textarea"}
            <table width="700">
            <tbody>
                <tr>
                    <td>
                        <table   width="450" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
                        <tbody>
                            <tr>
                                <td  align="left">
                                    {$questionrow.question}
                                    {if $questionrow.mandatory == 'Y'}
                                        <font class="required_info">{$smarty.const.REQUIRED_INFO}</font>
                                    {/if}
                                    <br/>
                                    {if $questionrow.description != NULL}
                                        {$questionrow.description}
                                    {/if}
                                </td>
                            </tr>
                        </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td>
                        <table   width="450" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
                        <tbody>
                            <tr>
                                <td>
                                    <textarea name="{$questionrow.id}{$questionrow.mandatory}" rows="7" cols="100">

You dont actually seem to be using any of the POST data.

Or have you not put all your code?

Thanks for that squidge, I will take a look at that. Hopefully it won't take much.

Thanks for that squidge, I will take a look at that. Hopefully it won't take much.

No problem squeak24

OK, finally I have had chance to look at this.

I still very new to actually doing this kind of thing. I have been working with PHP for a number of years, but just getting by, this appears to be way beyond my knowledge.

I have edited the savequestion.php file to look like this:

<?php

    if ( !defined( 'SMARTY_DIR' ) ) {
        include_once( 'init.php' );
    }

    include('sessioninc.php');

    $userid = $_SESSION['UserId'];


    foreach ( $_POST as $questionid => $options ) {
        $j = 0;

        if ( !is_array( $options ) ) {
            //If request variable contains variable=value. This is the case when user option has only one answer.

            $userpref[ $j++ ] = $userid;

            $questionid = substr( $questionid, 0, strlen( $questionid) -1  );

            $userpref[ $j ] = $questionid;

            $j++;

            $userpref[ $j ] = $options;

            //Check that user already has answered question
            $row = $fDB->getRow( 'SELECT id FROM ! WHERE userid = ? AND questionid = ?', array( USER_PREFERENCE_TABLE, $userpref[0], $userpref[1] ) );

            if ( $row ) {
                $fDB->query ( 'UPDATE ! SET userid  = ?, questionid = ?, answer = ?     WHERE id = ?', array(USER_PREFERENCE_TABLE, $userpref[0], $userpref[1], strip_tags($userpref[2]), $row['id']) );
            } else {
                $fDB->query( 'INSERT INTO ! ( userid, questionid, answer ) VALUES ( ?, ?, ? )' , array( USER_PREFERENCE_TABLE, $userpref[0], $userpref[1], strip_tags($userpref[2]) ) );
            }
            unset($row);
        } else {
            //If request variable contains variable=Array. This is the case when user option have many options.
            foreach( $options as $option ) {

                $j = 0;

                $userpref[ $j ] = $userid;

                $j++;


                $qid = substr( $questionid, 0, strlen( $questionid) -1 );

                $userpref[ $j ] = $qid;

                $j++;

                $userpref[ $j ] = $option;

                //Check that user already has answered question
                $row = $fDB->getRow ( 'SELECT id FROM ! WHERE userid=? AND questionid=? AND answer=?', array(USER_PREFERENCE_TABLE, $userpref[0], $userpref[1], $userpref[2] ) );

                //$row = $result->fetchRow();
                if ( $row ) {

                    $fDB->query ( 'UPDATE ! SET userid  = ?, questionid = ?, answer = ?     WHERE id = ?', array(USER_PREFERENCE_TABLE, $userpref[0], $userpref[1], strip_tags($userpref[2]), $row['id']) );
                } else {

                    $fDB->query( 'INSERT INTO ! ( userid, questionid, answer ) VALUES ( ?, ?, ? )' , array( USER_PREFERENCE_TABLE, $userpref[0], $userpref[1], strip_tags($userpref[2]) ) );

                }
            } //foreach
            unset($row, $userpref, $options, $option);

        } //else

    } //foreach

    if( $nextsection['id'] == "" ) 

    {
        header ( 'location: index.php?page=settings&default' );
    }

    ?>

But it still isn't saving the question. Any ideas what I may be doing wrong. Any help would be appreciated.

thanks,this code is executing succesfully

But it still isn't saving the question. Any ideas what I may be doing wrong. Any help would be appreciated.

Are you getting an error?

Have you dumped the POST data to make sure this is populated as you expect?

Have you tested your SQL strings?

Are you getting an error?

OK, whilst trying to edit the SettingsEditProfile.tpl to do the memory dump, it came up with:

Notice: Undefined variable: userid in /home/garyf1/public_html/dev/KangaB1_1/templates/widgets/SettingsEditProfile.php on line 82 Warning: Invalid argument supplied for foreach() in /home/xxxx/public_html/dev/KangaB1_1/includes/internal/Functions.php on line 977 Notice: Undefined variable: sectionid in /home/xxxxx/public_html/dev/KangaB1_1/templates/widgets/SettingsEditProfile.php on line 97 Notice: Undefined variable: sectionid in /home/xxxxxx/public_html/dev/KangaB1_1/templates/widgets/SettingsEditProfile.php on line 99 Notice: Undefined variable: sectionid in /home/garyf1/public_html/dev/KangaB1_1/templates/widgets/SettingsEditProfile.php on line 101 Notice: Undefined index: in /home/xxxxxx/public_html/dev/KangaB1_1/templates/widgets/SettingsEditProfile.php on line 101

Next question:

Have you dumped the POST data to make sure this is populated as you expect?

I completed the form and submitted it to a dump which came up with:

> Data Dump
> No method GET data received.
> Received data method POST —
> Name  Value
> 10N[0]    226
> 11N   10078
> 13N[0]    378
> 14N   the quick brown fox jumps over the lazy dog
> 16N[0]    39
> 17N[0]    368
> 18N   10090
> 19N[0]    10099
> 1N    1
> 20N   51
> 21N   52
> 22N   10106
> 24N[0]    60
> 25N   63
> 26N   66
> 27N   357
> 28N   237
> 29N[0]    196
> 29N[1]    199
> 2N[0] 10022
> 30N[0]    10056
> 3N    10016
> 4N    the quick brown fox jumps over the lazy dog
> 5N    165
> 6N    10031
> 7N    218
> 8N    94
> 9N    100
> sectionid 
> Received cookies data —
> Name  Value
> 7f32f61e95803804eb5b0297330d1697  5ndrltpccdcvbi39tbia0a9pm7
> KangaDateopt_lang english
> OACCAP    3.609_4.82_5.176
> OAID  3321522f21c51ce037121d2d725de4a7
> PHPSESSID ehee285h13r79k8g9ap4grmgc5
> __utma    163853353.271724651.1313692744.1313692744.1313692744.1
> cbe436f93d18bf01e1bb133475ac1961  au7cca2ncf6fp1gsaubuermu76
> fcspersistslider2 3
> jpanesliders_panel-sliders    
> jpanesliders_plugin-sliders-10001 
> phpbb3_kxuqo_k    
> phpbb3_kxuqo_sid  43ac23c980af9b7ec9cb905a99663cfe
> phpbb3_kxuqo_u    1
> style_cookie  printonly
> Environment variables —
> Name  Value
> CONTENT_LENGTH    372
> CONTENT_TYPE  application/x-www-form-urlencoded
> DOCUMENT_ROOT /home/xxxxx/public_html
> GATEWAY_INTERFACE CGI/1.1
> HTTP_ACCEPT   text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
> HTTP_ACCEPT_ENCODING  gzip, deflate
> HTTP_ACCEPT_LANGUAGE  en-gb,en;q=0.5
> HTTP_AUTHORIZATION    
> HTTP_CONNECTION   keep-alive
> HTTP_COOKIE   KangaDateopt_lang=english; fcspersistslider2=3; style_cookie=printonly; OAID=3321522f21c51ce037121d2d725de4a7; phpbb3_kxuqo_k=; phpbb3_kxuqo_u=1; phpbb3_kxuqo_sid=43ac23c980af9b7ec9cb905a99663cfe; __utma=163853353.271724651.1313692744.1313692744.1313692744.1; OACCAP=3.609_4.82_5.176; 7f32f61e95803804eb5b0297330d1697=5ndrltpccdcvbi39tbia0a9pm7; cbe436f93d18bf01e1bb133475ac1961=au7cca2ncf6fp1gsaubuermu76; jpanesliders_panel-sliders=0; jpanesliders_plugin-sliders-10001=0; PHPSESSID=ehee285h13r79k8g9ap4grmgc5
> HTTP_DNT  1
> HTTP_HOST garyfoster.net
> HTTP_REFERER  http://garyfoster.net/dev/KangaB1_1/index.php?page=settings&profile
> HTTP_USER_AGENT   Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:14.0) Gecko/20100101 Firefox/14.0.1
> PATH  /bin:/usr/bin
> PHP_SELF  /dev/KangaB1_1/datadump.php
> QUERY_STRING  
> REDIRECT_STATUS   200
> REMOTE_ADDR   78.144.136.92
> REMOTE_PORT   54828
> REQUEST_METHOD    POST
> REQUEST_TIME  1346272876
> REQUEST_URI   /dev/KangaB1_1/datadump.php
> SCRIPT_FILENAME   /home/xxxxx/public_html/dev/KangaB1_1/datadump.php
> SCRIPT_NAME   /dev/KangaB1_1/datadump.php
> SERVER_ADDR   xxx.xxx.xxx
> SERVER_ADMIN  webmaster@garyfoster.net
> SERVER_NAME   garyfoster.net
> SERVER_PORT   80
> SERVER_PROTOCOL   HTTP/1.1
> SERVER_SIGNATURE  <address>Apache/2.2.3 (CentOS) Server at garyfoster.net Port 80</address>
> SERVER_SOFTWARE   Apache/2.2.3 (CentOS)
> SUPHP_URI /dev/KangaB1_1/datadump.php

Next question:

Have you tested your SQL strings?

I have not tested the SQL strings, I will have a look at how to do this tomorrow. Looking at teh dump file it looks like I am missing a GET command, but unsure what I need to GET

OK, it turned out to be an issue with the version of PHP I was using.

Just one more issue now which I will post in another thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.