Enquiry script

Discussion in 'PHP' started by eddyvsbk, Dec 30, 2006.

  1. #1
    Hey,

    the following script is meant to do an enquiry. Each question should start together with the choice possibilities on a new page.
    Registration has not been programmed yet.
    The problem is that the execution stops after the second question.
    Why? Could anyone give a tip for a solution?

    Thanks,

    Eddy from Belgium

    <?php
    //connection to a test database
    $server = "*****";
    $username = "*****";
    $password = "*****";
    $db = "*****";   
    // please adapt these for your own use
    $connection = mysql_connect($server,$username,$password) or die ("No connection with the server");
    mysql_select_db($db,$connection) or die ("Could not select a database");
    //make and fill the table
    $table = "questions";
    if(!mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) {
    $sql1 = "CREATE TABLE `questions` (
      `IDquestion` tinyint(7) NOT NULL auto_increment,
      `question` varchar(200) NOT NULL,
      `firstanswer` varchar(20) NOT NULL,
      `secondanswer` varchar(20) NOT NULL,
      `thirdanswer` varchar(20) NOT NULL,
      `fourthanswer` varchar(20) NOT NULL,
      `fifthanswer` varchar(20) NOT NULL,
      `number` tinyint(3) NOT NULL,
      PRIMARY KEY  (`IDquestion`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
    
    $sql2 = "INSERT INTO `questions` VALUES (1, 'What is your age?', 'less than 20', '20 to 30', '30 to 40', '40 to 50', 'more than 50', 1);";
    $sql3 = "INSERT INTO `questions` VALUES (2, 'What is your most important hobby?', 'sport', 'travel', 'culture', 'computer', 'other', 3);";
    $sql4 = "INSERT INTO `questions` VALUES (3, 'What do you read the most?', 'newspapers', 'periodics', 'books', 'on line articles', 'nothing at all', 4);";
    $sql5 = "INSERT INTO `questions` VALUES (4, 'Which school did you follow?', 'lower', 'medium', 'high school', 'university', 'post-university', 2);";
    $sql6 = "INSERT INTO `questions` VALUES (5, 'In which sector you (want to) work?', 'agriculture', 'industry', 'services', 'medical', 'government', 5);";
    $sql7 = "INSERT INTO `questions` VALUES (6, 'What is your opinion about this inquiry?', 'very bad', 'bad', 'not too bad', 'good', 'very good', 6);";
    
    mysql_query($sql1) or die(mysql_error());
    mysql_query($sql2) or die(mysql_error());
    mysql_query($sql3) or die(mysql_error());
    mysql_query($sql4) or die(mysql_error());
    mysql_query($sql5) or die(mysql_error());
    mysql_query($sql6) or die(mysql_error());
    mysql_query($sql7) or die(mysql_error());
      echo "table has been created<br /><br /><br />";
    } else {
      echo "table already existed<br /><br /><br />";
    }
    //enquiry script - the aim is to start each question on a new page
    $var1 = "
    <form action='' method='POST' style='margin: 25px;'>
    <input type='radio' name='choice' value='1'> 
    ";
    $var2 = "
    <input type='radio' name='choice' value='2'> 
    ";
    $var3 = "
    <input type='radio' name='choice' value='3'> 
    ";
    $var4 = "
    <input type='radio' name='choice' value='4'> 
    ";
    $var5 = "
    <input type='radio' name='choice' value='5'> 
    ";
    $var6 = "
    <br /><br /><br /><br />
    <input type='submit' value='submit' name='submit'>
    </form>
    ";
    $sql = mysql_query("SELECT * FROM questions");
    $count = mysql_num_rows($sql);
    $nr = 1;
    $further = TRUE;
    while (($nr <= $count) and $further) {
      if(isset($_POST['submit'])) {
        $choice = $_POST['choice'];
        $further = TRUE;
        echo "choice = ".$choice."<br />";
      } else {
        $sql = mysql_query("SELECT * FROM questions WHERE number = ".$nr);
        while($record = mysql_fetch_object($sql)) {
          echo "<b>".$record->question."</b>";
          echo $var1.$record->firstanswer;
          echo $var2.$record->secondanswer;
          echo $var3.$record->thirdanswer;
          echo $var4.$record->fourthanswer;
          echo $var5.$record->fifthanswer;
          echo $var6;
        }
        $further = FALSE;
      }
      $nr = $nr + 1;
      unset($_POST['submit']);
      echo "nr=".$nr."/".$count."<br />";
    }
    //How comes the execution of the script stops after the second question?
    ?>
    
    PHP:

     
    eddyvsbk, Dec 30, 2006 IP
  2. RobPinnacle

    RobPinnacle Active Member

    Messages:
    423
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Here is a registeration script, may be useful. I can't even read that.

    <?php
    if($_GET[act]=="register"){
    if(!$_POST[username]){
    print "Please enter a username!";
    include("footer.php");
    exit;
    }
    if(!$_POST){
    print "Please enter an e-mail address!";
    include("footer.php");
    exit;
    }
    if(!$_POST[pass]){
    print "Please enter a password!";
    include("footer.php");
    exit;
    }
    $sel=mysql_fetch_array(mysql_query("select * from players where username='$_POST[username]'"));
    if($sel){
    print "Someone already has that username!";
    include("footer.php");
    exit;
    }
    $sel2=mysql_fetch_array(mysql_query("select * from players where email='$_POST[email]'"));
    if($sel2){
    print "Someone already has that email!";
    include("footer.php");
    exit;
    }
    mysql_query("INSERT INTO `players` ( `username` , `password` , `email` ) VALUES ($_POST[username], $_POST[pass], $_POST[email])") or die("Sorry, we cannot register you due to an error");
    print "You are now registered. You may now login!";
    }
    ?>


    Then use this type of login script...


    <form method=post action=login.php>
    Username:<input type=text name=username><br>
    Pass:<input type=password name=pass><br>
    <input type=submit value=Login></form>
     
    RobPinnacle, Dec 30, 2006 IP
  3. RobPinnacle

    RobPinnacle Active Member

    Messages:
    423
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Sorry this isn't layed out like I'm used to. But I'm in a hurry :)
     
    RobPinnacle, Dec 30, 2006 IP
  4. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #4
    I would write this differently:

    File 1: show_questions.php
    <?php
    
    $sql="select * from questions";
    while (...) {
       // print each question
    };
    
    ?>
    PHP:
    File 2: check_questions.php
    <?php
    $questions=$_POST['questions'];
    foreach question {
      // Lookup the correct answer
      // score or whatever
    }
    ?>
    PHP:
    Things are much easier that way.

    Take a look at:
    http://www.tandac.com/school/showtest.php

    All questions and answers and types of questions are pulled from a database. It might give you a few more ideas.
     
    tandac, Dec 30, 2006 IP