Blogger Information
Blog 19
fans 0
comment 6
visits 19182
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php提交数据到SQLserver数据库
葵花宝典
Original
1210 people have browsed it

PHP提交数据到SQLserver数据库做复杂计算

应用场景:

生产领料生成功能:
1.生成领料表头,表体
2.回写生产订单表体中的已领量,做累加计算
3.如果领用完成,标记生产订单表头,不让再次领料

以前复杂数据写入数据库都是用特殊字符分割,传到SQL中后,调用函数来处理成表格,再以这个表格展开计算.现在不用这么麻烦了,SQLserver2016版本中提供了解晰JSON的函数,减化了我们的工作

  • 前端提交表单,以JSON格式提交给PHP后台接口
    示例图片

    这里的代码,用的是layUI的表格,直接提取就是数组,POST提交到后台

  • PHP处理前端传入的JSON,组装成SQL存储过程解晰的格式
    php接收处理时,不要拼接SQL,用参数化查询,大概如下示例:

    1. //配料保存本地
    2. case 'saveML':
    3. //组装JSON,格式要求:{'主表键值 ',[{'子表行项目'},{'子表行项目'}]}
    4. $arr = ['orderCode'=>$_POST['ml']['orderCode'],'prdCode'=>$_POST['ml']['prdCode'],'prdName'=>$_POST['ml']['prdName'],"mls"=>$_POST['mls']];
    5. //此处建义参数化查询,安全,可能需要提交特殊字符
    6. $sql = "exec PR_pdAddML ?";
    7. $pram = [json_encode($arr)];
    8. //调用存储过程,此处两个静态方法是专搞SQL连接和执行的
    9. $link = Sqlserver::connect($ssoSystem);
    10. $re = Sqlserver::getLists($link,$sql,$pram);
    11. //结果判断
    12. if($re[0]['re']==1)
    13. {
    14. echo json_encode(["state"=>1,"msg"=>"success"]);
    15. }
    16. else{
    17. echo json_encode(["state"=>0,"msg"=>"fail"]);
    18. }
    19. break;
  • SQL存储过程计算,将结果返回给PHP后台
    存储过程在计算时,要启用事务

    1. USE [ApiServer]
    2. GO
    3. /****** Object: StoredProcedure [dbo].[PR_pdAddML] Script Date: 2021-01-22 15:20:07 ******/
    4. SET ANSI_NULLS ON
    5. GO
    6. SET QUOTED_IDENTIFIER ON
    7. GO
    8. -- =============================================
    9. -- Author: <Author,,jackli>
    10. -- Create date: <Create Date,2020-01-21,>
    11. -- Description: <Description,配料保存本地,更新已领,领用完成,配料完成,>
    12. -- =============================================
    13. ALTER PROCEDURE [dbo].[PR_pdAddML]
    14. @ml nvarchar(max)
    15. AS
    16. BEGIN
    17. declare @mlCode nvarchar(50) = newID() --配料单号
    18. declare @orderCode nvarchar(50) --订单号
    19. declare @potCode nvarchar(50) --拆分批次锅号
    20. declare @re int = 0 --返回结果
    21. set nocount on
    22. --json生成表
    23. SELECT orderCode,prdCode,prdName,item,potCode,prdCodes,prdNames,rsvQty,batCode,wh,qty,unit,ordersItem,reservation,rsItem into #pdml
    24. FROM OPENJSON(@ml)
    25. WITH (
    26. orderCode nvarchar(50) '$.orderCode',
    27. prdCode NVARCHAR(200) '$.prdCode',
    28. prdName NVARCHAR(200) '$.prdName',
    29. mls NVARCHAR(MAX) '$.mls' AS JSON
    30. )
    31. OUTER APPLY OPENJSON(mls)
    32. WITH (item NVARCHAR(50) '$.id',
    33. potCode NVARCHAR(50) '$.potCode',
    34. prdCodes nvarchar(50) '$.prdCodes',
    35. prdNames NVARCHAR(50) '$.prdNames',
    36. rsvQty decimal(18,3) '$.rsvQty',
    37. batCode NVARCHAR(50) '$.batCode',
    38. wh NVARCHAR(50) '$.wh',
    39. qty decimal(18,3) '$.qty',
    40. unit NVARCHAR(50) '$.unit',
    41. ordersItem NVARCHAR(50) '$.item',
    42. reservation NVARCHAR(50) '$.reservation',
    43. rsItem NVARCHAR(50) '$.rsItem');
    44. --取订单号
    45. select top 1 @orderCode=orderCode,@potCode=potCode from #pdml
    46. --启用事务
    47. begin tran mytran
    48. begin try
    49. --写配料表头
    50. insert rc_pdML(mlCode,orderCode,prdCode,prdName,createTime)
    51. select top 1 @mlCode,orderCode,prdCode,prdName,GETDATE() from #pdml
    52. --写配料表体
    53. insert rc_pdMLs(mlCode,item,potCode,prdCode,prdName,rsvQty,batCode,wh,qty,unit,ordersItem,reservation,rsItem)
    54. select @mlCode,item,potCode,prdCodes,prdNames,rsvQty,batCode,wh,qty,unit,ordersItem,reservation,rsItem from #pdml
    55. --更新订单已领量
    56. update rc_pdOrders set qty = b.qty from rc_pdOrders a inner join
    57. (select reservation,ordersItem,SUM(qty) as qty from #pdml group by reservation,ordersItem) b
    58. on a.reservation = b.reservation and a.item=b.ordersItem
    59. --不允许再次配料
    60. update rc_pdOrders set clo=1 where potCode=@potCode and orderCode = @orderCode
    61. --全部配料后关闭订单
    62. declare @potNum int,@oldNum int
    63. select @potNum=count(1) from (select potCode from rc_pdOrders where orderCode=@orderCode group by potCode)b
    64. select @oldNum=count(1) from (select b.potCode from rc_pdML a inner join rc_pdMLs b on a.mlCode=b.mlCode where a.orderCode=@orderCode group by potCode)c
    65. if @potNum = @oldNum
    66. begin
    67. update rc_pdOrder set endMark=1 where orderCode=@orderCode
    68. end
    69. set @re=1
    70. commit tran --执行事务前设置返回值为1
    71. end try
    72. begin catch
    73. rollback tran --回滚事务
    74. end catch
    75. --事务结束 开启SQL消息记录
    76. drop table #pdml
    77. set nocount off
    78. select @re as re
    79. END
  • PHP后台将结果返回给前端,跳出页面提示
    成功返回1,失败返回0,前端根据结果提示用户
  1. parm = JSON.stringify(parm);
  2. $.ajax({
  3. type: "POST",
  4. dataType: "json",
  5. url: "/ajaxServer/ajaxServer.php",
  6. data: { parm: parm },
  7. beforeSend: function () {
  8. //请求等待时动图,本地太快了基本没效果
  9. this.layerIndex = layer.load(0, { shade: false });
  10. },
  11. complete: function () {
  12. //请求完成关闭动画
  13. layer.close(this.layerIndex);
  14. },
  15. success: function (re) {
  16. console.log(JSON.stringify(re.msg));
  17. //let state = JSON.parse(re)["state"];
  18. if (re.state == 1) {
  19. layer.msg("保有存成功:" + re.msg, { icon: 1 });
  20. } else {
  21. layer.msg("保存失败:" + re.msg, { icon: 2 });
  22. }
  23. },
  24. });
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post