Re: Everything works...Unless they hit the "back" button...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Jack Jackson" <jackson.linux@xxxxxxxxx> wrote in message
news:42F0B043.5050108@xxxxxxxxxxxx
> Hi all. This has been an interesting week.
>
> Now the form works, and I am able to error check, if no errors look into
> user answer table and delete from that all q_ids matching the ones just
> picked, insert this page of question/answer info (q_id/a_id) into the
> user answer db, and if successful advance the category by 1 and get more
> questions. It works.
>
> I must have a logic error though because for some reason even though I
> say to delete matching  q_ids and reinsert, then move on, if the user
> hits the back button, changes answers and hits submit again, the table
> does not update . .  . and the category does not increment. We get stuck
> on this page of questions/answers forever.

Do you want to allow people to go back and chnage things ?

If so, write a suitable UPDATE statement

Sample UPDATE syntax

UPDATE table
SET field=1
WHERE field2='x'

You will probably want to check whether the answers already exist, use a
select statement for this. Unsure if this works in mysql, but

IF NOT EXISTS
(select id from table where field=1)
UPDATE table
SET field=1
WHERE field2='x'

might do the trick, or something similar will at any rate.

If you want to stop people going  back, check the referer (sic)
>
> What DOES happen after BACK is that the answers they select get passed
> back to error check (eg if they select a select box to nothing, error
> checking reenters its value into $message) and those values get passed
> back to the display page (eg $_POST[$qname] == $aid) because the
> "selected" answers change. It's just the db call and category advance
> which get stuck.

>
> Any help will be greatly appreciated - Here's the code:
>
> <?php
> //Start the session
> session_start();
> //Error announcements
> echo "POST:<BR />";
> print_r($_POST);
> echo "<br /><br />";
> echo "required session:<BR />";
> var_dump($_SESSION['required_fields']);
>
> echo "<br />\$ cat:" . $cat . "<br />";
> echo "\$message: ";
> var_dump($message);
>
> //error_reporting(E_ALL);
>
>
> /* A script to retrieve from database questions and answers,
>   * create a multi-page HTML form, error check answers and
>   * submit them to the database on a per-user basis.
>   * August 2005
>   */
>
> //Some basic vars
>   if (!isset($cat)) { $cat = "1"; }
> $error=0;
> $SUCCESS=0;
>
>
> if (!isset($message))
>      {
>      $message = array();
>      }
>
> if (!isset($_SESSION['required_fields']))
>      {
>      $_SESSION['required_fields'] = array();
>      }
>
> if(!sizeof($_POST))
> {
> include_once(QUESTIONS . 'q.inc');
>      }
>
>   //error checking
>
>   reset($_SESSION['required_fields']);
>    foreach ($_SESSION['required_fields'] as $fieldname)
>      {
>         if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname]))
>         {
>          $message[$fieldname]=1;
>         }
>
>       }//error check
>    if (!empty($message))
>    {           $cat=$_POST['cat'];
>                include_once(QUESTIONS . 'q.inc');
>    }
>
> //No errors? Store what's been done so far
>
>        if ( ($_POST['action'] == 'process') && (!sizeof($message) ) )
>        {
>            foreach($_POST as $key=>$val)
>                 {
>                     //find key/val sets within posts which are both
numeric
>                     if(is_numeric($key) && is_numeric($val))
>                     {
>                        $nkey=$key;
>                        //add keys to the qidlist
>                        $qidlist[] .= $key;
>                        //add these values ( q_id, a_id ) to sql statement
>                        $qanda[] .= "('1' , '" . $nkey . "' , '" . $val .
> "')";
>                     }
>                     //find key/val sets within sub-arrays of $_POST
> which are numeric
>                     if(is_array($val))
>                     {
>                         foreach ($val as $akey=>$aval)
>                             {
>                             //add these values ( q_id, a_id ) to sql
> statement
>                             $qanda[] .= "('1' , '" . $key . "' , '" .
> $aval . "')";
>                             var_dump($qanda);
>                             }
>                     }
>                 }
>
>
>                 $qidlist_sql="DELETE FROM $userAnswers WHERE q_id IN ("
> . (implode(",",$qidlist)) . ");";
>
>                 $q_a_sql="INSERT INTO $userAnswers (u_id, q_id, a_id )
>                  VALUES " . (implode(",",$qanda)) . ";";
>
>                   mysql_query($qidlist_sql);
>
>
>                    if($q_a_result = mysql_query($q_a_sql))
>                      {
>                        unset($_SESSION['required_fields']);
>                        $cat = $_POST['cat']+1;
>                        include_once(QUESTIONS . 'q.inc');
>                      }
>
>                  else
>                      {
>                      echo "<b>A fatal MySQL error occured</b>.\n
>                      <br />Query: " . $q_a_sql . "<br />\nError: (" .
> mysql_error();
>                      die;
>                      }
>
>        }
> ?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux