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"}]
  • 写回答

2条回答 默认 最新

  • dongluan2612 2017-01-13 02:45
    关注

    Once you enter the loop

    foreach ($array as $key => $value) {
        #loop entered here after open
    
        #each value of the array is accessible
    
        #end loop here before close
         }
    

    With $key being the index value of the array as an integer and $value as the actual variable holding the value at that index. $array inside the loop is the same as outside loop. $value represents the slice of that array.

    once you load the data into a new variable as $price = $value["price"] it is available until the close of the loop and resets to the new value when looped over again.

    So your SQL statement out of the loop will contain the last values held by $array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示