duanjiancong4860
2017-01-13 02:05
浏览 44
已采纳

将PHP数组插入MYSQL

I have a html table that is converted to JSON and sent over via AJAX to PHP. I've decoded the JSON object and now have a PHP array. I'd like to insert this array or rather the contents of it into a MySQL database.

I'm confused on some of the methodologies out there. what is the ideal method to prevent SQL injection? I see a number of methods that look like so;

 foreach ($array as $key => $value) 
    {
        $price = $array["price"][$key];
        ...................................
    }

this doesn't seem to work, or at least I can't get php to print or echo out what $price is.

I've tried this:

     foreach ($array as $key => $value) 
         {
           $price = $value["price"];
          ...................................
         }

This i can get echo or print_r to display the value.

First question is: what is the purpose of $key here in terms of inserting these values into MySQ? Second is: why doesn't the first one return the expected result, like that of the second code snippet? echo should display the value of $price? Lastly, from this assuming both methods are valid, an insert statement like below is correct for actually pushing the data into MySQL?

       $sql = mysql_query("insert into Daily_Requests values ('','$price','$item','$etc...','$etc....')");

Regards

EDIT

Here is my JSON:

[{"Price":"5000","Manufacturer":"Newton","Model":"84x26x10 43u","Model_info":"Newton 84x26x10 43u","Type":"Rack","Height_in":"83.97637795","Height_mm":"2133","Width_in":"25.98425197","Width_mm":"660","Description":"Newton 84x26x10 43u","Depth_in":"10","Depth_mm":"254","Mount_Type":"Floor Mount","Rack_UNITS":"43","Rack_INSIDE_HEIGHT_mm":"1911","Rack_INSIDE_WIDTH_mm":"584","Rack_INSIDE_DEPTH_mm":"","ASSET_TYPE":"Rack","Phases":"","Status":"","Date":"2017-01-11","Submitted":"","Image File / Web Info":"","Site":"Orlando"}]

图片转代码服务由CSDN问答提供 功能建议

我有一个转换为JSON的html表,并通过AJAX发送给PHP。 我已经解码了 JSON对象,现在有一个PHP数组。 我想将这个数组或其内容插入到MySQL数据库中。

我对某些方法感到困惑 那么。 什么是防止SQL注入的理想方法? 我看到许多类似的方法;

  foreach($ array as $ key => $ 价值)
 {
 $ price = $ array [“price”] [$ key]; 
 ..........................  ......... 
} 
   
 
 

这似乎不起作用,或者至少我不能让php打印或回显 价格是多少。

我试过这个:

  foreach($ array as $ key => $ value)  
 {
 $ price = $ value [“price”]; 
 ................................  ... 
} 
   
 
 

我可以使用echo或print_r来显示值。

第一个问题是: w ^ 在将这些值插入MySQ方面,这里是$ key的目的是什么? 第二个:为什么第一个没有返回预期结果,就像第二个代码片段那样? echo应该显示$ price的值吗? 最后,假设两种方法都有效,这样的插入语句对于实际将数据推送到MySQL是否正确?

  $  sql = mysql_query(“插入Daily_Requests值('','$ price','$ item','$ etc ...','$ etc ....')”); 
  <  / pre> 
 
 

问候

编辑

这是我的JSON:

  [{“Price”:“5000”,“Manufacturer”:“Newton”,“Model”:“84x26x10 43u”,“Model_info”:“Newton 84x26x10 43u”,“Type”  :“Rack”,“Height_in”:“83.97637795”,“Height_mm”:“2133”,“Width_in”:“25.98425197”,“Width_mm”:“660”,“Description”:“Newton 84x26x10 43u”,“Depth_in”  :“10”,“Depth_mm”:“254”,“Mount_Type”:“Floor Mount”,“Rack_UNITS”:“43”,“Rack_INSIDE_HEIGHT_mm”:“1911”,“Rack_INSIDE_WIDTH_mm”:“584”,“Rack_INSIDE_DEPTH_mm”:  “”,“ASSET_TYPE”:“机架”,“阶段”:“”,“状态”:“”,“日期”:“2017-01-11”,“已提交”:“”,“图像文件/网络信息 “:”“,”网站“:”奥兰多“}] 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题