ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

I'm trying to figure out this tutorial (phP database driven website)



--- Code: PHP ---<?php// Connect to the database server Make sure not to use a 'blank' root password// the way that I am doing here! :)$dbconn = @mysql_connect('', 'a8784371_kyle', 'xxx');if (!$dbconn){        die('Error connecting to DB!');}// Find the surveys dbif (! @mysql_select_db('a8784371_surveys') ) {         die( '<p>Unable to locate the main database at this time.</p>' ); }  // This page requires a URL parameter with the QuestionID.if(isset($_GET["QuestionID"]))        $Qid = $_GET["QuestionID"];else        die("Please set a question id in the URL"); // Get the Question text corresponding to this ID       $Sql = "Select * from Questions where QuestionID=" . $Qid;$result= mysql_query($Sql) or die(mysql_error());$row = mysql_fetch_row($result);// The text of the Question is in Column 1$QText = $row[1]; // Get the options for this question.$Sql2 = "Select * from Options where QuestionID=" . $Qid;$OptResult = mysql_query($Sql2) or die(mysql_error());// Write out the HTML for the page?><html><head><meta http-equiv="Content-Language" content="en-us"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Test 1</title><style><!--.BulletText  { font-family: Verdana; font-size: 8pt;   text-align: left; line-height: 100%;   word-spacing: 0; margin-top: 0; margin-bottom: 0 }--></style></head> <body><!-- ---------------------------------------------------------------The question is going to be a HTML form. The form will be generated bythis PHP script. It will be a paragraph of text containing the questionfollowed by n radio buttons, with each being the options associated withthe question. Finally, there will be a hidden button containing the Question ID (so it can be passed onto the form processor) ------------------------------------------------------------------><form method="POST" action="process.php"> <!-- Write out the question Text --> <p><?php echo($QText)?></p> <? $nVal = 0; while  ($optrow = mysql_fetch_array($OptResult))  {   if($nVal==0)   // The First Option will be checked.   echo("<p class='BulletText'><input type='radio' value='" .      $nVal . "' checked name='Q' id='Q'>" . $optrow[2] . "</p>");   else   // The Others Won't   echo("<p class='BulletText'><input type='radio' value='" . $nVal      . "' name='Q' id='Q'>" . $optrow[2] . "</p>");  $nVal++;  } ?> <p><input type="hidden" value="<?php echo($Qid); ?>" name="QID"/></p> <p><input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p></form></body></html>I view my php file in the browser using (this is were the file is stored)

The code is executed until the php requests the "questionid" and I get the controlled error message, "Please set a question id in the URL".

By the code above I'm pretty sure that I'm getting into the database correctly.

The tutorial site tells me that I should call the page using example, http://yourserver/survey.php?questionid=1.

So I do

I don't know what is wrong because I'm an relatively new to php.

This is how I created the my fields.

--- Code: Text ---CREATE TABLE `answers` (  `AnswerID` int(11) NOT NULL auto_increment,  `AnswerValue` int(11) default '0',  `AnswerIP` text,  `QuestionID` int(11) default '0',  PRIMARY KEY  (`AnswerID`)) TYPE=MyISAM;  CREATE TABLE `options` (  `OptionID` int(11) NOT NULL auto_increment,  `QuestionID` int(11) default '0',  `OptionText` text,  `OptionValue` int(11) default '0',  PRIMARY KEY  (`OptionID`)) TYPE=MyISAM;  CREATE TABLE `questions` (  `QuestionID` int(11) NOT NULL auto_increment,  `QuestionText` text NOT NULL,  PRIMARY KEY  (`QuestionID`)) TYPE=MyISAM;

Also, I added a record in 'questions' with id being 1, and text being 'What's your favorite color'.
and for options I connected each option to questionid=1.

I'm pretty sure my database is correct because everything makes sense and everything is spelled the same.

Please take a look and give me a few suggestions

Looks like you need to call the link as QuestionID not questionid.  Becuase you are asking for QuestionID on line 16.

Also at the moment you are just passing the query string to the database (i could ask for question 1 and the names of your tables) .  Before you promote it or put it live read through

Oh wow! I had no idea it was case sensitive and to think I spent hours looking at it before I posted the problem here.

Thanks a lot justice. When I graduate college I'm definitely going to pass the buck (literal) because everyone here always helps and with a timely manner.


[0] Message Index

Go to full version