Please help with survey data collecting php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KT83
Forum Newbie
Posts: 5
Joined: Thu Jun 11, 2009 12:31 am

Please help with survey data collecting php

Post by KT83 »

Hi, I've created an online survey and am trying to gather the results in a database and display them. The current version of the survey I have is working. When someone takes the survey, the results are sent to my email. I'm posting all the new information I've put together, although I'm not sure if I've put them together correctly.

Below is what I have so far. This is just a short sample survey with only 2 questions. I need to know a few things:

1) when do I add the code for connecting to the database?
2) what else am I missing?
3) how are these codes related to the survey I already have online. Do they go in the same file?
4) how are these codes related to the "survey results" Is that a separate php file?

I hope someone can help. Thanks!

Where I learned this tutorial from: http://www.phpbuilder.com/columns/patte ... 51109.php3

<?php


/////////////////////////////////////////////////////////////
Since we're now connected, we need to define some values so that "male", "female", "email" and "phone" aren't literal values, but rather numbers that represent the values. We'll do that by setting up a couple of arrays to reference.
/////////////////////////////////////////////////////////////

$__gender = Array('male' => '0', 'female' => '1');
$__age = Array('under-20' => '0', '20-29' => '1', '30-39' => '2', '40-49' => '3', '50+' => '4');





/////////////////////////////////////////////////////////////
At this point we're ready for the long part. It's not hard, just extensive. First we need to set up variables to reference in the sql statement. We will create 41 variables, and give them a value of 1 or 0. I'll use the ternary operator to decide the value. If you don't know what the ternary operator is, it's a very short way of saying "if this, than x, else y". Here's how it is set up: if (expression)?true:false; Let's set up our variables:
/////////////////////////////////////////////////////////////

$male = ($__gender[$_POST['gender']] == '0')?'1':'0';
$female = ($__gender[$_POST['gender']] == '1')?'1':'0';
$under-20 = ($__age[$_POST['age']] == '0')?'1':'0';
$20-29 = ($__age[$_POST['age']] == '1')?'1':'0';
$30-39 = ($__age[$_POST['age']] == '2')?'1':'0';
$40-49 = ($__age[$_POST['age']] == '3')?'1':'0';
$50+ = ($__age[$_POST['age']] == '4')?'1':'0';





/////////////////////////////////////////////////////////////
We can execute a huge SQL statement that adds 1 or 0 to the value of the column, thus incrementing it, (or not based upon what the user submitted). The incrementation is determined via the lines above. Let's look at the SQL statement:
/////////////////////////////////////////////////////////////

$sql2 = "UPDATE `onlinesurvey_totals`
SET male=male+".$male.", female=female+".female.", under-20=under-20+".under-20.", 20-29=20-29+".20-29.", 30-39=30-39+".30-39.", 40-49=40-49+".40-49.", 50+=50++".50+.";

$rslt2=mysqli_query($dbc, $sql2);
if(!$rslt2)
{
die('<span style="color: #F00; font-weight: bold;">Error!</span><br />
There was an error processing your request. mySQL returned the following:<br /><br />'.
mysqli_errno($dbc).'<br />'.mysqli_error($dbc));
}
else
{


?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Please help with survey data collecting php

Post by califdon »

I think you need a tutorial, not specific answers to coding problems, which is what this forum is for. I suggest:
http://dev.mysql.com/tech-resources/art ... index.html
http://www.freewebmasterhelp.com/tutorials/phpmysql
KT83
Forum Newbie
Posts: 5
Joined: Thu Jun 11, 2009 12:31 am

Re: Please help with survey data collecting php

Post by KT83 »

I think my 4 questions are pretty specific... but i'll check out the links thanks
Post Reply