0

Basically what I'm trying to do is to get a user to submit his/her order into my database through a form. I can grab the c_id (customer_id) from a php code at the top of the page so there's nothing wrong there.

This is my html for my form.

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

Below is my sql statement.

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

}

?>

There are a few attributes in my sql statement that I wish to be added automatically into my database such as the sub total price for the item and the invoice num, date and status.

For some reason, it's just not inserting the values into the database and I've ran through the statement for quite some time now and I can't figure out what is wrong. Hoping to find an answer here. Thank you.

4
  • Please invest some time to learn about prepared statements in PHP. Commented Mar 3, 2019 at 14:25
  • Check the value of $run_tshirt_order and $run_tpending_order, if they are false, output the value of mysqli_error($conn) Commented Mar 3, 2019 at 14:27
  • Why do you have two tables containing the same data? Which insert fails? Commented Mar 3, 2019 at 14:30
  • The two tables are for different uses. customer_orders is for customers to view their orders, pending_orders are for the admins to check in admin panel. Both the insert statements fail and or (mysqli_error($conn) isn't showing any errors. Even tried or die(mysqli_error($conn)) Commented Mar 3, 2019 at 14:38

2 Answers 2

1

mysqli_query() returns a boolean response, meaning you should listen to it, in case something is wrong with your SQL code.

Instead of $run_tshirt_order = mysqli_query($conn,$insert_tshirt_order); try the following:

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
}

That way, if there is ever an error in your SQL, you'll be told about it. If you can figure out what the error is from the error message, paste the results in your question so that we can have a look.

Sign up to request clarification or add additional context in comments.

3 Comments

Add the format to both your SQL queries (mysqli_query($conn,$insert_tpending_order); also). Still no error?
If that doesn't work, do a var_dump() of your SQL query, and see if it even contains the values you want to insert.
I recoded the insert statement and decided to test each column so now I manually insert all the records through the form, customer_id included.. I'm able to insert the records without t_name and t_image. I can't insert into the database when they are in the query.. But when I remove t_name and t_image, I'm able to insert them.
0

I solved my problem. I recoded the entire sql statement. To anyone who's having the same problem as I did, here's my recoded code, maybe you can work something out from mine :)

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

}

?>

Also, a big thank you to the people who tried to help me! I appreciate it alot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.