Website Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • August 30, 2015, 11:47:02 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: I'm trying to figure out this tutorial (phP database driven website)  (Read 2076 times)

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • gravatar avatar
  • Posts: 24
    • View Profile
    • Donate to Member
http://www.devx.com/...ticle/27175/0/page/2

Code: PHP
  1. <?php
  2. // Connect to the database server Make sure not to use a 'blank' root password
  3. // the way that I am doing here! :)
  4. $dbconn = @mysql_connect('mysql6.000webhost.com', 'a8784371_kyle', 'xxx');
  5. if (!$dbconn)
  6. {
  7.         die('Error connecting to DB!');
  8. }
  9. // Find the surveys db
  10. if (! @mysql_select_db('a8784371_surveys') )
  11. {
  12.         die( '<p>Unable to locate the main database at this time.</p>' );
  13. }
  14.  
  15. // This page requires a URL parameter with the QuestionID.
  16. if(isset($_GET["QuestionID"]))
  17.         $Qid = $_GET["QuestionID"];
  18. else
  19.         die("Please set a question id in the URL");
  20.  
  21. // Get the Question text corresponding to this ID      
  22. $Sql = "Select * from Questions where QuestionID=" . $Qid;
  23. $result= mysql_query($Sql) or die(mysql_error());
  24. $row = mysql_fetch_row($result);
  25. // The text of the Question is in Column 1
  26. $QText = $row[1];
  27.  
  28. // Get the options for this question.
  29. $Sql2 = "Select * from Options where QuestionID=" . $Qid;
  30. $OptResult = mysql_query($Sql2) or die(mysql_error());
  31. // Write out the HTML for the page
  32. ?>
  33. <html>
  34. <head>
  35. <meta http-equiv="Content-Language" content="en-us">
  36. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  37. <title>Test 1</title>
  38. <style>
  39. <!--
  40. .BulletText  
  41. { font-family: Verdana; font-size: 8pt;
  42.   text-align: left; line-height: 100%;
  43.   word-spacing: 0; margin-top: 0; margin-bottom: 0 }
  44. -->
  45. </style>
  46. </head>
  47.  
  48. <body>
  49. <!-- ---------------------------------------------------------------
  50. The question is going to be a HTML form. The form will be generated by
  51. this PHP script. It will be a paragraph of text containing the question
  52. followed by n radio buttons, with each being the options associated with
  53. the question. Finally, there will be a hidden button containing the
  54. Question ID (so it can be passed onto the form processor)
  55. ------------------------------------------------------------------>
  56. <form method="POST" action="process.php">
  57.  <!-- Write out the question Text -->
  58.  <p><?php echo($QText)?></p>
  59.  <?
  60.  $nVal = 0;
  61.  while  ($optrow = mysql_fetch_array($OptResult))
  62.   {
  63.    if($nVal==0)
  64.    // The First Option will be checked.
  65.    echo("<p class='BulletText'><input type='radio' value='" .
  66.      $nVal . "' checked name='Q' id='Q'>" . $optrow[2] . "</p>");
  67.    else
  68.    // The Others Won't
  69.    echo("<p class='BulletText'><input type='radio' value='" . $nVal
  70.      . "' name='Q' id='Q'>" . $optrow[2] . "</p>");
  71.   $nVal++;
  72.   }
  73.  ?>
  74.  <p><input type="hidden" value="<?php echo($Qid); ?>" name="QID"/></p>
  75.  <p><input type="submit" value="Submit" name="B1">
  76.  <input type="reset" value="Reset" name="B2"></p>
  77. </form>
  78. </body>
  79. </html>
  80.  
I view my php file in the browser using http://istem.comyr.com/survey.php (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/su...vey.php?questionid=1.

So I do http://istem.comyr.c...vey.php?questionid=1

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
  1. CREATE TABLE `answers` (
  2.   `AnswerID` int(11) NOT NULL auto_increment,
  3.   `AnswerValue` int(11) default '0',
  4.   `AnswerIP` text,
  5.   `QuestionID` int(11) default '0',
  6.   PRIMARY KEY  (`AnswerID`)
  7. ) TYPE=MyISAM;
  8.  
  9. CREATE TABLE `options` (
  10.   `OptionID` int(11) NOT NULL auto_increment,
  11.   `QuestionID` int(11) default '0',
  12.   `OptionText` text,
  13.   `OptionValue` int(11) default '0',
  14.   PRIMARY KEY  (`OptionID`)
  15. ) TYPE=MyISAM;
  16.  
  17. CREATE TABLE `questions` (
  18.   `QuestionID` int(11) NOT NULL auto_increment,
  19.   `QuestionText` text NOT NULL,
  20.   PRIMARY KEY  (`QuestionID`)
  21. ) TYPE=MyISAM;
  22.  


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


justice

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,896
    • View Profile
    • Donate to Member
Re: I'm trying to figure out this tutorial (phP database driven website)
« Reply #1 on: March 09, 2009, 05:55:38 AM »
Looks like you need to call the link as QuestionID not questionid.  Becuase you are asking for QuestionID on line 16.
http://istem.comyr.c...vey.php?QuestionID=1

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 http://www.acunetix....y/php-security-1.htm

« Last Edit: March 09, 2009, 06:01:50 AM by justice »

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • gravatar avatar
  • Posts: 24
    • View Profile
    • Donate to Member
Re: I'm trying to figure out this tutorial (phP database driven website)
« Reply #2 on: March 09, 2009, 07:49:15 AM »
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.