Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value Increment

Status
Not open for further replies.

overyde

Programmer
Joined
May 27, 2003
Messages
226
Location
ZA
Hi,
I have a voting page that users need to vote on dofferent aspects on one page. My pronblem is that I need to increment the value in the database by 1. I can call the value from the database and increase the respective value by 1 but I don't know how to repost it?
The code for the voting page and the code for the process follows:

FRATELLI.PHP (VOTING PAGE)
*******************************************************
<html>

<head>
<title>New Page 1</title>
</head>

<body>
<form method=&quot;POST&quot; action=&quot;addvote.php&quot;>

<table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;border-collapse: collapse&quot; bordercolor=&quot;#111111&quot; width=&quot;100%&quot;>
<tr>
<td width=&quot;20%&quot; colspan=&quot;2&quot;>fratelli</td>
<td width=&quot;20%&quot; colspan=&quot;2&quot;> </td>
</tr>
<tr>
<td width=&quot;20%&quot; colspan=&quot;2&quot;>service</td>
<td width=&quot;20%&quot; colspan=&quot;2&quot;>management</td>
</tr>
<tr>
<td width=&quot;10%&quot;>Excellent</td>
<td width=&quot;10%&quot;>

<input type=&quot;radio&quot; value=&quot;servicea&quot; name=&quot;service&quot;></td>
<td width=&quot;10%&quot;>Excellent</td>
<td width=&quot;10%&quot;>

<input type=&quot;radio&quot; value=&quot;managementa&quot; name=&quot;management&quot;></td>
</tr>
<tr>
<td width=&quot;10%&quot;>Good</td>
<td width=&quot;10%&quot;>

<input type=&quot;radio&quot; value=&quot;serviceb&quot; name=&quot;service&quot;></td>
<td width=&quot;10%&quot;>Good</td>
<td width=&quot;10%&quot;>

<input type=&quot;radio&quot; value=&quot;managementb&quot; name=&quot;management&quot;></td>
</tr>
<tr>
<td width=&quot;10%&quot;>Average</td>
<td width=&quot;10%&quot;>

<input type=&quot;radio&quot; value=&quot;servicec&quot; name=&quot;service&quot;></td>
<td width=&quot;10%&quot;>Average</td>
<td width=&quot;10%&quot;>

<input type=&quot;radio&quot; value=&quot;managementc&quot; name=&quot;management&quot;></td>
</tr>
<tr>
<td width=&quot;10%&quot;>Poor</td>
<td width=&quot;10%&quot;>

<input type=&quot;radio&quot; value=&quot;serviced&quot; name=&quot;service&quot;></td>
<td width=&quot;10%&quot;>Poor</td>
<td width=&quot;10%&quot;>

<input type=&quot;radio&quot; value=&quot;managementd&quot; name=&quot;management&quot;></td>
</tr>
<tr>
<td width=&quot;10%&quot;><input type=&quot;submit&quot; value=&quot;Submit&quot;></td>
<td width=&quot;10%&quot;><input type=&quot;reset&quot; value=&quot;Reset&quot;></td>
<td width=&quot;10%&quot;> </td>
<td width=&quot;10%&quot;> </td>
</tr>
</table>
</form
</body>

</html>

ADDVOTE.PHP
**********************************************************
<?php
session_start();

//check for required fields from the form
if ((!$_POST[service]) || (!$_POST[management])) {
header(&quot;Location: fratelli.php&quot;);
exit;
}

//connect to server and select database
$conn = mysql_connect(&quot;localhost&quot;,&quot;&quot;,&quot;&quot;)
or die(mysql_error());
mysql_select_db(&quot;imperial&quot;,$conn) or die(mysql_error());

//create and issue the query
$sql = &quot;select servicea, serviceb, servicec, serviced, managementa, managementb, managementc, managementd from place&quot;;
$result = mysql_query($sql,$conn) or die(mysql_error());

//get the values of f_name l_name and other details
$servicea = mysql_result($result, 0, 'servicea');
$serviceb = mysql_result($result, 0, 'serviceb');
$servicec = mysql_result($result, 0, 'servicec');
$serviced = mysql_result($result, 0, 'serviced');
$managementa = mysql_result($result, 0, 'managementa');
$managementb = mysql_result($result, 0, 'managementb');
$managementc = mysql_result($result, 0, 'managementc');
$managementd = mysql_result($result, 0, 'managementd');

//Adding 1 to respective field
if ($_POST[service]== &quot;servicea&quot;)
{
$servicea++;
}
elseif ($_POST[service]== &quot;serviceb&quot;)
{
$serviceb++;
}
elseif ($_POST[service]== &quot;servicec&quot;)
{
$servicec++;
}
elseif ($_POST[service]== &quot;serviced&quot;)
{
$serviced++;
}

if ($_POST[management]== &quot;managementa&quot;)
{
$managementa++;
}
elseif ($_POST[management]== &quot;managementb&quot;)
{
$managementb++;
}
elseif ($_POST[management]== &quot;managementc&quot;)
{
$managementc++;
}
elseif ($_POST[management]== &quot;managementd&quot;)
{
$managementd++;
}

// create the SQL statement
$sql = &quot;UPDATE place SET servicea = '$servicea', serviceb = '$serviceb', servicec = '$servicec', serviced = '$serviced' where user = 'wazza'&quot;;


//prepare message for printing, and user menu
$msg = &quot;<p><b>Service</b></p>&quot;;
$msg .= &quot;<p>Excellent: $servicea</p>&quot;;
$msg .= &quot;<p>Good: $serviceb</p>&quot;;
$msg .= &quot;<p>Average: $servicec</p>&quot;;
$msg .= &quot;<p>Poor: $serviced</p>&quot;;

$msg .= &quot;<p><b>Management</b></p>&quot;;
$msg .= &quot;<p>$managementa</p>&quot;;
$msg .= &quot;<p>$managementb</p>&quot;;
$msg .= &quot;<p>$managementc</p>&quot;;
$msg .= &quot;<p>$managementd</p>&quot;;




?>
<html>

<head>
<link rel=&quot;STYLESHEET&quot; type=&quot;text/css&quot; href=&quot;style.css&quot;>
<title>Thank-You For Voting</title>
</head>

<body>

<? print &quot;$msg&quot;; ?>
</body>
</html>

Reality is built on a foundation of dreams.
 
You are using words For measuring your site righrt?
For example! You right inside the db the word servicea for Good and serviceb for something else!?
and in the db
you send this code

$sql = &quot;UPDATE place SET
servicea = '$servicea',
serviceb = '$serviceb',
servicec = '$servicec',
serviced = '$serviced'
where user = 'wazza'&quot;;

this means that you must have 5 vars and 5 DBrows

You must give everytime vars servicea to d but the user just click on one! Dot so everytime you pass one from the above vars! so probably the db is not being updated cause one of the fields is missing!

Juyst check this and tell me if its right!
If you want any help just post :)
 
Okay,
In the database, servicea has a number
(the number of times servicea has been voted-
servicea being the &quot;excellent&quot; value
serviceb being the &quot;good&quot; value etc)

So say the database looks like this:
user * servicea * serviceb * servicec * serviced *
**************************************************
wazza* 1 * 5 * 3 * 2 *
**************************************************

I need to increment the value of the respective choice and then display it as a percentage.
i.e. servicea = servicea / (servicea + serviceb + servicec + serviced) * 100

My problem is the increment works but I can't write the value to the database.

P.S. Please ignore the management section as I did not finish writing the code and am just testing the service section first.

Reality is built on a foundation of dreams.
 
Ok so please tell me what the rows represent again
so I can figure it out the math relation betwen them so to show you the 100%

In order to increase a number it would be wize 1st to read it from DB then add one ($data_collected++;) and then UPDATE it to db! while you do that put some echos so that you can check out if this is working.

 
That's EXACTLY what I want to do!!!! I need to know how to read it from the db then add one then update it whilst seeing if it's working. That's where my problem is lying!

Each row is a seperate user that has an establishment.
the first column is the user.
the second column = servicea which represents excellent
the third column = serviceb which represents good
the fourth column = servicec which represents average
the fifth column = serviced which represents poor

the data in these field values are numbers.

By the way-thanks for your time :)

Reality is built on a foundation of dreams.
 
Do something like that

When the user submits a good vote or something like that use this

$query = &quot;SELECT * FROM table WHERE user = '$user'&quot;;
$result = mysql_query($query,$db) or die(mysql_error());
$data = mysql_fetch_array($result, MYSQL_ASSOC);
so now you have $data[row]
and you create the increasment
$data[row]++;
$sql = &quot;UPDATE place SET servicea = '$servicea', serviceb = '$serviceb', servicec = '$servicec', serviced = '$serviced' where user = 'wazza'&quot;;


Now check this out you can change anything for example
you want to change servicea
do before update
$data[servicea]++;
but head in mind beware of the updates and the db tables
It is better to use id_num which is uniq! and play with this

Sorry but I do now now excacly the structure and the php confs so I am tring to help :))

Hope it helps
 
Just can't get this to work.
Ideally I'd like to get my original script to work as this would be ideal. I just need it write to the dbase.

Reality is built on a foundation of dreams.
 
Ok can you post all the php and html file so I can look at them!

Just paste them both so I can create them and play with them alittle :)
 
They are both on the top of the forum in the first posting.
They are called fratelli.php and addvote.php

thks
;)

Reality is built on a foundation of dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top