SQL语句未将数据插入数据库

时间:2019-03-03 14:23:51

标签: php mysqli sql-insert

基本上我想做的是让用户通过表单将其订单提交到我的数据库中。我可以从页面顶部的php代码中获取c_id(customer_id),所以这里没有任何问题。

这是我的表单的html。

                       <form action="tshirtorder.php" method="post" enctype="multipart/form-data"><!-- form Begin -->

                        <div class="form-group"><!-- form-group Begin -->

                           <label>Design Image</label>

                           <input type="file" name="t_image" class="form-control form-height-custom" required>
                       </div>
                       <div class="form-group"><!-- form-group Begin -->

                           <label>Design Name</label>

                           <input type="text" class="form-control" name="t_name" value="Custom Design" disabled>

                       </div><!-- form-group Finish -->

                       <div class="form-group"><!-- form-group Begin -->

                           <label>Quantity</label>

                           <input type="text" class="form-control" name="t_qty" required>

                       </div><!-- form-group Finish -->

                       <div class="form-group"><!-- form-group Begin -->

                           <label>Size</label>

                           <input type="text" class="form-control" name="t_size" required>

                       </div><!-- form-group Finish -->


                       <div class="text-center"><!-- text-center Begin -->

                          <a href="torder.php?c_id=<?php echo $customer_id ?>"><button type="submit" name="tsubmit" class="btn btn-primary">

                           <i class="fa fa-user-md"></i> Send Message

                              </button></a>

                       </div><!-- text-center Finish -->

                   </form><!-- form Finish -->

下面是我的sql语句。

<?php

if(isset($ _ POST ['tsubmit'])){

$customer_id = $_GET['c_id'];

$t_image = $_FILES['t_image']['name'];

$t_image_tmp = $_FILES['t_image']['tmp_name'];

$t_name = $_POST['t_name'];

$due_amount = "25";

$invoice_no = mt_rand();

$t_qty = $_POST['t_qty'];

$t_size = $_POST['t_size'];

$status = "pending";

$pro_id = "$customer_id + 1";

move_uploaded_file($t_image_tmp,"images/customer/customerdesign/$t_image");

$sub_total = "$t_qty*25";

$insert_tshirt_order = "insert into tshirt_orders (customer_id,due_amount,invoice_no,qty,size,order_date,order_status) values ('$customer_id','$sub_total','$invoice_no','$t_qty','$t_size',NOW(),'$status')";

$run_tshirt_order = mysqli_query($conn,$insert_tshirt_order);

$insert_tpending_order = "insert into tpending_order (customer_id,invoice_no,product_id,qty,size,order_status) values ('$customer_id','$invoice_no','$pro_id','$t_qty','$t_size','$status')";

$run_tpending_order = mysqli_query($conn,$insert_tpending_order);


echo "<script>alert('Your T-Shirt Design has been successfully ordered!')</script>";

echo "<script>window.open('profile.php?t_orders','_self')</script>";

}

?>

我的sql语句中有一些属性希望自动添加到数据库中,例如商品的小计价格以及发票编号,日期和状态。

由于某种原因,它只是没有将值插入数据库中,而我已经运行了相当长的一段时间,因此我无法弄清楚出了什么问题。希望在这里找到答案。谢谢。

2 个答案:

答案 0 :(得分:1)

mysqli_query()返回一个布尔响应,这意味着您应该侦听该响应,以防SQL代码出现问题。

而不是$run_tshirt_order = mysqli_query($conn,$insert_tshirt_order);,请尝试以下操作:

if(!$run_tshirt_order = mysqli_query($conn,$insert_tshirt_order)){
    //If the query returns a boolean FALSE
    die(mysqli_error($conn));
    //die() simply kills the script and outputs a message
    //You should replace it with better error handling
}

这样,如果您的SQL中有错误,系统会告知您。 如果您可以从错误消息中找出错误的原因,请将结果粘贴到您的问题中,以便我们查看。

答案 1 :(得分:0)

我解决了我的问题。我重新编码了整个sql语句。对于遇到和我一样的问题的人,这是我重新编码的代码,也许您可​​以从我的工作中解决问题:)

    <?php 

if(isset($ _ POST ['tsubmit'])){

$session_name = $_SESSION['customer_name'];

$select_customer = "select * from customers where customer_name='$session_name'";

$run_customer = mysqli_query($conn,$select_customer);

$row_customer=mysqli_fetch_array($run_customer);

$customer_id= $row_customer['customer_id'];

$t_name = $_POST['t_name'];

$due_amount = 25;

$invoice_no = mt_rand();

$t_qty = $_POST['t_qty'];

$t_size = $_POST['t_size'];

$t_image = $_FILES['t_image']['name'];

$t_image_tmp = $_FILES['t_image']['tmp_name'];

$status = "pending";

$total_amount = $due_amount*$t_qty;

$pro_id = $customer_id + 1;

move_uploaded_file($t_image_tmp,"images/customer/customerdesign/$t_image");

$insert_tshirt_order = "insert into tshirt_orders (customer_id,t_image,t_name,due_amount,invoice_no,qty,size,order_date,order_status) values ('$customer_id','$t_image','$t_name','$total_amount','$invoice_no','$t_qty','$t_size',NOW(),'$status')";

$run_tshirt_order = mysqli_query($conn,$insert_tshirt_order);

$insert_tpending_order = "insert into tpending_order (customer_id,invoice_no,product_id,qty,size,order_status) values ('$customer_id','$invoice_no','$pro_id','$t_qty','$t_size','$status')";

$run_tpending_order = mysqli_query($conn,$insert_tpending_order);

    echo "<script>alert('Your T-Shirt order has been made!')</script>";  
        echo "<script>window.open('profile.php?t_orders','_self')</script>";

}

?>

也非常感谢那些试图帮助我的人!非常感谢。