【问题标题】:issue with inserting to database插入数据库的问题
【发布时间】:2014-02-23 08:56:23
【问题描述】:

我想将 result2 的内容,即描述、数量、价格、折扣和总计以及一些添加的变量插入另一个表(显示帐单)。

我在一个 while 循环内将插入插入到 displaybilling 查询中,但不知何故它不起作用。对不起,我是 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, oustanding";
$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 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>";


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


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

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

          }    
          echo "</table>";

          echo "Total Amount:";
          echo "&nbsp;";
          echo $totalsum;
          echo "<br />";

          echo "Customer Name:";
          echo "&nbsp;";
          echo $custname;
          echo "<br />";

          echo "Date:";
          echo "&nbsp;";
          echo $today;
          echo "<br />";

          echo "Customer Paid:";
          echo "&nbsp;";
          echo $paid;
          echo "<br />";

          echo "Outstanding fee:";
          echo "&nbsp;";
          echo $outstanding;
          echo "<br />";







}

?>

代码

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

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


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

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

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


          $sumtotal = $row['total_amt_2'];

          echo "<form action=\"invoiceconirm.php\" method=\"POST\">";
          echo "<input type=\"hidden\" name=\"total_amt\" value=\"$sumtotal\" />";
          echo "<input type=\"hidden\" name=\"cust_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>";

          }

【问题讨论】:

    标签: php mysqli insert


    【解决方案1】:

    表单输入的值用引号括起来...它们只是作为 $sumtotal 和 $cname 输出,因此实际上没有信息从 php 脚本传递到 HTML。

    这应该可行。

    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>";
    
                $name = $rows['description'];
                $today=date('d-m-Y');
                $quantity = $rows['quantity'];
                $amount = $rows['amount'];
                $discount = $rows['discount'];
    
    
                global $connection;
                $query = "INSERT INTO displaybilling (";
                $query.= " cust_name, date, description, quantity, price, discount, total_amt";
                $query.= ") VALUES (";
                $query.= " '{$cname}', '{$today}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total}";
                $query.= ")";
    
                $result = mysqli_query($connection, $query);
    
              }         
              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>";
    
    
              $sumtotal = $row['total_amt_2'];
    
              echo "<form action=\"invoiceconirm.php\" method=\"POST\">";
              echo "<input type=\"hidden\" name=\"total_amt\" value=\"".$sumtotal."\" />";
              echo "<input type=\"hidden\" name=\"cust_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>";
    
              }
    

    【讨论】:

    • 其实可以将表单的值解析到下一页。它正在工作,但我无法插入数据库。我将在下一条评论中粘贴下一页的代码
    • 太长了,所以我编辑了这个问题。我将下一页的代码放在问题中的下一页代码下
    猜你喜欢
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多