calculating sum and parsing first table's variables to another page - php

The Problem: Trying to calculate sum of a total variable which is inside the table invoicesub in the Form Process (2nd page). Trying to make the sum(total) appear in 3rd page which is display_invoice and also get the customer_name in the form process page(2nd page). somehow $_get["$cust_name"]; is not working and im getting a blank from it. Next, after i can get the sum_total variable how do i parse all these to the 4th page using a form?
Edit:http://imageshack.com/a/img19/8729/btls.png (picture of output)
http://imageshack.com/a/img20/8744/s3ut.png (picture of my database)
Too low rep, i cant post images. I uploaded it to image shack. The 1 on top of the table is from the mysqli_num_rows. This is the first row i entered in the form page. The 2nd row is missing
edit:
New Invoicefinal
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Services Rendered</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Price($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Amount</th>\n";
echo "</tr>";
$cname = $_GET["cname"];
global $connection;
$sql1="SELECT description,quantity, amount, discount, total, SUM(total) as sumtotal FROM invoicesub WHERE cust_name='$cname' GROUP BY description ORDER BY id";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
echo $count;
while ($rows = mysqli_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $rows['description'] . "</td>";
echo "<td>" . $rows['quantity'] . "</td>";
echo "<td>" . $rows['amount'] . "</td>";
echo "<td>" . $rows['discount']. "%" . "</td>";
echo "<td>" ."$". $rows['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
$sql1="SELECT SUM(total) as total_amt FROM invoicesub WHERE cust_name='$cname'";
$result3 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
while ($rows = mysqli_fetch_array($result3)){
echo "<tr>";
echo "<td>". "Total Amount:" ."$". $rows['total_amt'] . "</td>";
echo "</tr>";
echo "<form action=\"4thpage.php\" method=\"POST\">";
echo "<input type=\"hidden\" name=\"total_amt\"/>";
echo "Customer Paid:";
echo "<input type=\"text\" name=\"paid\" value=\"\"/>";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\"/>";
echo "<input type=\"button\" value=\"Cancel\" onclick=\"window.location='manage_content.php';\"/>";
echo "</form>";
}
The Form: It consists of a form that can allow more than 1 row of inputs, which means i will have more than 2 descriptions, quantity, amount and discount. Thus, i put them inside an array
Form Process: This is the 2nd page, it will take in all the arrays parsed in, make a loop to insert all the inputted variables in the form into the table invoicesub.
Display_invoice: This is the 3rd page, i want to show all the customer_name, descriptions, quantity, amount, total_amt that was keyed into invoicesub and then do a sum(total) and show it at the bottom of the page. I also want to insert the customer_name, sum(total) variable into the fourth page through the form at display_invoice. Is this possible?
Form
function addTextArea(){
count= count+1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' "+"id=name"+count+"> </div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' "+"id=amount"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('discount');
div.innerHTML += "<div><input type='text' name='discount[]' value ='' "+"id=discount"+count+"></div>";
div.innerHTML += "\n<br />";
}
function removeTextArea(){
document.getElementById("name"+count).remove();
document.getElementById("quantity"+count).remove();
document.getElementById("amount"+count).remove();
document.getElementById("discount"+count).remove();
count = count-1;
}
</script>
</head>
<body>
<form action="invoiceprocess.php" method="POST">
<?php
echo "<table border='2'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "</tr>";
echo "<tr>";
echo "<td>"?><input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/><?php "</td>";
echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='discount[]' value='' /><?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?><div id="name"></div> <?php "</td>";
echo "<td>"?><div id="quantity"></div> <?php "</td>";
echo "<td>"?><div id="amount"></div> <?php "</td>";
echo "<td>"?><div id="discount"></div> <?php "</td>";
echo "</tr>";
?>
Customer Name:
<br />
<input type="text" name="cust_name" value="" />
<br />
<input type="button" value="Add Description" onClick="addTextArea();">
<input type="button" value="Remove Description" onClick="removeTextArea();">
<input type="submit" name="submit" value="submit">
</form>
</body>
invoiceprocess (The 2nd page)
if (isset($_POST['submit'])){ // Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
$cust_name_array = mysql_prep( $_POST['cust_name']);
for ($i = 0; $i < count($name_array); $i++){
$cust_name = $cust_name_array;
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
global $connection;
$query = "INSERT INTO invoicesub (";
$query.= " cust_name, description, quantity, amount, discount, total";
$query.= ") VALUES (";
$query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total_amt}";
$query.= ")";
$result = mysqli_query($connection, $query);
}
redirect_to("invoicesubmitfinal.php?cname=".urlencode($cust_name));
}
Display_Invoice(invoicesubmitfinal.php)
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Customer_name</th>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Total_amt</th>\n";
echo "</tr>";
$cust_name = $_GET["$cust_name"];
global $connection;
$sql1="SELECT SUM(total) FROM invoicesub WHERE cust_name='$cust_name'";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
while ($rows = mysqli_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $rows['quantity'] . "</td>";
echo "<td>" . $rows['amount'] . "</td>";
echo "<td>" . $rows['discount']. "%" . "</td>";
echo "<td>" ."$". $rows['total'] . "</td>";
echo "<td>" . "$" . $total_amt . "</td>";
echo "</tr>";
<form action="insertfinal.php" method="POST">
Customer Paid:
<input type="text" name="paid" value="" />
</form>

Related

PHP MySQL Get material and quantity for an order form

I need to create an order form that has a checkbox for each product in a table and have a quantity box next to each product.
I need to process the products that have been checked and update an orders table with the checked products and the quantity entered.
I don't know how to link the product checkbox to the quantity field.
$sql="select * from tproducts";
$result=mysqli_query($con, $sql) or die(mysqli_error($con));
echo "<table>";
echo "<tr>";
echo "<th>Product Names</th>";
echo "</tr>";
if(mysqli_num_rows($result)>0)
{
echo "<form action=welcome1.php method=post>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[]' value='qty' size=5></td>";
echo "</tr>";
}
}
echo "</table>";
echo '<input type="submit" name="submit" value="submit" />';
echo "</form>";
echo "</html>";
welcome1.php
<?php
echo "materialcode<br>";
print_r($_POST['materialcode']);
echo "<br>";
echo "qty<br>";
print_r($_POST['qty']);
?>
PHP will keep the key name you supply in the fields html.
$productIndex = 0;
while ($row = mysqli_fetch_array($result) )
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[$productIndex]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[$productIndex]' value='qty' size=5></td>";
echo "</tr>";
$productIndex++;
}
This example uses numeric indexes but you could probably use $row['intProductId'] as well.
In your php:
$_POST['materialCode'][0] will be the analog to $_POST['qty'][0] but only the checked checkboxes will be supplied. You might end up with a $_POST['materialCode'] array that has index 0, 5, and 7 if those were the only ones checked.
So use a foreach loop instead of a for loop.
foreach( $_POST['materialCode'] as $key => $value ){
$productId = $value;
$quantity = $_POST['qty'][$key];
}

Get values of radio selected values of entire row to next file

I have displayed the values from mysql table along with radio buttons with different values. On submit button it should redirect to next page and display the selected values. My code on first file is as follows :
$query1 = "select * from booking";
$result1 = $connection->query($query1);
echo "<form method='post' action='edit.php'>";
echo "<center>";
echo "<table border='1'>";
$count = 0;
while($row = $result1->fetch_row())
{
echo "<tr>";
echo "<td width=".'10'.">";
$count = $count + 1;
#$sr=$sr+1;
echo "<input type='radio' name='select' value='".$count."' />";
#$row = mysqli_fetch_row($result1);
#echo "<tr>";
echo "<td width=".'10'.">";
$rooms = $row[0];
$srrooms = $rooms;
echo "<input type='text' name='srrooms' value='".$srrooms."' hidden='hidden'>";
#echo $srrooms;
echo $rooms;
echo "</td>";
echo "<td width=".'10'.">";
$no_roomss = $row[1];
$nosrrooms = $no_roomss;
echo "<input type='text' name='nosrrooms' value='".$nosrrooms."' hidden='hidden'>";
echo $no_roomss;
echo "</td>";
echo "<td width=".'10'.">";
$no_dayss = $row[2];
$nodays = $no_dayss;
echo "<input type='text' name='nodays' value='".$nodays."' hidden='hidden'>";
echo $no_dayss;
echo "</td>";
echo "<td width=".'10'.">";
$name = $row[3];
echo $name;
echo "</td>";
echo "</tr>";
#$count++;
}
echo "<input name='count' type='hidden' id='count'>"; // hidden input where counter value will be stored
echo "</table>";
echo "<input type='submit' name='submitRooms' />";
echo "</center>";
echo "</form>";
The javascript code is :
$(function(){
$('input[name="select"]').change(function(){
$('#count').val($("input[name='select']:checked").val());
});
});
edit.php code is :
<?php
if(isset($_POST['submitRooms'])){
#$count = $_POST['count'];
#$srrooms=$_POST['srrooms'.$count];
#$nosrrooms=$_POST['nosrrooms'.$count];
#$nodays=$_POST['nodays'.$count];
echo 'Selected values are: '. $srrooms . $nosrrooms . $nodays;
}
?>
You can use counter for submitting the values of the selected radiobutton's row as:
<?php
$query1 = "select * from booking";
$result1 = $connection->query($query1);
echo "<form method='post' action='edit.php'>";
echo "<center>";
echo "<table border='1'>";
$count = 0; // counter variable
while($row = $result1->fetch_row())
{
echo "<tr>";
echo "<td width=".'10'.">";
echo "<input type='radio' name='select' value='".$count."' />";
echo "<td width=".'10'.">";
$rooms = $row[0];
$srrooms = $rooms;
echo "<input type='hidden' name='srrooms".$count."' value='".$srrooms."' >"; // add counter value to every name of the input fields
echo $rooms;
echo "</td>";
echo "<td width=".'10'.">";
$no_roomss = $row[1];
$nosrrooms = $no_roomss;
echo "<input type='hidden' name='nosrrooms".$count."' value='".$nosrrooms."'>";
echo $no_roomss;
echo "</td>";
echo "<td width=".'10'.">";
$no_dayss = $row[2];
$nodays = $no_dayss;
echo "<input type='hidden' name='nodays".$count."' value='".$nodays."' >";
echo $no_dayss;
echo "</td>";
echo "<td width=".'10'.">";
$name = $row[3];
echo $name;
echo "</td>";
echo "</tr>";
$count++;
}
echo "<input name='count' type='hidden' id='count'>"; // hidden input where counter value will be stored
echo "</table>";
echo "<input type='submit' name='submitRooms' />";
echo "</center>";
echo "</form>";
?>
and then use jquery for saving the active radiobuttons count in the hidden count input field [after the above php code]
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type='text/javascript'>
$(function(){
$('input[name="select"]').change(function(){
$('#count').val($("input[name='select']:checked").val());
});
});
</script>
then on your edit.php fetch the posted data as:
<?php
if(isset($_POST['submitRooms'])){
#$count = $_POST['count'];
#$srrooms = $_POST['srrooms'.$count];
#$nosrrooms = $_POST['nosrrooms'.$count];
#$nodays = $_POST['nodays'.$count];
echo 'Selected values are: '. $srrooms . $nosrrooms . $nodays;
}
?>
You didn't have added the whole form code so please add the above mentioned form code and the reason why your code is not working is that you haven't added the count with the input field names. And their is no use of fetching the posted data with count in the edit.php till you don't have the input fields named with the $count variable
echo "<input type='hidden' name='srrooms".$count."' value='".$srrooms."' >";

insert multiple rows of data by single submit button using php

How do I retrieve data from a SQL table, modify the data and store it in another database table with multiple rows & columns and with single submit button I want insert every rows at a time I don't know how to get that hidden value and work properly with that
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
for($i=1; $i<=$amt; $i++) {
$qry .= "('".$_POST["rollno$i"]."', '".$_POST["name$i"]."', '".$_POST["year$i"]."', '".$_POST["attendance$i"]."', '".$_POST["reason$i"]."' ),"; // loop the mysql_query values to avoid more server loding time
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=rollno$i value=" . $record['rollno'] . " </td>";
echo "<td>" . "<input type=text name=name$i value=" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=year$i value=" . $record['year'] . " </td>";
echo "<td> "."<select name=attendance$i >
<option value=Present >present</option>
<option value=Absent >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols=15 rows=2 name=reason$i placeholder=Enter reason ...></textarea>" . "</td>" ;
echo "<td>" . "<input type=hidden name=total value=" . $i-1 . "</td>";
echo "</tr>";
}
echo"</table>";
echo "<input type=submit name=submit value=save class=Button3>";
echo "</form>";
};
mysqli_close($dbcon);
?>
you are opening multiple forms, for each row in your table on.
This causes your html to be invalid, just start the form before displaying the table.
You could use this html
<table>
<?php
for ($i = 0; $i < $num; $i++) {
$record = mysqli_fetch_array($myData);
?>
<tr>
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
<td><input type="text" name="name[<?= $record['rollno'] ?>]" value="<?= $record['name']?>" </td>
<td><input type="text" name="year[<?= $record['rollno'] ?>]" value="<?= $record['year'] ?>" </td>
<td><select name="attendance[<?= $record['rollno'] ?>]" >
<option value="Present" >present</option>
<option value="Absent" >Absent</option>
</select></td>
<td><textarea cols="15" rows="2" name="reason[<?= $record['rollno'] ?>]" placeholder="Enter reason ..."></textarea></td>
</tr>
<?php
}
?>
</table>
with this your values will every row will be put into the $_POST-Array, you can access the values via the indexes (I am guessing rollno represents the ID of the dataset).
When you really only want to insert all the values into a table, you can leave the index out. Meaning you could write
<td><input type="text" name="rollno[]" value="<?= $record['rollno'] ?>" </td>
Instead of
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
You don't need the hidden field, you can just count the items in the array.
$total = count($_POST['rollno']);
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
$rollnos= $_POST['rollno'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
$i=0;
foreach($rollnos as $rollno) {
$qry .= "('".$rollno."', '".$_POST["name"][$i]."', '".$_POST["year"][$i]."', '".$_POST["attendance"][$i]."', '".$_POST["reason"][$i]."' ),"; // loop the mysql_query values to avoid more server loding time
$i=$i+1;
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type='text' name='rollno[]' value='" . $record['rollno'] . "'> </td>";
echo "<td>" . "<input type='text' name='name[]' value='" . $record['name'] . "'> </td>";
echo "<td>" . "<input type='text' name='year[]' value='" . $record['year'] . "'> </td>";
echo "<td> "."<select name='attendance[]' >
<option value='Present' >present</option>
<option value='Absent' >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols='15' rows='2' name='reason[]' placeholder='Enter reason ...'></textarea>" . "</td>" ;
echo "<td></td>";
echo "</tr>";
}
echo "<input type='hidden' name='total' value='" . $i-1 . "'>";
echo"</table>";
echo "<input type='submit' name='submit' value='save' class='Button3'>";
echo "</form>";
};
mysqli_close($dbcon);
?>

cant update multiple existing rows in php mysql

I have this edit form:
<?php
while($rows=mysql_fetch_array($query)){
$area=$rows['area'];
$dates=$rows['dates'];
$number_of_local_hires=$rows['number_of_local_hires'];
$salary_per_local_hire_per_day=$rows['salary_per_local_hire_per_day'];
$amount=$rows['amount'];
echo "<tr id='addr0'>";
echo "<td>";
echo "<input type='text' name='ca_salary_area[]' placeholder='Area' class='form-control' value='$area' required/>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='ca_salary_date[]' placeholder='Date/s' class='form-control' value='$dates' required/>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='ca_salary_localhires[]' placeholder='Number of Local Hires' class='form-control' value='$number_of_local_hires' required/>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='ca_salary_perday[]' placeholder='Salary Per Local Hire Per Day' class='form-control' value='$salary_per_local_hire_per_day'required/>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='ca_salary_amount[]' placeholder='Amount' class='form-control' value='$amount'/>";
echo "</td>";
echo "</tr>";
}
?>
But I can't update it using this query in my edit_salary.php:
$sql_rows = mysql_query("SELECT *FROM `tblcasalaryformdetails` WHERE casalaryform_id =$id");
$num_rows = mysql_num_rows($sql_rows);
if ($num_rows == count($_POST['ca_salary_area'])){
for($i=0;$i<$num_rows;$i++)
{
$nos_hire = $_POST['ca_salary_localhires'][$i];
$nos_salaray = $_POST['ca_salary_perday'][$i];
$subtotal_salary_amount = $nos_hire * $nos_salaray;
$sql = "UPDATE `tblcasalaryformdetails`
SET area = '". $_POST['ca_salary_area'][$i]."',
dates = ". $_POST['ca_salary_date'][$i].",
number_of_local_hires = ". $_POST['ca_salary_localhires'][$i].",
salary_per_local_hire_per_day = ". $_POST['ca_salary_perday'][$i].",
amount = ". $subtotal_salary_amount."
WHERE casalaryform_id = $id";
mysql_query($sql);
}
}
The result is all the values will be updated same as the last row value. What did I missed? I tried show its index, it looks fine.

Issue on parsing and sum using database

im creating an invoice function. It involves 3 pages.
The first page which is a form that takes in the variable of customer name, description, amount, quantity and discount.
The second page parses all these variable, make a new variable called total that will calculate amount X quantity minus away the discount and then insert into database then redirect to the third page.
The Third Page
Is supposed to sum the total column for a final total variable. How do i use the sum function to calculate the total in order to get the final total? And also how do i store all the variables printed out so that i can store them into database?
E.g. i want to print out all the descriptions, customer name, amount, quantity and total entered in page one and then add the final total which was calculated in page 3 .
Page one(submit form)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var count = 0;
function addTextArea() {
count = count + 1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' " + "id=name" + count + "> </div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' " + "id=quantity" + count + "></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' " + "id=amount" + count + "></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('discount');
div.innerHTML += "<div><input type='text' name='discount[]' value ='' " + "id=discount" + count + "></div>";
div.innerHTML += "\n<br />";
}
function removeTextArea() {
document.getElementById("name" + count).remove();
document.getElementById("quantity" + count).remove();
document.getElementById("amount" + count).remove();
document.getElementById("discount" + count).remove();
count = count - 1;
}
</script>
</head>
<body>
<form action="invoicesubmit.php" method="POST">
<?php
echo "<table border='2'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "</tr>"; echo "<tr>";
echo "<td>"?>
<input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/>
<?php "</td>"; echo "<td>"?>
<input type='text' size="50" name='quantity[]' value='' />
<?php "</td>";
echo "<td>"?>
<input type='text' size="50" name='amount[]' value='' />
<?php "</td>";
echo "<td>"?>
<input type='text' size="50" name='discount[]' value='' />
<?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?>
<div id="name"></div>
<?php "</td>"; echo "<td>"?>
<div id="quantity"></div>
<?php "</td>"; echo "<td>"?>
<div id="amount"></div>
<?php "</td>"; echo "<td>"?>
<div id="discount"></div>
<?php "</td>";
echo "</tr>"; ?> <br />
<input type="button" value="Add Description" onClick="addTextArea();">
Customer Name: <input type="text" value="" name="cust_name" />
<input type="button" value="Remove Description" onClick="removeTextArea();">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Page Two (Store into database)
<?php require_once ("includes/session.php");?>
<?php require_once ("includes/db_connection.php");?>
<?php require_once ("includes/functions.php");?>
<?php require_once ("includes/validation_function.php");?>
<?php
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Total_amt</th>\n";
echo "</tr>";
<?php
if (isset($_POST['submit']))
{ // Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
for ($i = 0; $i < count($name_array); $i++)
{
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
$cust_name = mysqlprep($_POST["cust_name"]);
global $connection;
$query = "INSERT INTO invoicesub (";
$query.= " cust_name, description, quantity, amount, discount, total";
$query.= ") VALUES (";
$query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total}";
$query.= ")";
$result = mysqli_query($connection, $query);
echo "<tr>";
echo "<td>" . $name . "</td>";
echo "<td>" . $quantity . "</td>";
echo "<td>" . "$" . $amount . "</td>";
echo "<td>" . $discount . "%" . "</td>";
echo "<td>" . "$" . $total_amt . "</td>";
echo "</tr>";
if ($result)
{
redirect_to("invoicesubmitfinal.php");
}
}
?>
page three (Print and store into 2nd database)
<?php
$query = "SELECT * ";
$query.= "FROM invoicesub";
$result = mysqli_query("SELECT sum(amount) FROM invoicesub") or die(mysqli_error());
while ($rows = mysql_fetch_array($result))
{
$result1 = mysqli_query("SELECT sum(quantity) FROM invoicesub") or die(mysqli_error());
while ($rows1 = mysqli_fetch_array($result1))
{
$result2 = mysqli_query("SELECT sum(total) FROM invoicesub") or die(mysqli_error());
while ($rows2 = mysqli_fetch_array($result2))
{
echo $rows['amount'];
echo $rows1['quantity'];
echo $rows2['total'];
$finaltotal = $rows2['total'];
}
}
}
?>
Since, your requirement is not really understandable, i tried clearing some errors & changed the SQL. if you, refrain your questions, along with the included function & connection files, we can only take a look. The modified codes are shown below.
invoicesubmit.php
<?php require_once ("includes/session.php");?>
<?php require_once ("includes/db_connection.php");?>
<?php require_once ("includes/functions.php");?>
<?php require_once ("includes/validation_function.php");?>
<?php
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Total_amt</th>\n";
echo "</tr>";
if (isset($_POST['submit'])){ // Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
print_r($quantity_array);
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
for ($i = 1; $i < count($name_array); $i++){
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
$cust_name = mysqlprep( $_POST["cust_name"]);
global $connection;
$query = "INSERT INTO invoicesub (";
$query.= " cust_name, description, quantity, amount, discount, total";
$query.= ") VALUES (";
$query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total_amt}";
$query.= ")";
$result = mysqli_query($connection, $query);
echo "<tr>";
echo "<td>" . $name . "</td>";
echo "<td>" . $quantity . "</td>";
echo "<td>" . "$" . $amount . "</td>";
echo "<td>" . $discount . "%" . "</td>";
echo "<td>" . "$" . $total_amt . "</td>";
echo "</tr>";
}
if ($result){
redirect_to("invoicesubmitfinal.php?cname=$cust_name");
}
}
?>
invoicesubmitfinal.php
<?php
require_once ("includes/db_connection.php");
global $connection;
$name=$_REQUEST['cname'];
$sql1="SELECT sum(amount) as amount, sum(quantity) as quantity, sum(total) as total FROM invoicesub where cust_name=$name";
$result = mysqli_query($connection, $sql1) or die(mysqli_error());
while ($rows = mysql_fetch_array($result)){
echo $rows['amount'];
echo $rows['quantity'];
echo $rows['total'];
}
?>

Categories