Forums
This topic is locked
Adding Fields together in a db
Posted 04 Apr 2003 17:48:08
1
has voted
04 Apr 2003 17:48:08 Greg LeBreck posted:
I have an Access DB which I will turn into a MYSQL db. It has a key id field which is an auto number.The other fields are p1, p2, p3. They are currency fields. I want to add a colum called total_p. And I would like that filed to give me the total of p1,p2,p3. Is this possible?
Replies
Replied 04 Apr 2003 20:19:36
04 Apr 2003 20:19:36 Brent Colflesh replied:
Perhaps easier to use SUM in your query for displaying the totals dynamically - rather than having to run some UPDATE everytime you want the latest total - assuming you keep adding new records.
www.mysql.com/doc/en/Group_by_functions.html#IDX1370
Regards,
Brent
www.mysql.com/doc/en/Group_by_functions.html#IDX1370
Regards,
Brent
Replied 04 Apr 2003 20:22:41
04 Apr 2003 20:22:41 Greg LeBreck replied:
That's the problem. I don't know how to say take the sum of p1, p2, p3 and then put it in this file.
Replied 04 Apr 2003 21:43:32
04 Apr 2003 21:43:32 Brent Colflesh replied:
I'm saying there is no reason to put a sum field in your db - use the SUM function in your MySQL query to get the SUM of the selected fields.
Regards,
Brent
Regards,
Brent
Replied 04 Apr 2003 21:47:52
04 Apr 2003 21:47:52 Greg LeBreck replied:
I got ya there. It's just I dont understand how to wite it.
Replied 04 Apr 2003 22:44:59
04 Apr 2003 22:44:59 Brent Colflesh replied:
SELECT id, p1+p2+p3 AS total_p FROM myTable ORDER BY total_p ASC
-or-
SELECT id, SUM(p1+p2+p3) AS total_p FROM myTable GROUP BY id ORDER BY total_p ASC
Regards,
Brent
-or-
SELECT id, SUM(p1+p2+p3) AS total_p FROM myTable GROUP BY id ORDER BY total_p ASC
Regards,
Brent
Replied 27 Nov 2006 15:28:55
27 Nov 2006 15:28:55 Jeremy Hill replied:
i to have a this issue (not knowing the right syntax to use) i have a table that has the fields nname, authid, email, amount, and date... i would like to have a query on a php page that displays total amounts per authid which is then associated with the nname so if i have 6 rows in the table consisting of this info ....
+-------------------------------------------------------------------+
|nname | authid | email | amount | date |
| |
|Mike 3343 20.00 11/27/2006 |
|Mark 3342 10.00 11/26/2006 |
|Mike 3343 20.00 11/12/2006 |
|Mark 3342 20.00 11/22/2006 |
|Mike 3343 20.00 11/18/2006 |
|Mike 3343 20.00 11/09/2006 |
+-------------------------------------------------------------------+
it would display something like this ...
Mike (SteamID 3343) Has donated $80.00 since 11/09/2006 {this date being the oldest date of entry}
Mark (SteamID 3342) Has donated $30.00 since 11/22/2006
i would also like to have it display the total donations in a line kinda like this ...
Total donations this month $110.00 { where the sql query displays the sum of all donations for the current month}
Edited by - archangel2006 on 27 Nov 2006 18:24:57
+-------------------------------------------------------------------+
|nname | authid | email | amount | date |
| |
|Mike 3343 20.00 11/27/2006 |
|Mark 3342 10.00 11/26/2006 |
|Mike 3343 20.00 11/12/2006 |
|Mark 3342 20.00 11/22/2006 |
|Mike 3343 20.00 11/18/2006 |
|Mike 3343 20.00 11/09/2006 |
+-------------------------------------------------------------------+
it would display something like this ...
Mike (SteamID 3343) Has donated $80.00 since 11/09/2006 {this date being the oldest date of entry}
Mark (SteamID 3342) Has donated $30.00 since 11/22/2006
i would also like to have it display the total donations in a line kinda like this ...
Total donations this month $110.00 { where the sql query displays the sum of all donations for the current month}
Edited by - archangel2006 on 27 Nov 2006 18:24:57
Replied 29 Nov 2006 18:53:11
29 Nov 2006 18:53:11 Vince Baker replied:
you need to use the group sql command:
select authid, sum(amount) as sum_amount
from yourtable
group by authid
this will give you the sum of the amounts for each authid. You must query the other info seperately.
Regards
Vince Baker
<strong>DMX Zone Manager</strong>
[VBScript | ASP | HTML | CSS | SQL | Oracle | AS400 | ERP Logic | Hosting]
select authid, sum(amount) as sum_amount
from yourtable
group by authid
this will give you the sum of the amounts for each authid. You must query the other info seperately.
Regards
Vince Baker
<strong>DMX Zone Manager</strong>
[VBScript | ASP | HTML | CSS | SQL | Oracle | AS400 | ERP Logic | Hosting]
Replied 29 Nov 2006 22:57:31
29 Nov 2006 22:57:31 Jeremy Hill replied:
ok im still not quite sure what you mean .. here is what i have so far ...
this is the form that a user will use to donate
donate.php
<pre id=code><font face=courier size=2 id=code>
<form action="donate_add.php" method="post">
<table width="343" height="348" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="339" height="348"> <p align="center"><font size="4">Please Fill
in the Fields to Make a Donation.</font></p>
<table width="258" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="114"><div align="left"><strong>SteamID: </strong></div></td>
<td width="144"><div align="center">
<input name="authid" type="text" id="authid">
</div></td>
</tr>
<tr>
<td><div align="left"><strong>Nick Name: </strong></div></td>
<td><div align="center">
<input name="nname" type="text" id="nname">
</div></td>
</tr>
<tr>
<td><div align="left"><strong>Email Address: </strong></div></td>
<td><div align="center">
<input name="email" type="text" id="email">
</div></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><strong>Amount:</strong></td>
<td><input name="amount" type="text" id="amount"></td>
</tr>
</table>
<p>
<label> </label>
<center><input type="hidden" value="<?php
$date = mktime(0,0,0,date("m",date("d",date("Y");
echo "".date("m/d/Y", $date);
?>" name="date" id="date"></center><br>
<div align="center"><input type="submit" name="Submit" value="Submit"></div>
</p>
<br></td>
</tr>
</table>
</form>
</font id=code></pre id=code>
This is the page that adds the info to the database and allows for edit (on another page)
donate_add.php
<pre id=code><font face=courier size=2 id=code>
<?php
include 'config.php';
$db = mysql_connect ($hostname, $username, $password) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db($database);
$query = "INSERT INTO $donate_table (authid,nname,email,amount,date) VALUES ('" . mysql_real_escape_string($_POST['authid']) . "','" . mysql_real_escape_string($_POST['nname']) . "','" . mysql_real_escape_string($_POST['email']) . "','" . mysql_real_escape_string($_POST['amount']) . "','" . mysql_real_escape_string($_POST['date']) . "')";
mysql_query($query) or die("Query failed " . mysql_error());
mysql_close($db);
$authid = $_REQUEST['authid'];
$nname = $_REQUEST['nname'];
$email = $_REQUEST['email'];
$amount = $_REQUEST['amount'];
$date = $_REQUEST['date'];
echo "<center>";
echo "<p align=\"center\"><font size=\"5\">Is this information Correct?</font></p>";
echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo "<tr>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>SteamID</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>Nick Name</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>Email</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>Amount</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>Date</strong></font></div></td>";
echo "</tr>";
echo "<tr>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$authid</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$nname</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$email</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$amount</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$date</strong></font></div></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
echo "<form action=\"www.paypal.com/cgi-bin/webscr\" method=\"post\">";
echo "<input type=\"hidden\" name=\"cmd\" value=\"_xclick\">";
echo "<input type=\"hidden\" name=\"business\" value=\" \">";
echo "<input type=\"hidden\" name=\"item_name\" value=\"Test Name\">";
echo "<input type=\"hidden\" name=\"item_number\" value=\"Test ID\">";
echo "<input type=\"hidden\" name=\"amount\" value=\"$amount\">";
echo "<input type=\"hidden\" name=\"page_style\" value=\"Primary\">";
echo "<input type=\"hidden\" name=\"no_shipping\" value=\"1\">";
echo "<input type=\"hidden\" name=\"return\" value=\"successpay.com\">";
echo "<input type=\"hidden\" name=\"cancel_return\" value=\"cancelpay.com\">";
echo "<input type=\"hidden\" name=\"no_note\" value=\"1\">";
echo "<input type=\"hidden\" name=\"currency_code\" value=\"USD\">";
echo "<input type=\"hidden\" name=\"tax\" value=\"0\">";
echo "<input type=\"hidden\" name=\"lc\" value=\"US\">";
echo "<input type=\"hidden\" name=\"bn\" value=\"PP-DonationsBF\">";
echo "<br><br><br><center>";
echo "<input type=\"submit\" name=\"submit\" value=\"Yes-Continue Donation\"> ";
echo "</form>";
echo "<td><form action=\"donate_edit.php?authid=$authid\" method=\"post\"><input type=\"submit\" value=\"No-Go Back\"></form></td>";
echo "</center>";
?>
</font id=code></pre id=code>
and this is page that i want the totals in another column (Total Donations)
donate_view.php
<pre id=code><font face=courier size=2 id=code>
<?php
include 'config.php'; //is where my connection info is stored
include 'access.php';// is where my access levels are stored for other information i plan to use here
$db = mysql_connect ($hostname, $username, $password) or die ('Failed to connect to database: ' . mysql_error());
mysql_select_db($database);
$query = "SELECT * FROM $donate_table";
$result = mysql_query($query) or die ("Cannot query table " . mysql_error());
?>
</td>
</tr>
<tr>
<td align="left"> <table width="100%" border="1" bordercolor="#000000">
<tr bgcolor="#000000">
<td><div align="center"><font size="4">Name</font></div></td>
<td><div align="center"><font size="4">Amount</font></div></td>
<td><div align="center"><font size="4">Date</font></div></td>
<td><div align="center"><font size="4">Total Donations</font></div></td>
</tr>
<tr bgcolor="#666666">
<td>
<?php
while ($row = mysql_fetch_assoc($result))
{
$nname = $row['nname'];
echo "<strong>$nname</strong><br>";
echo "</td><td>";
$amount = $row['amount'];
echo "<strong>$amount</strong><br>";
echo "</td><td>";
$date = $row['date'];
echo "<strong>$date</strong><br>";
$total = $row['total'];
echo "<strong>$total</strong><br>";
}
mysql_free_result($result);
?>
</font id=code></pre id=code>
this is the form that a user will use to donate
donate.php
<pre id=code><font face=courier size=2 id=code>
<form action="donate_add.php" method="post">
<table width="343" height="348" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="339" height="348"> <p align="center"><font size="4">Please Fill
in the Fields to Make a Donation.</font></p>
<table width="258" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="114"><div align="left"><strong>SteamID: </strong></div></td>
<td width="144"><div align="center">
<input name="authid" type="text" id="authid">
</div></td>
</tr>
<tr>
<td><div align="left"><strong>Nick Name: </strong></div></td>
<td><div align="center">
<input name="nname" type="text" id="nname">
</div></td>
</tr>
<tr>
<td><div align="left"><strong>Email Address: </strong></div></td>
<td><div align="center">
<input name="email" type="text" id="email">
</div></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><strong>Amount:</strong></td>
<td><input name="amount" type="text" id="amount"></td>
</tr>
</table>
<p>
<label> </label>
<center><input type="hidden" value="<?php
$date = mktime(0,0,0,date("m",date("d",date("Y");
echo "".date("m/d/Y", $date);
?>" name="date" id="date"></center><br>
<div align="center"><input type="submit" name="Submit" value="Submit"></div>
</p>
<br></td>
</tr>
</table>
</form>
</font id=code></pre id=code>
This is the page that adds the info to the database and allows for edit (on another page)
donate_add.php
<pre id=code><font face=courier size=2 id=code>
<?php
include 'config.php';
$db = mysql_connect ($hostname, $username, $password) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db($database);
$query = "INSERT INTO $donate_table (authid,nname,email,amount,date) VALUES ('" . mysql_real_escape_string($_POST['authid']) . "','" . mysql_real_escape_string($_POST['nname']) . "','" . mysql_real_escape_string($_POST['email']) . "','" . mysql_real_escape_string($_POST['amount']) . "','" . mysql_real_escape_string($_POST['date']) . "')";
mysql_query($query) or die("Query failed " . mysql_error());
mysql_close($db);
$authid = $_REQUEST['authid'];
$nname = $_REQUEST['nname'];
$email = $_REQUEST['email'];
$amount = $_REQUEST['amount'];
$date = $_REQUEST['date'];
echo "<center>";
echo "<p align=\"center\"><font size=\"5\">Is this information Correct?</font></p>";
echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo "<tr>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>SteamID</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>Nick Name</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>Email</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>Amount</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"3\"><strong>Date</strong></font></div></td>";
echo "</tr>";
echo "<tr>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$authid</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$nname</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$email</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$amount</strong></font></div></td>";
echo "<td><div align=\"center\"><font size=\"2\"><strong>$date</strong></font></div></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
echo "<form action=\"www.paypal.com/cgi-bin/webscr\" method=\"post\">";
echo "<input type=\"hidden\" name=\"cmd\" value=\"_xclick\">";
echo "<input type=\"hidden\" name=\"business\" value=\" \">";
echo "<input type=\"hidden\" name=\"item_name\" value=\"Test Name\">";
echo "<input type=\"hidden\" name=\"item_number\" value=\"Test ID\">";
echo "<input type=\"hidden\" name=\"amount\" value=\"$amount\">";
echo "<input type=\"hidden\" name=\"page_style\" value=\"Primary\">";
echo "<input type=\"hidden\" name=\"no_shipping\" value=\"1\">";
echo "<input type=\"hidden\" name=\"return\" value=\"successpay.com\">";
echo "<input type=\"hidden\" name=\"cancel_return\" value=\"cancelpay.com\">";
echo "<input type=\"hidden\" name=\"no_note\" value=\"1\">";
echo "<input type=\"hidden\" name=\"currency_code\" value=\"USD\">";
echo "<input type=\"hidden\" name=\"tax\" value=\"0\">";
echo "<input type=\"hidden\" name=\"lc\" value=\"US\">";
echo "<input type=\"hidden\" name=\"bn\" value=\"PP-DonationsBF\">";
echo "<br><br><br><center>";
echo "<input type=\"submit\" name=\"submit\" value=\"Yes-Continue Donation\"> ";
echo "</form>";
echo "<td><form action=\"donate_edit.php?authid=$authid\" method=\"post\"><input type=\"submit\" value=\"No-Go Back\"></form></td>";
echo "</center>";
?>
</font id=code></pre id=code>
and this is page that i want the totals in another column (Total Donations)
donate_view.php
<pre id=code><font face=courier size=2 id=code>
<?php
include 'config.php'; //is where my connection info is stored
include 'access.php';// is where my access levels are stored for other information i plan to use here
$db = mysql_connect ($hostname, $username, $password) or die ('Failed to connect to database: ' . mysql_error());
mysql_select_db($database);
$query = "SELECT * FROM $donate_table";
$result = mysql_query($query) or die ("Cannot query table " . mysql_error());
?>
</td>
</tr>
<tr>
<td align="left"> <table width="100%" border="1" bordercolor="#000000">
<tr bgcolor="#000000">
<td><div align="center"><font size="4">Name</font></div></td>
<td><div align="center"><font size="4">Amount</font></div></td>
<td><div align="center"><font size="4">Date</font></div></td>
<td><div align="center"><font size="4">Total Donations</font></div></td>
</tr>
<tr bgcolor="#666666">
<td>
<?php
while ($row = mysql_fetch_assoc($result))
{
$nname = $row['nname'];
echo "<strong>$nname</strong><br>";
echo "</td><td>";
$amount = $row['amount'];
echo "<strong>$amount</strong><br>";
echo "</td><td>";
$date = $row['date'];
echo "<strong>$date</strong><br>";
$total = $row['total'];
echo "<strong>$total</strong><br>";
}
mysql_free_result($result);
?>
</font id=code></pre id=code>
Replied 06 Dec 2006 02:50:12
06 Dec 2006 02:50:12 Jeremy Hill replied:
any ideas on how to fix this in my code ?