php and forms issue

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
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

php and forms issue

Post by nawhaley »

Ok I've been working on a php script all day and I'm to a point to where I'm not sure my syntax is right regarding one particular part. I have a php page for testing purposes called databasesessiontest.php which calls databasetest2.php through a function called writequestion() like so.

Code: Select all

<?
session_start();
include 'databasetest2.php';


$_SESSION["link"] = odbc_connect("data","name","password");

/* set up query for questions */
$_SESSION["questionquery"] = "Select * from tblQuestions";
$_SESSION["questionresults"] = odbc_exec($_SESSION["link"],$_SESSION["questionquery"]);

$_SESSION["questioncounter"] = 1;
$_SESSION["counter"] = 1;


/* find out type of question Single answer or multiple answer */
$typeofquestion = odbc_result($_SESSION["questionresults"],"QuestionType");

while(odbc_fetch_row($_SESSION["questionresults"]))
{
$_SESSION["questioncounter"]++;

}




/*pull questioncode from the table tblQuestions*/
$questioncode = odbc_result($_SESSION["questionresults"],"QuestionCode");


/* get answers for the questions */
$queryanswers = "Select * from tblAnswers";
$answerresult = odbc_exec($_SESSION["link"],$queryanswers);

/*pull questioncode from tblAnswers*/
$ansquestioncode = odbc_result($answerresult,"QuestionCode");



writequestion();
the next page then writes out the question after connecting to the database etc and uses the print function to print out the HTML like so.

Code: Select all

function writequestion()
{

/*gets the type of questions and question itself from database results,prints question*/  
   print('<FORM METHOD = "POST" ACTION = "databasetest2.php">');
 if( odbc_fetch_row($_SESSION["questionresults"],$_SESSION["counter"])):
   
  
   $question = odbc_result($_SESSION["questionresults"],"Question");
   
   $questioncode = odbc_result($_SESSION["questionresults"],"QuestionCode");
   $typeofquestion = odbc_result($_SESSION["questionresults"],"QuestionType");
   $queryanswers = "Select Answer from tblAnswers where QuestionCode ='$questioncode'";
   $answerresult = odbc_exec($_SESSION["link"],$queryanswers);
  
   print("$question"."<BR>");
   

/* if the question is a single answer S then print as radio button selection else use checkboxes*/  
    if($typeofquestion =="S"):
        //print("hello S"."<BR>");     
        $answer = odbc_result($answerresult,"Answer");
        $correct = odbc_result($answerresult,"Correct");
        print('<INPUT TYPE = "radio" NAME = "answer" VALUE= "$correct">'."$answer"."<BR>");    


 
      while(odbc_fetch_row($answerresult))
        {
           
          $answer = odbc_result($answerresult,"Answer");
          $correct = odbc_result($answerresult,"Correct");
          print('<INPUT TYPE = "radio" NAME = "answer" VALUE= "$correct">'."$answer"."<BR>");
        }
      
    else:
     
       while(odbc_fetch_row($answerresult))
        {
          print("hello M"."<BR>");
          $answer = odbc_result($answerresult,"Answer");
          $correct = odbc_result($answerresult,"Correct");
          print('<INPUT TYPE = "checkbox" NAME = "answer" VALUE= "$correct">'."$answer"."<BR>");
        }

   endif;
     print('<INPUT TYPE ="submit" VALUE = "submit">');
     print('</FORM>');
     $_SESSION["counter"]++; 
   
 endif;

   }
the first time you run the page it works fine the function writes out the HTML and everything displays to my satisfaction. The issue arrises when you click the submit button on the databasetest2.php page. When you do this the next page is blank it will not print a single thing not even a submit button. I think the problem lies in the following line.

Code: Select all

print('<FORM METHOD = "POST" ACTION = "databasetest2.php">');
but I'm running out of ideas on how to test for the breakdown in the logic. I thought at first it was a syntax error but if it was nothing would print even the first time. It also cannot be the way the session is setup because the values change correctly from one page to the next(that much I was able to test). Does anyone more experienced than me have any thoughts on this at all? I'm pretty new to the PHP language and imagine its a simple logic error but its one I've been unable to find a solution to.
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post by thomas777neo »

I have a suggestion, not regarding your question. Your file naming convention is this at the moment databasesessiontest.php. Rather use something like databaseSessionTest.php, which is more readable.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If your function really is that complicated, I'd suggest breaking it into a class.
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post by nawhaley »

its bound to get even more complicated than that. I dont mind reorganizing it but I would like to get back to the orginal problem as to why the it writes the information out once then blanks out upon the second call. I understand this is some fairly lengthy code but I could really use some help here and while I appreciate the suggestions posted so far though thank you :).
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post by nawhaley »

I'm going to bump this one last time before I let it slip off the first page. Can anyone explain to me possibly why I get so few responses to my posts? Are the questions just not well phrased,am I posting to much code or are the questions just that complicated that they take a while to come up with a reponse?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Do you have error reporting turned out?

Can you restate the question and series of events again? I've tried to understand your problem several times and can't quite figure out what you're trying to do.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You also never showed databasetest2.php, which is from what I can decipher the page that's misbehaving.
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post by nawhaley »

I dont seem to be able to catch an error coming out its just that once the page is written out and you click submit to call the following page.

Code: Select all

<?session_start();
writequestion();
function writequestion()
{

/*gets the type of questions and question itself from database results,prints question*/  
   print('<FORM METHOD = "POST" ACTION = "databasetest2.php">');
 if( odbc_fetch_row($_SESSION["questionresults"],$_SESSION["counter"])):
   
  
   $question = odbc_result($_SESSION["questionresults"],"Question");
   
   $questioncode = odbc_result($_SESSION["questionresults"],"QuestionCode");
   $typeofquestion = odbc_result($_SESSION["questionresults"],"QuestionType");
   $queryanswers = "Select Answer from tblAnswers where QuestionCode ='$questioncode'";
   $answerresult = odbc_exec($_SESSION["link"],$queryanswers);
  
   print("$question"."<BR>");
   

/* if the question is a single answer S then print as radio button selection else use checkboxes*/  
    if($typeofquestion =="S"):
        //print("hello S"."<BR>");     
        $answer = odbc_result($answerresult,"Answer");
        $correct = odbc_result($answerresult,"Correct");
        print('<INPUT TYPE = "radio" NAME = "answer" VALUE= "$correct">'."$answer"."<BR>");    


 
      while(odbc_fetch_row($answerresult))
        {
           
          $answer = odbc_result($answerresult,"Answer");
          $correct = odbc_result($answerresult,"Correct");
          print('<INPUT TYPE = "radio" NAME = "answer" VALUE= "$correct">'."$answer"."<BR>");
        }
      
    else:
     
       while(odbc_fetch_row($answerresult))
        {
          print("hello M"."<BR>");
          $answer = odbc_result($answerresult,"Answer");
          $correct = odbc_result($answerresult,"Correct");
          print('<INPUT TYPE = "checkbox" NAME = "answer" VALUE= "$correct">'."$answer"."<BR>");
        }

   endif;
     print('<INPUT TYPE ="submit" VALUE = "submit">');
     print('</FORM>');
     $_SESSION["counter"]++; 
   
 endif;

   }
?>
best idea of the errors its giving are the following which arent really clear to me save the one about not being able to find field "Correct" thats simple enough to fix its the error about the odbc_fetch_row thats not adding up.

Code: Select all

[client 127.0.0.1] PHP Warning:  odbc_result() [<a href='function.odbc-result'>function.odbc-result</a>]: Field Correct not found in C:\\Web\\databasetest2.php on line 43
[client 127.0.0.1] PHP Warning:  odbc_result() [<a href='function.odbc-result'>function.odbc-result</a>]: Field Correct not found in C:\\Web\\databasetest2.php on line 43
[client 127.0.0.1] PHP Warning:  odbc_result() [<a href='function.odbc-result'>function.odbc-result</a>]: Field Correct not found in C:\\Web\\databasetest2.php on line 43
[client 127.0.0.1] PHP Warning:  odbc_fetch_row(): supplied argument is not a valid ODBC result resource in C:\\Web\\databasetest2.php on line 17, referer: http://localhost\\databasetestsesssion.php
the overall goal of the app is to have it print one question and one answerset from a database. It should then stop and allow the user to answer and when submit is hit pull the "next" question and answerset from the database. It will do some calculations and refer the user to certain places if the answer is wrong later on but for right now I just need it to print a question,stop,then after user input print the next question.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

One first observation, I'm not too familiar with the ODBC family of database queries, but generally speaking you can not put a database connection resource or a result set resource into a session and expect it to be present on the next page. This may be part of your problem. It also looks like you're sticking a lot into the session scope that doesn't belong there and using it as a $GLOBALS array for passing data between your functions. This might also be causing problems.

can you put this at the top of your script (after session_start is fine)

Code: Select all

echo "ErrorLevel: ".ini_get("error_reporting")."<br />";
echo "ErrorDisplay:" .ini_get("display_errors"). "<br />";
and let us know what gets printed out
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post by nawhaley »

Hmm maybe I'm not understanding how to use sessions properly I generally pull from the global $_SESSION array basically because the session dosen't seem to work if I just use session_register('somevariable'). At any rate here are the results from the two lines you asked me to put in the code.

Code: Select all

ErrorLevel: 2047
ErrorDisplay:
are there any good guides you could suggest towards using sessions and possibly OOP as well. I'm afraid that even though I learned java in college the concepts and implementation of a solid OO structure still eludes me :(.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

OK at the top of your script add

Code: Select all

ini_set("display_errors",1);
Your server is configured to NOT display errors, even though its set to report all of them. This may help idenfiy what's going wrong.

session_register() is old, so yes, don't use that.

If you're working with functions, and you need a db handle, pass the database resource into the function as an argument, not overloading the $_SESSION.

for instance

Code: Select all

function writeQuestion($db,$questionNumber) {
   // ...
     $answerResult =odbc_exec($db,$queryAnswer);
  // ...
}

$db = odbc_connect(...);
$questionNumber=$_SESSION["QuestionNumber"];
writeQuestion($db,$questionNumber);
nawhaley
Forum Commoner
Posts: 85
Joined: Wed May 18, 2005 11:43 am

Post by nawhaley »

ok heres the output as it stands now don't mind the goofy question and the even goofier answers I'm kind of a monty python fan and was having some fun with my test data :lol: .

Code: Select all

ErrorLevel: 2047
ErrorDisplay:1


Warning: odbc_fetch_row(): supplied argument is not a valid ODBC result resource in C:\Web\databasetest2.php on line 20
What is your favorite color?
Blue

Warning: odbc_result() [function.odbc-result]: Field Correct not found in C:\Web\databasetest2.php on line 46
Yellow

Warning: odbc_result() [function.odbc-result]: Field Correct not found in C:\Web\databasetest2.php on line 46
Green

Warning: odbc_result() [function.odbc-result]: Field Correct not found in C:\Web\databasetest2.php on line 46
Green no Blue ahhhhhh!!
So basically your saying I need to recreate my database link in the databasetest2 I cant just pass it as a value in my $SESSION array?

I.E.

Code: Select all

$_SESSION["link"] = odbc_connect("database","username","password");

/* set up query for questions */
$_SESSION["questionquery"] = "Select * from tblQuestions";
$_SESSION["questionresults"] = odbc_exec($_SESSION["link"],$_SESSION["questionquery"]);
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Yes that's exactly what I'm saying. Database and result resources do not survive the end of the script. So you'll need to reconnect on every page, you'll also need to re-issue the query to get a valid result resource.
Post Reply