【问题标题】:Is it possible to save data from local storage to the databse mssql是否可以将数据从本地存储保存到数据库 mssql
【发布时间】:2018-06-15 03:46:39
【问题描述】:

我从不同的页面收集数据并存储在本地存储中。我想把它保存到数据库MySQL中

我的 javascript 代码:

<script>
   $('#inscrit').click(function(){
    var email = $("#email").val();
    var ip_offre = localStorage.getItem('offre_ip');
    var offre= localStorage.getItem('offre');
    var pays = localStorage.getItem('pays');
    var nombre = localStorage.getItem('nbr_sms');
    var tot = localStorage.getItem('totale');

    $.ajax({
        type: "POST",
        url: "get_data_front.php",
        data: {email: email,ip_offre: ip_offre, offre: offre, pays: pays, nombre: nombre, tot: tot},

        success: function (data) {


       },
        error: function (xhr) { // if error occured
          alert("Error occured.please try again" + xhr);
        }
      });




        });
 </script>

我的 php 代码“get_data_front.php”

<?php

include_once "config.inc.php";
global $sqlserver; 
error_reporting(E_ALL);
ini_set('display_errors', 1);

$email=$_POST['email']; 
$ip_plan=$_POST['ip_offre'];
$plan=$_POST['offre']; 
$pays=$_POST['pays']; 
$nbr=$_POST['nombre'];  
$totale=$_POST['tot']; 


$sqlinsert = $sqlserver->prepare("insert into t_data_from_front 
                            (S_EMAIL,I_NB_SMS,S_PAYS,S_PRICE,S_PLAN,S_IP_PLAN,B_TRAITE)
                    VALUES($email,$nbr,$pays,$totale,$plan,$ip_plan,)");     
$resultinsert= $sqlinsert->execute() or die(var_dump($sqlinsert->errorInfo()));

?>

数据库中的插入已完成,但我得到所有字段 NULL 。 谢谢你们。

【问题讨论】:

  • 1) 计算列数,然后计算 VALUE 子句中的值。你错过了一个价值。这一定会产生错误。你确定插入工作正常吗?我怀疑!
  • 2) 您从错误显示中看到了什么?
  • @RiggsFolly 数据验证失败

标签: javascript php local-storage


【解决方案1】:

您应该将您的 json 更改为:

{'email': email,'ip_offre': ip_offre, 'offre': offre, 'pays': pays, 'nombre': nombre, 'tot': tot}

您所做的是参数名称不是电子邮件,而是对象电子邮件的值。 所以如果:

var email = 'test@test.com'

您发送的 json 实际上是:

{'test@test.com': email,'ip_offre': ip_offre, 'offre': offre, 'pays': pays, 'nombre': nombre, 'tot': tot}

【讨论】:

  • 我得到的只是 $email=$_POST['email']; 的未定义索引; ...当我使用 var_dump($email) NULL
  • 这意味着您没有在帖子中发送电子邮件值。
【解决方案2】:

您的查询:

"insert into t_data_from_front(S_EMAIL,I_NB_SMS,S_PAYS,S_PRICE,S_PLAN,S_IP_PLAN,B_TRAITE)VALUES($email,$nbr,$pays,$totale,$plan,$ip_plan,)"

缺少一个参数,您的说明,7 列:

S_EMAIL,I_NB_SMS,S_PAYS,S_PRICE,S_PLAN,S_IP_PLAN,B_TRAITE

并且仅通过 6:

$email,$nbr,$pays,$totale,$plan,$ip_plan,

【讨论】:

  • 是的,但我得到的只是未定义的索引 $email=$_POST['email']; $ip_plan=$_POST['ip_offre']; .....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 2012-01-15
  • 2018-11-17
相关资源
最近更新 更多