weixin_33727510 2015-08-20 04:17 采纳率: 0%
浏览 33

使用JSON插入时出现问题

I am trying to insert the data like this

function submitData() {
//Posting to ContactDB with JSON format
$.post("contactDB.php",
    JSON.stringify({ 
          name: $("#name").val(),
          email: $("#email").val(),
          phone: $("#phone").val(),
          message: $("#message").val() 
    }),
    function(usrava){
        // if data is inserted successfully than show insert success message in Result div
        if(usrava=='Data Inserted')
        {
            $("#result").fadeTo(200,0.1,function(){ 
                $(this).html('Your message is successfully saved with us. We will get back to you soon.').fadeTo(900,1);
            });     
        }
        //else show the error 
        else
        {
            $("#result").fadeTo(200,0.1,function(){ 
                $(this).html(usrava).fadeTo(900,1);
            });
        }
    });
}   

ContactDB.php

<?php
    $mysqli = new mysqli("localhost", "root", "", "contactDB"); //Connection to the Database
    //If Error than die
    if (mysqli_connect_errno()) { 
        die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
        //Echo for the response 
        echo "Data base connection NOT Successful. Please get the assistance from your Administrator.";
        //Should not go out if not connected
        exit();
    }
    //data received in json. decoded it to an array
    $data = json_decode(file_get_contents('php://input'), true);
    //Create a insert Command using implode as the data is already in the array
    $insert = "INSERT INTO contact(Name,Email,Phone,Message) VALUES ('" .implode(",",$data)."')";
    $mysqli->query($insert);
    //Close the connection
    $mysqli->close();
    // Echo for the response
    echo "Data Inserted";
?>

The issue is I am getting error on $mysqli->query($insert); that You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near Rajesh, at line 1

What should I do to make it run. I have tried everything but could not find any working solution. Please Help!! Thanks in advance

  • 写回答

3条回答 默认 最新

  • weixin_33725722 2015-08-20 04:26
    关注

    There are some string values. You need to use the 's properly.

    VALUES ('" .implode("','",$data)."')"
    

    Explanation

    The code you are using is generating something like - VALUES ('name, email, phone, message') which is wrong in syntax. They all are strings and need to wrapped in 's.

    ('" . implode("', '", $data) . "') - will wrap them up properly. It will generate - VALUES ('name', 'email', 'phone', 'message')

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)