javascript - How to inserting json stringify object into database with php mysqli -
hi guy's need help. try create function inserting data json stringify object mysql database use php mysqli.
the json stringify data got code
$(document).ready(function(){ $("#submit-file").click(function(){ var myfile = $("#files")[0].files[0]; var json = papa.parse(myfile, { skipemptylines: true, complete: function(results) { var serialize = json.stringify(results); $.ajax({ type: "post", url: "../php/tagihan/save_data.php", data: {array:serialize}, cache: false, success: function(data) { console.log(data); } }); } }); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/papaparse/4.3.5/papaparse.min.js"></script>
the data view enter image description here
the php code far try this
<?php include("../../connections/koneksi.php"); $array = json_decode( $_post['array'] ); $p1 = $array; foreach($p1 $val) { $sql = "insert table(subject,message) values('".$val['subject']."', '".$val['message']."')"; $result = $mysqli->query($sql); } ?>
js code:
$(document).ready(function(){ $("#submit-file").click(function(){ var myfile = $("#files")[0].files[0]; var json = papa.parse(myfile, { skipemptylines: true, complete: function(results) { var serialize = json.stringify(results); $.ajax({ type: "post", url: "../php/tagihan/save_data.php", data: serialize, cache: false, success: function(data) { console.log(data); } }); } }); }); });
php:
<?php include("../../connections/koneksi.php"); $array = json_decode(file_get_contents("php://input")); foreach($array $val) { if ($stmt = $mysqli->prepare('insert table(subject,message) values (?,?)')) { $stmt->bind_param("ss", $val['subject'], $val['message']); $stmt->execute(); $stmt->close(); } } ?>
Comments
Post a Comment