转移到另一个表后删除表中的所有内容的问题

时间:2014-03-04 11:36:14

标签: php mysqli sql-delete

问题:在文件invoiceconfirm.php中,最后有一个删除表内容(invoicesub)的代码,但它不起作用。完成所有计算后,invoicesub表的所有内容都将传输到displaybilling表。我希望删除invoicesub表,因为在invoicesubitfinal页面中有计算,如果输入相同的客户名称,则会导致计算错误。因此,我需要删除invoicesub表的内容。

文件说明:Invoice submit.php接受名称,数量,金额,折扣和客户名称变量,然后将其插入表invoicesub。之后,它会重定向到invoicesubmitfinal。

invoicesubmitfinal.php将刚刚打印出无论是在invoicesub的表,然后添加在一个隐藏字段,其将解析的名称,总金额和支付变量到invoiceconfirm.php

Invoiceconfirm.php的目的是将invoicesub中的所有内容插入名为displayoutstanding的新表中。但是,我还想删除invoicesub中的所有内容,因为如果在表单中输入相同的客户名称,则会导致总和中的计算错误。

Invoicesubmit.php

<?php

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;




            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>";

            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));

        }


?>

invoicesubmitfinal.php

 <?php
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 FROM invoicesub WHERE cust_name='$cname' GROUP BY description ORDER BY id";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));

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_2 FROM invoicesub WHERE cust_name='$cname'";
$result3 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));

          while ($row = mysqli_fetch_array($result3)){

          echo "<tr>";
          echo "<td>". "Total Amount: " ."$ ". $row['total_amt_2']  . "</td>";
          echo "</tr>";

          $cname = $_GET["cname"];
          $sumtotal = $row['total_amt_2'];

          echo "<form action=\"invoiceconfirm.php\" method=\"POST\">";
          echo "<input type=\"hidden\" name=\"total_amt\" value=\"$sumtotal\" />";
          echo "<input type=\"hidden\" name=\"customer_name\" value=\"$cname\" />";
          echo "Customer Paid: "."$ ";
          echo "<input type=\"text\" name=\"paid\" value=\"\"/>";
          echo "<br />";
          echo "<input type=\"submit\" name=\"submit\" value=\"Submit\"/>";
          echo "<input type=\"button\" value=\"Cancel\" onclick=\"window.location='manage_content.php';\"/>";
          echo "</form>";

          }
          ?>

invoiceconfirm.php

<?
if (isset($_POST['submit'])){




$totalsum = $_POST["total_amt"];
$custname = $_POST["customer_name"];
$paid = $_POST["paid"];
$today=date('d-m-Y');
$outstanding = $totalsum - $paid;

global $connection;
$query = "INSERT INTO displayoutstanding (";
$query.= " cust_name, date, paid, final_total, outstanding";
$query.= ") VALUES (";
$query.= " '{$custname}', '{$today}', {$paid}, {$totalsum}, {$outstanding}";
$query.= ")";

$finished = mysqli_query($connection, $query);




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>";

global $connection;

$sql1="SELECT cust_name, description, quantity, amount, discount, total FROM invoicesub WHERE cust_name='$custname' GROUP BY description ORDER BY id";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));

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>";

            $custname = $rows['cust_name'];
            $name = $rows['description'];
            $today=date('d-m-Y');
            $quantity = $rows['quantity'];
            $amount = $rows['amount'];
            $discount = $rows['discount'];
            $total = $rows['total'];  

            global $connection;
            $query = "INSERT INTO displaybilling (";
            $query.= " cust_name, date, description, quantity, price, discount, total_amt";
            $query.= ") VALUES (";
            $query.= " '{$custname}', '{$today}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total}";
            $query.= ")";

            $result = mysqli_query($connection, $query);


              $query = "DELETE FROM invoicesub WHERE cust_name = {$custname}";
  $delete = mysqli_query($connection, $query);





          }    

?>

1 个答案:

答案 0 :(得分:0)

运行查询时出现未知列错误,因为您错过了此查询中的单引号:

$query = "DELETE FROM invoicesub WHERE cust_name = {$custname}";

应该是:

$query = "DELETE FROM invoicesub WHERE cust_name = '{$custname}'";

{}括号在这里也没用,所以它可能是:

$query = "DELETE FROM invoicesub WHERE cust_name = '$custname'";

您说要删除该表。 invoicesub表中的其他数据是否比该客户的更多?如果没有,你可以考虑做

$query = "TRUNCATE invoicesub";

将清空表并将所有计数器重置为默认值。