I reread my post and I realize it sounds like I was bashing Patterson's post. What I meant to say is I followed all his instructions to create all the tables and codes but there's one crucial step that's not included in the post and that is the final product, the full source codes.
I'll post what I have, and you can tell what else I need to add to make this file ready for collecting survey answers. The database is already set up with the appropriate tables and fields.
Hope someone can help! Thanks!
SOURCE: http://www.phpbuilder.com/columns/patterson20051109.php3
<?php
/////////////Connecting to the Database/////////////////////
$dbc = mysqli_connect("localhost", "username", "password", "db_name");
if(!$dbc)
{
die('<span style="color: #F00; font-weight: bold;">Error!</span><br />
There was error connecting. mySQL returned the following:<br /><br />'.
mysqli_connect_errno().'<br />'.mysqli_connect_error());
}
////////////////////////////////
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.
////////////////////////////////
$g = Array('male' => '0', 'female' => '1');
$a = Array('0_8' => '0', '9_16' => '1', '17_24' => '2', '25_32' => '3', '33_40' =>
'4', '41_x' => '5');
$st = Array('email' => '0', 'phone' => '1');
$k = Array('little' => '0', 'some' => '1', 'lots' => '2', 'expert' => '3');
$__r = Array('yes' => '0', 'no' => '1');
//////////////////////////////////////////////
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 = ($g[$POST['gender']] == '0')?'1':'0';
$female = ($g[$POST['gender']] == '1')?'1':'0';
$8 = ($a[$POST['age']] == '0')?'1':'0';
$16 = ($a[$POST['age']] == '1')?'1':'0';
$24 = ($a[$POST['age']] == '2')?'1':'0';
$32 = ($a[$POST['age']] == '3')?'1':'0';
$40 = ($a[$POST['age']] == '4')?'1':'0';
$41 = ($a[$POST['age']] == '5')?'1':'0';
$email = ($st[$POST['suptype']] == '0')?'1':'0';
$phone = ($st[$POST['suptype']] == '1')?'1':'0';
$little = ($k[$POST['knowledge']] == '0')?'1':'0';
$some = ($k[$POST['knowledge']] == '1')?'1':'0';
$lots = ($k[$POST['knowledge']] == '2')?'1':'0';
$exprt = ($k[$POST['knowledge']] == '3')?'1':'0';
$1vd = ($POST['speed'] == '0')?'1':'0';
$1d = ($POST['speed'] == '1')?'1':'0';
$1s = ($POST['speed'] == '2')?'1':'0';
$1vs = ($POST['speed'] == '3')?'1':'0';
$1n = ($POST['speed'] == '4')?'1':'0';
$2vd = ($POST['speed'] == '0')?'1':'0';
$2d = ($POST['speed'] == '1')?'1':'0';
$2s = ($POST['speed'] == '2')?'1':'0';
$2vs = ($POST['speed'] == '3')?'1':'0';
$2n = ($POST['speed'] == '4')?'1':'0';
$3vd = ($POST['repknowledge'] == '0')?'1':'0';
$3d = ($POST['repknowledge'] == '1')?'1':'0';
$3s = ($POST['repknowledge'] == '2')?'1':'0';
$3vs = ($POST['repknowledge'] == '3')?'1':'0';
$3n = ($POST['repknowledge'] == '4')?'1':'0';
$4y = ($POST['recommend'] == 'yes')?'1':'0';
$4n = ($POST['recommend'] == 'no')?'1':'0';
$5vd = ($POST['result'] == '0')?'1':'0';
$5d = ($POST['result'] == '1')?'1':'0';
$5s = ($POST['result'] == '2')?'1':'0';
$5vs = ($POST['result'] == '3')?'1':'0';
$5n = ($POST['result'] == '4')?'1':'0';
$6nvp = ($POST['overall'] == '0')?'1':'0';
$6sp = ($POST['overall'] == '1')?'1':'0';
$6p = ($POST['overall'] == '2')?'1':'0';
$6vp = ($POST['overall'] == '3')?'1':'0';
$6ep = ($POST['overall'] == '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.", 0_8=0_8+".$8.", 9_16=9_16+".$16.",
17_24=17_24+".$24.", 25_32=25_32+".$32.", 33_40=33_40+".$40.", 41_x=41_x+".$41.",
email=email+".$email.", phone=phone+".$phone.", little=little+".$little.", some=some+".
$some.",a_lot=a_lot+".$lots.", expert=expert+".$exprt.", 1_vd=1_vd+".$1vd.", 1_d=1_d+".$1d.",
1_s=1_s+".$1s.", 1_vs=1_vs+".$1vs.", 1_n=1_n=".$1n.", 2_vd=2_vd+".$2vd.", 2_d=2_d+".$2d.",
2_s=2_s+".$2s.", 2_vs=2_vs+".$2vs.", 2_n=2_n=".$2n.", 3_vd=3_vd+".$3vd.", 3_d=3_d+".$3d.",
3_s=3_s+".$3s.", 3_vs=3_vs+".$3vs.", 3_n=3_n=".$3n.", 4_yes=4_yes+".$4y.", 4_no=4_no+".$4n.",
5_vd=5_vd+".$5vd.", 5_d=5_d+".$5d.", 5_s=5_s+".$5s.", 5_vs=5_vs+".$5vs.", 5_n=5_n=".$5n.",
6_nvp=6_nvp+".$6nvp.", 6_sp=6_sp+".$6sp.", 6_p=6_p+".$6p.", 6_vp=6_vp+".$6vp.",
6_ep=6_ep+".$_6ep;
$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
{
?>