doubi9615 2015-08-31 10:15
浏览 21

无法将本地存储上载到数据库

I have some data that I want to store in Database for synchronization. Here is my jQuery code:

$(document).ready(function() {

  var local_array = [];

  for ( var i = 0, len = localStorage.length; i < len; ++i ) {
    local_array.push(localStorage.key(i));
    local_array.push(localStorage.getItem(localStorage.key(i)));
    console.log(local_array);
  } 

  $.ajax({
    type: "POST",
    url: "uploadtodatabase.php",
    data: {data: local_array}
  });
});

This is my PHP code:

  <?php session_start();
  $connection = new mysqli('host', 'name', 'password', 'database');
  $username   = $_SESSION['user'];
  $username   = $connection->real_escape_string($username);
  $query      = $connection->query("SELECT * from members WHERE username ='$username'");
  $matches    = mysqli_num_rows($query);

  if ($matches == 1) {
  $row     = mysqli_fetch_array($query);
  $visited = $_POST['data'];
  $visited = $connection->real_escape_string($visited);
  $connection->query("UPDATE members SET visited = '$visited' WHERE username ='$username'");
  } 

  mysqli_close($connection);
  ?>

The data is stored in local_array , as I checked in console. The jQuery code in is footer.php. This is the same location where I store uploadtodatabase.php. However, footer.php is appended to other files which might not be in same directory. Can this cause the problem? I have never used AJAX before. So, I might be doing something stupid and not notice it at all.

  • 写回答

1条回答 默认 最新

  • duanpei8853 2015-08-31 11:09
    关注

    Hi you are half way there, you just need to decode the array inside of your php code...

    if(!empty($_POST['data'])) {
        $data = explode(',',$_POST['data']);
        //code to store $data content
    }
    

    now you can use the $data variable to store the contents of the session from localSotrage

    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私