[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

PHP script to display survey results

Hi!
I got a PHP script to conduct an online survey. But the script is incomplete and does not include scripts to retrieve abd display the survey results from MySQL database. So I typed in scripts (marked "my addition" in the scripts below) to do the job, but it does not work. I need you experts' help.  

Thanks a lot!

___________________________survey_add.php  ________________________________________

<?php
   
    // Connect to DB here  
 
    $MySQL_Server   = "localhost";
    $MySQL_User     = "root";
    $MySQL_Password = "root";
    $MySQL_Database = "survey_questions";
    $MySQL_Table    = "survey_1_results";
 
    mysql_connect($MySQL_Server, $MySQL_User, $MySQL_Password);
    mysql_select_db($MySQL_Database);
   
    $query = "
        INSERT INTO $MySQL_Table
        VALUES ('$q01', '$q02', '$q03', '$q04', '$q05',
                '$email', '$conf_num', '$dept','')
    ";
   
    $result = mysql_query($query);
?>
//    if ($result) {
//?>
  <p>Your survey was added to the database ...<br />
  //<a href=\"http://survey.yourcompany.com/?view=results">view results here</a></p>


////////////////////// Here is my addition  /////////////////////////////////////////////

<?php  
if (mysql_num_rows($result) > 0) {
     // yes
     // print them one after another
     echo "<table cellpadding=10 border=1>";
     while(list($q01, $q02, $q03,$04, $q05,$email,$conf_num,$dept)  = mysql_fetch_row($result)) {
          echo "<tr>";
          echo "<td>$q01</td>";
          echo "<td>$q02</td>";
          echo "<td>$q03</td>";
          echo "<td>$q04</td>";
          echo "<td>$q05</td>";
          echo "<td>$email</td>";
          echo "<td>$conf_num</td>";
          echo "<td>$dept</td>";echo "<td>$q03</td>";
          echo "</tr>";
     }
     echo "</table>";
}
else {
     // no
     // print status message
     echo "No rows found!";
}  

////////////////////////// The end of my addition  ////////////////////////////////////////
   
<?php
    } else {
?>

   <p>Your survey could not be added to the database.";

<?php
    }
?>

___________________________________________survey_html.php _________________________
<?php

   
 
    echo '<form method="POST" action="survey_add.php">';

   
 
    $MySQL_Server   = "localhost";
    $MySQL_User     = "root";
    $MySQL_Password = "root";
    $MySQL_Database = "survey_questions";
    $MySQL_Table    = "survey_1";
 
    mysql_connect("$MySQL_Server", "$MySQL_User", "$MySQL_Password");
    mysql_select_db("$MySQL_Database");
     
    $query = "SELECT * FROM $MySQL_Table ORDER BY question_number ASC";
    $result = mysql_query($query);

    if ($result) {
        while ($r = mysql_fetch_array($result)) {
            $ID                = $r["ID"];
            $question_number   = $r["question_number"];
            $question_text     = $r["question_text"];
            $yes_or_no         = $r["yes_or_no"];
            $pull_down_text    = $r["pull_down_text"];
            $options           = $r["options"];

            // take out slashes in entry

            $question_text = stripslashes ($question_text);

           // show questions from database

           echo "$question_number.) $question_text<br><br>";

           // if *not* Yes or No, replace && and &
 
           if ($yes_or_no == "NO") {

               // begin replacement

               $options = preg_replace ('/&([^%]+)%\ &&([^%]+)%%/',
                                        '<option value="\\1">\\2</option>', $options);

              // now show HTML with imbedded PHP to create drop down menus
 
?>
    <select size="1" name="q<?php echo $question_number; ?>">
        <option selected><?php echo $pull_down_text; ?></option>
        <option> ------ </option>
        <?php echo $options; ?>
    </select>
    <br /><br />

<?php

              // if Yes or NO produce radio buttons

          } else if ($yes_or_no == "YES") {

?>

  &nbsp;<input type="radio" name="q<?php echo $question_number; ?>" value="YES">YES<br/>
  &nbsp;<input type="radio" name="q<?php echo $question_number; ?>" value="NO">NO<br/>
  &nbsp;<input type="radio" checked
                            name="q<?php echo $question_number; ?>" value="NA">N/A<br/>
    <br />
<?php
      }
  }

    // end of questions
?>
    <input type="submit" value="Submit Survey" name="submit">
</form>
<?php
    } else {
         echo "No data.";    
    }

    mysql_free_result($result);

?>
________________________________________________________________________________________


0
duta
Asked:
duta
  • 7
  • 5
  • 3
  • +1
2 Solutions
 
RoonaanCommented:
Before you are able to fetch the inserted record, you would have to do a "SELECT query first.

You could try and use:

////////////////////// Here is my addition  /////////////////////////////////////////////

<?php  

$query = 'SELECT * FROM '.$MySQLTable.' WHERE someIDField = '.mysql_insert_id().' DESC LIMIT 1';
$result = mysql_query($query);

Note that you herefor need to have a someIDField which is preferably an AUTO_INCREMENT integer field.

-r-
0
 
Harisha M GCommented:
Hi, change the added code to this:


<?php  

$sql = "SELECT * FROM $MySQL_Table";
$result = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result) > 0) {
     // yes
     // print them one after another
     echo "<table cellpadding=10 border=1>";
     while(list($q01, $q02, $q03,$04, $q05,$email,$conf_num,$dept)  = mysql_fetch_row($result)) {
          echo "<tr>";
          echo "<td>$q01</td>";
          echo "<td>$q02</td>";
          echo "<td>$q03</td>";
          echo "<td>$q04</td>";
          echo "<td>$q05</td>";
          echo "<td>$email</td>";
          echo "<td>$conf_num</td>";
          echo "<td>$dept</td>";echo "<td>$q03</td>";
          echo "</tr>";
     }
     echo "</table>";
}
else {
     // no
     // print status message
     echo "No rows found!";
}  


---
Harish
0
 
Harisha M GCommented:
Sorry Roonaan, forgot to refresh
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dutaAuthor Commented:
TO: Roonaan and mgh_mgharish:

Thank you so much for your very kind, prompt response.

By the way, I modified my scripts as you kindly suggested. But for some reason, nothing happens (when I clicked Sumbit button, a blank screen shows up).

I am a novice in PHP. I am spending quite much time every day in learning PHP, but I do not think I am making any progress.

Thanks again!

duta
Sunday at 3:58 a.m.

0
 
RoonaanCommented:
Then there probably is a syntax error.

Do you have a editor with syntax highlighting? Otherwise, post the full code and harish and I will look for the error.

-r-
0
 
dutaAuthor Commented:
TO: Roonaan :

Thanks again for your kind super-prompt response.

I am posting my full scripts below. What I just found is that when I clicked Submit button after checkking answers to the questions, the answers are not in the database. That seems to be the problem.

Here are my full scripts:

Thanks again!

duta

Sunday at 4:12 a.m.

___________________________ survey_form.php  ____________________________________

?php

   
 
    echo '<form method="POST" action="process.php">';

   
 
    $MySQL_Server   = "localhost";
    $MySQL_User     = "root";
    $MySQL_Password = "root";
    $MySQL_Database = "survey_questions";
    $MySQL_Table    = "survey_1";
 
    mysql_connect("$MySQL_Server", "$MySQL_User", "$MySQL_Password");
    mysql_select_db("$MySQL_Database");
     
    $query = "SELECT * FROM $MySQL_Table ORDER BY question_number ASC";
    $result = mysql_query($query);

    if ($result) {
        while ($r = mysql_fetch_array($result)) {
            $ID                = $r["ID"];
            $question_number   = $r["question_number"];
            $question_text     = $r["question_text"];
            $yes_or_no         = $r["yes_or_no"];
            $pull_down_text    = $r["pull_down_text"];
            $options           = $r["options"];

            // take out slashes in entry

            $question_text = stripslashes ($question_text);

           // show questions from database

           echo "$question_number.) $question_text<br><br>";

           // if *not* Yes or No, replace && and &
 
           if ($yes_or_no == "NO") {

               // begin replacement

               $options = preg_replace ('/&([^%]+)%\ &&([^%]+)%%/',
                                        '<option value="\\1">\\2</option>', $options);

              // now show HTML with imbedded PHP to create drop down menus
 
?>
    <select size="1" name="q<?php echo $question_number; ?>">
        <option selected><?php echo $pull_down_text; ?></option>
        <option> ------ </option>
        <?php echo $options; ?>
    </select>
    <br /><br />

<?php

              // if Yes or NO produce radio buttons

          } else if ($yes_or_no == "YES") {

?>

  &nbsp;<input type="radio" name="q<?php echo $question_number; ?>" value="YES">YES<br/>
  &nbsp;<input type="radio" name="q<?php echo $question_number; ?>" value="NO">NO<br/>
  &nbsp;<input type="radio" checked
                            name="q<?php echo $question_number; ?>" value="NA">N/A<br/>
    <br />
<?php
      }
  }

    // end of questions
?>
    <input type="submit" value="Submit Survey" name="submit">
</form>
<?php
    } else {
         echo "No data.";    
    }

    mysql_free_result($result);

?>
____________________________ process.php  _____________________________________________

<?php
   
    // Connect to DB here  
 
    $MySQL_Server   = "localhost";
    $MySQL_User     = "root";
    $MySQL_Password = "root";
    $MySQL_Database = "survey_questions";
    $MySQL_Table    = "survey_1_results";
 
    mysql_connect($MySQL_Server, $MySQL_User, $MySQL_Password);
    mysql_select_db($MySQL_Database);
   
    $query = "
        INSERT INTO $MySQL_Table
        VALUES ('$q01', '$q02', '$q03', '$q04', '$q05',
                '$email', '$conf_num', '$dept','')
    ";
   
   $result = mysql_query($query);
?>
//    if ($result) {
//?>
  <p>Your survey was added to the database ...<br />
  //<a href=\"http://survey.yourcompany.com/?view=results">view results here</a></p>
<?php
 

<?php  

$query = 'SELECT * FROM '.$MySQL_Table.' WHERE ID = '.mysql_insert_id().' DESC LIMIT 1';
$result = mysql_query($query);



if (mysql_num_rows($result) > 0) {
     // yes
     // print them one after another
     echo "<table cellpadding=10 border=1>";
     while(list($q01, $q02, $q03,$04, $q05,$email,$conf_num,$dept)  = mysql_fetch_row($result)) {
          echo "<tr>";
          echo "<td>$q01</td>";
          echo "<td>$q02</td>";
          echo "<td>$q03</td>";
          echo "<td>$q04</td>";
          echo "<td>$q05</td>";
          echo "<td>$email</td>";
          echo "<td>$conf_num</td>";
          echo "<td>$dept</td>";echo "<td>$q03</td>";
          echo "</tr>";
     }
     echo "</table>";
}
else {
     // no
     // print status message
     echo "No rows found!";
}  

 
 
    } else {
?>

   <p>Your survey could not be added to the database.";

<?php
    }
?>

_______________________________________________________________________________________
0
 
RoonaanCommented:
The part below is somewhat messy:
   $result = mysql_query($query);
?>
//    if ($result) {
//?>
  <p>Your survey was added to the database ...<br />
  //<a href=\"http://survey.yourcompany.com/?view=results">view results here</a></p>
<?php
 

<?php  

Try changing it to:

   $result = mysql_query($query);
   if ($result) {
    echo '<p>Your survey was added to the database ...<br />';

Also removing the two <?php's

-r-
0
 
Harisha M GCommented:
Try this:


<?php
      echo '<form method="POST" action="process.php">';

      $MySQL_Server   = "localhost";
      $MySQL_User     = "root";
      $MySQL_Password = "root";
      $MySQL_Database = "survey_questions";
      $MySQL_Table    = "survey_1";

      mysql_connect("$MySQL_Server", "$MySQL_User", "$MySQL_Password");
      mysql_select_db("$MySQL_Database");

      $query = "SELECT * FROM $MySQL_Table ORDER BY question_number ASC";
      $result = mysql_query($query);

      if ($result)
      {
            while ($r = mysql_fetch_array($result))
            {
                  $ID                = $r["ID"];
                  $question_number   = $r["question_number"];
                  $question_text     = $r["question_text"];
                  $yes_or_no         = $r["yes_or_no"];
                  $pull_down_text    = $r["pull_down_text"];
                  $options           = $r["options"];

                  // take out slashes in entry

                  $question_text = stripslashes ($question_text);

                  // show questions from database

                  echo "$question_number.) $question_text<br><br>";

                  // if *not* Yes or No, replace && and &

                  if ($yes_or_no == "NO")
                  {

                  // begin replacement

                        $options = preg_replace ('/&([^%]+)%\ &&([^%]+)%%/',
                                                '<option value="\\1">\\2</option>', $options);

                  // now show HTML with imbedded PHP to create drop down menus

?>
<select size="1" name="q<?php echo $question_number; ?>">
<option selected><?php echo $pull_down_text; ?></option>
<option> ------ </option>
<?php echo $options; ?>
</select>
<br /><br />

<?php
            // if Yes or NO produce radio buttons

                  }
                  else if ($yes_or_no == "YES")
                  {
?>
&nbsp;<input type="radio" name="q<?=$question_number?>" value="YES">YES<br/>
&nbsp;<input type="radio" name="q<?=$question_number?>" value="NO">NO<br/>
&nbsp;<input type="radio" checked
            name="q<?=$question_number?>" value="NA">N/A<br/>
<br />
<?php
                  }
            }
      
// end of questions
?>
<input type="submit" value="Submit Survey" name="submit">
</form>
<?php
      }
      else
      {
            echo "No data.";    
      }

      mysql_free_result($result);

?>
____________________________ process.php  _____________________________________________

<?php

      // Connect to DB here  

      $MySQL_Server   = "localhost";
      $MySQL_User     = "root";
      $MySQL_Password = "root";
      $MySQL_Database = "survey_questions";
      $MySQL_Table    = "survey_1_results";

      mysql_connect($MySQL_Server, $MySQL_User, $MySQL_Password);
      mysql_select_db($MySQL_Database);

      $query = "
      INSERT INTO $MySQL_Table
      VALUES ('$q01', '$q02', '$q03', '$q04', '$q05',
      '$email', '$conf_num', '$dept','')
      ";

      $result = mysql_query($query);
?>
      //    if ($result) {
      //?>
      <p>Your survey was added to the database ...<br />
      //<a href=\"http://survey.yourcompany.com/?view=results">view results here</a></p>
<?php
      $query = 'SELECT * FROM '.$MySQL_Table.' WHERE ID = '.mysql_insert_id().' DESC LIMIT 1';
      $result = mysql_query($query);



      if (mysql_num_rows($result) > 0)
      {
            // yes
            // print them one after another
            echo "<table cellpadding=10 border=1>";
            while(list($q01, $q02, $q03,$q04, $q05,$email,$conf_num,$dept) = mysql_fetch_row($result))
            {
                  echo "<tr>";
                  echo "<td>$q01</td>";
                  echo "<td>$q02</td>";
                  echo "<td>$q03</td>";
                  echo "<td>$q04</td>";
                  echo "<td>$q05</td>";
                  echo "<td>$email</td>";
                  echo "<td>$conf_num</td>";
                  echo "<td>$dept</td>";echo "<td>$q03</td>";
                  echo "</tr>";
            }
            echo "</table>";
      }
      else
      {
            // no
            // print status message
            echo "No rows found!";
?>

      <p>Your survey could not be added to the database.";

<?php
      }
?>
0
 
dutaAuthor Commented:
TO:  mgh_mgharish and Roonaan:

Thank you again for your very kind, prompt response. I am so sorry to respond as fast. I had to go to bed after posting my last comment.

I tried as you kindly suggested, but it still did not work. The biggest problem is that the response to the question is not put into MYSql table (survey_1_results).

Hope you experts may figure out why. I will keep trying to figure out by myself too.

Thanks!

duta
Sunday at 12:32 p.m.


0
 
RoonaanCommented:
You can show the (possible) mysql error by changing to:

$result = mysql_query($query) or die(mysql_error());

This is useful for debugging purposes only.

-r-
0
 
dutaAuthor Commented:
TO: Roonaan:

You guys are just awesome. You are doing your best to help me out. It is just great!

By the way, I modified the script and clicked the Submit button.

I got a response as follows, but nothing was inserted into MYSQL table("survey_1_results").

Thanks again!

_______________________ What is shown on the screen after clicking Submit button _______________
Your survey was added to the database ...
//view results here

No rows found!
Your survey could not be added to the database.";

_______________________________________________________________________________________
0
 
RoonaanCommented:
Did you add the 'or die(mysql_error())' also to the topmost mysql_query line?

-r-
0
 
dutaAuthor Commented:
TO: Roonaan:

Thanks again for your kind response. I am writing this comment upon reading your message. I was out most of the day for workout.

Yes, I put the "or die . . ." all mysql_query line.

The problem seems to be that MySql table is not taking responses to the survey questions.

Thanks again!

duta
Monday (May 22, 2006) at 12:00 a.m.
0
 
dutaAuthor Commented:
TO Everyone:

I got the survey scripts from "http://www.zend.com/zend/tut/tutorial-jenkins.php?print=1".
Hope that the information may be of any help to you in helping me out.

Thanks!

duta
Monday at 12:24 a.m.
0
 
Khanh DoanSenior DeveloperCommented:
 $query = "
        INSERT INTO $MySQL_Table
        VALUES ('$q01', '$q02', '$q03', '$q04', '$q05',
                '$email', '$conf_num', '$dept','')
    ";

in your process.php
when you run script, the query will be

INSERT INTO survey_1_results
        VALUES ('', '', '', '', '',
                '', '', '','')

because you didn't var $q01. I think it should be

$q01 = 'something here';
....
$dept = 'something here';

Sorry for my bad english.

Bonmat86
0
 
dutaAuthor Commented:
TO: bonmat86:

Thank you so much for your kind response.

Sorry to tell you, but  mgh_mgharish's tip worked fine. So I have to accept his (very energetic) tip as an accepting solution.

Hope you may come out again to help me in the (very near) future (I have tons of questions to ask).

duta
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now