jeudi 16 avril 2015

On form submit, is it possible to use nested IF statements to perform multiple MySQL commands?

I have 3 forms where each entry, depending on which form is completed, is assigned a value.


The value is a hidden field that is given a default point value.


(Form 1 entries = 4 point values, Form 2 entries = 12 point values, Form 3 entries = 40 point values)


I have each entry's unique ID created from the timestamp so a user can insert multiple entries into the table with their email that I have as a hidden input, and echoes from the initial login.


My goal is to count all values and total them from all of the submissions a user has entered upon each form so it updates the total upon success.


I want to identify all the user's email addresses in the table submitted and then count the value column from each and then total them and submit that data to a table where there would be 3 totals, one per each point value (4, 12, 40)


and then total all those values in a separate "Total" table (the sum of all 3 fields in the 2nd table, 4, 12, 40 = "total") and I want to accomplish all of this upon the form submission of any one of these forms.


Form 1 database table example:


"email@email.com" (entry info) Value: "4" Unique ID: "Timestamp"


"email@email.com" (entry info) Value: "4" Unique ID: "Timestamp"


"email@email.com" (entry info) Value: "4" Unique ID: "Timestamp"


Total value for user from Form 1:"email@email.com" = 12


My goal is, upon successful submission of each form data into the database table, is to have an IF statement that tallies up all of the value fields for that user and have that inserted/updated into a separate table where the tallies of each point values (4, 12, or 40) are added up in their own column:


Tallied up database table:


"email@email.com" form 1 totals: (4) form 2 totals: (12) form 3 totals: (40)


(each total would update according to the success of each entry on their individual forms)


AND THEN I want to have another nested IF statement that tallies the total of all 3 of THOSE columns and inserts/updates that value into a separate table:


All form totals database table:


"email@email.com" Total from all tables: ("total")


I want to be able to echo that "total" on a page so the user can see the cumulative point value of all the entries from all of the different forms.


Is this possible to do with nested IF statements? Am I doing this the hard way?


Here is my submit form code so far that updates the tables. I have little experience with IF statements and am not sure where to start if its even possible to do with how I THINK it will work:



$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "40point")) {
$insertSQL = sprintf("INSERT INTO 40point (email, part_number, invoice_number, `date`, points) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['part_number'], "text"),
GetSQLValueString($_POST['invoice_number'], "text"),
GetSQLValueString($_POST['date'], "text"),
GetSQLValueString($_POST['points'], "text"));

mysql_select_db($database_register, $register);
$Result1 = mysql_query($insertSQL, $register) or die(mysql_error());

$insertGoTo = "40point-success.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}


<form id="40point" name="40point" action="<?php echo $editFormAction; ?>" method="POST" accept-charset="utf-8">
<input type="hidden" name="email" value="<? echo $user['email']; ?>">
<td><input type="text" id="part_number" name="part_number" required = "true"></td>
<td>Invoice number</td>
<td><input type="text" id="invoice_number" name="invoice_number" required = "true"></td>
<td>date</td>
<td><input type="text" id="date" name="date" required = "true">
<input type="hidden" name="points" value="40"></td>
<td><input type="submit">

Aucun commentaire:

Enregistrer un commentaire