简体   繁体   English

PHP-将相同字段插入mysql

[英]PHP - insert same field into mysql

PHP - Inserting Into Xampp/ Mysql PHP-插入Xampp / Mysql

iam making product database with insert php like this IAM制作产品数据库,像这样插入PHP

  $save_sale = mysql_query("INSERT INTO sale(no_receipt,no_confirm,time,date,day,total) values
         ('$_POST[no_receipt]',
          '$_POST[no_confirm]',
          '$hour_now',
          '$date_now',
          '$day_now',
          '$_POST[total]')")
         or die(mysql_error());
  $save_sale_detail =  mysql_query("INSERT INTO sale_detail(no_receipt,id_product,quantity,price,subtotal) values
         ('$_POST[no_receipt]',
          '$_POST[id_product]',
          '$_POST[quantity]',
          '$_POST[price]',
          '$_POST[subtotal]')")
         or die(mysql_error());

but it just save one transaction every time i submit multipple product order with different quantity, price and subtotal. 但每次我提交数量,价格和小计不同的多个产品订单时,它只节省一笔交易。 i was meaning something like in sale saving and create no_receipt and then sale_detail create no_receipt too following sale . 我的意思是在销售储蓄类似,创造no_receipt然后sale_detail创建no_receipt以下销售过。 example : Sale 示例:销售

     No. Receipt  = 001
     No. Confirm  = 205850 <-- it just confirmation order
     hour         = 17.00
     date         = 31/08/2013
     day          = saturday
     total        = $770 <-- subtotal calculation

     sale_detail
     No. Receipt  = 001
     id_product   = 1 <-- let say it's a hat
     quantity     = 3
     price        = $150 <-- price by each product
     subtotal     = $450 <-- price * quantity
     ----- and come other product order ---
     sale_detail
     No. Receipt  = 001
     id_product   = 4 <-- let's say it's a glasses
     quantity     = 4
     price        = $80 <-- price by each product
     subtotal     = $320 <-- price * quantity
     ----- and come other.. ---------------

but i stuck and just can saving one transaction, if sale _detail success to saving, the sale still empty. 但是我卡住了,只能保存一笔交易,如果将_detail成功保存成功,则销售仍然为空。

any help would be appreciate.. 任何帮助将不胜感激。

You need to POST the per-product details as an array. 您需要将每个产品的详细信息作为数组发布。 In the markup you would use <input name="id_product[]"..../> 在标记中,您将使用<input name="id_product[]"..../>

This will then be received in PHP as an array ( echo gettype($_POST['id_product']) will output array ). 然后,它将在PHP中作为数组接收( echo gettype($_POST['id_product'])将输出array )。

Then you can loop through with foreach ($_POST['id_product'] as $id) or something else, and call your INSERT INTO sale_detail... query multiple times (once per loop iteration). 然后,您可以使用foreach ($_POST['id_product'] as $id) INSERT INTO sale_detail... foreach ($_POST['id_product'] as $id)或其他内容进行INSERT INTO sale_detail... ,并多次调用INSERT INTO sale_detail...查询(每次循环迭代一次)。

Note that you only want to put [] in the name attribute of the values that actually are per-item. 请注意,您只想将[]放在每个项目实际值的name属性中。 For example the no_invoice field is a single value, not an array, so you would just use name="no_invoice" and not name="no_invoice[]" . 例如, no_invoice字段是单个值,而不是数组,因此您只使用name="no_invoice"而不是name="no_invoice[]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM