如何在MySQL插入语句中包含java脚本变量值

时间:2017-11-20 20:52:33

标签: javascript mysql database edi

我在Java脚本变量的帮助下获取了一些值。我试图在Bizmapper(Bizlink)中将这些值插入到DB(MySQL)中,但它会引发一些错误:

[Error is --> " Failed to execute SQL insert into po_ref_table values (4200913801AA, DEAM, 067,Joseph Keefe,jkeefe@synovos.com,W.W. Grainger, Inc.) failed to execute. Reason: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Keefe,jkeefe@synovos.com,W.W. Grainger, Inc.)' at line 1."].

我尝试过以下提到的方式:

MySQL_DB.open(true);

// Get necessory data //
var erp_PONum        = mek.targetDocument.getRootSegment("ST").getSegment("BEG").getEDIElement("BEG03").getData();
var erp_OriginSystem = mek.targetDocument.getRootSegment("ST").getSegment("BEG").getRecord("WFG_BEG").getField("WF_OriginSystem").getData();
var erp_SiteCode     = mek.targetDocument.getRootSegment("ST").getSegment("BEG").getRecord("WFG_BEG").getField("WF_SiteCode").getData();
var erp_BuyerName    = mek.targetDocument.getRootSegment("ST").getSegment("BEG").getRecord("WFG_BEG").getField("WF_BuyerName").getData();
var erp_BuyerEmail   = mek.targetDocument.getRootSegment("ST").getSegment("BEG").getRecord("WFG_BEG").getField("WF_BuyerEmail").getData();
var erp_SupplierName = mek.targetDocument.getRootSegment("ST").getSegment("BEG").getRecord("WFG_BEG").getField("WF_SupplierName").getData();

//Inser into Table//

MySQL_DB.command("insert into po_ref_table Values ("+ erp_PONum +", "+ erp_OriginSystem +", "+ erp_SiteCode +","+ erp_BuyerName +","+ erp_BuyerEmail +","+ erp_SupplierName +")");

//Close DB//

MySQL_DB.close();

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

小心SQL注入漏洞!在查询中使用参数持有者。

var args = [erp_PONum, erp_OriginSystem, erp_SiteCode, erp_BuyerName, erp_BuyerEmail, erp_SupplierName];
MySQL_DB.command("insert into po_ref_table Values (?,?,?,?,?,?)",[args]);