EKBG EKBG - 22 days ago 7
HTML Question

Insert form value, which retrieved from database, again in to MySQL database

I have a read-only text box which is retrieving max receipt no as it's value, from the database

sales
table.

<input name="txtReceiptNo" type="text" id="txtReceiptNo" size="10" value="<?php
$receiptNo = mysqli_fetch_row(mysqli_query($connection,"SELECT max(sales_id)+1 FROM sales")); echo $receiptNo[0];?>" readonly />


When I try to send this to the database, it gives an error.

#1054 - Unknown column 'receipt_no' in 'field list'


(Yes, I know this is incorrect, but how can I correct this?)

SQL Query:

INSERT INTO sales(cus_id,item_id,stock_id,receipt_no,qty,unit_price,discount,amount,user_id,purchase_id)
SELECT C.cus_id, I.item_id, S.stock_id, receipt_no='$receipt_no',ST.qty, ST.unit_price, ST.discount, ST.amount, U.id, P.purchase_id
FROM customers C, items I, stock S, sales_temp ST, users U, purchase_items P
WHERE ST.staff='$user' AND
C.customer_name='$customer' AND
I.item_name=ST.item_name AND
S.stock_code=ST.stock_code AND
ST.purchase_id=P.purchase_id


Edited Code:



INSERT INTO sales(cus_id,item_id,stock_id,receipt_no,qty,unit_price,discount,amount,user_id,purchase_id)
SELECT C.cus_id, I.item_id, S.stock_id, receipt_no='$receiptNo',ST.qty, ST.unit_price, ST.discount, ST.amount, U.id, P.purchase_id
FROM customers C, items I, stock S, sales_temp ST, users U, purchase_items P
WHERE ST.staff='$user' AND
C.customer_name='$customer' AND
I.item_name=ST.item_name AND
S.stock_code=ST.stock_code AND
ST.purchase_id=P.purchase_id

Answer

I have fixed the above issue.

Edited code is below:

INSERT INTO sales(cus_id,item_id,stock_id,receipt_no,qty,unit_price,amount,user_id,purchase_id) 
SELECT C.cus_id, I.item_id, S.stock_id, $receipt_no, ST.qty, ST.unit_price, ST.amount, U.id, P.purchase_id 
FROM customers C, items I, stock S, sales_temp ST, users U, purchase_items P 
WHERE ST.staff='$user' 
AND C.customer_name='$customer' 
AND I.item_name=ST.item_name 
AND S.stock_code=ST.stock_code 
AND ST.purchase_id=P.purchase_id
Comments