DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • Posts: 24
http://www.devx.com/webdev/Article/27175/0/page/2

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('mysql6.000webhost.com', 'a8784371_kyle', 'xxx');
if (!$dbconn)
{
	die('Error connecting to DB!');
}
// Find the surveys db
if (! @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 by
this PHP script. It will be a paragraph of text containing the question
followed by n radio buttons, with each being the options associated with
the 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 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/survey.php?questionid=1.

So I do http://istem.comyr.com/survey.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: (sql)
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

justice

  • Solve issues simply.
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,898
    • Participated in NANY Challenge Four Times
    • Cody Award
    • Coding Snacks
    • DJ Badge - DJ
Looks like you need to call the link as QuestionID not questionid.  Becuase you are asking for QuestionID on line 16.
http://istem.comyr.com/survey.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.com/websitesecurity/php-security-1.htm

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

h0meopathic

  • Participant
  • Joined in 2007
  • *
  • Posts: 24
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.

Original address: https://www.donationcoder.com/forum/index.php?topic=17356.0