【问题标题】:Issue in Inserting using JSON使用 JSON 插入时的问题
【发布时间】:2015-11-13 13:52:05
【问题描述】:

我正在尝试像这样插入数据

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";
?>

问题是我在$mysqli-&gt;query($insert); 上遇到错误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

我应该怎么做才能让它运行。我已经尝试了一切,但找不到任何可行的解决方案。请帮忙!!提前致谢

【问题讨论】:

  • 回显 $insert,我猜语法不正确,好的,检查你的#result div 它显示的内容
  • $data 已经是一个关联数组,因此使用 implode 会生成一个与您想的不同的字符串,只需在执行查询之前 print_r() $data,您会发现它不是按照SQL 语法

标签: php jquery mysql ajax json


【解决方案1】:

有一些string 值。您需要正确使用's。

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

说明

您正在使用的代码正在生成类似 - VALUES ('name, email, phone, message') 的内容,这在语法上是错误的。它们都是strings,需要包裹在's中。

('" . implode("', '", $data) . "') - 将它们正确包裹起来。它将生成 - VALUES ('name', 'email', 'phone', 'message')

【讨论】:

  • 我是 PHP 新手,能否解释一下 implode。
【解决方案2】:

试试这个:

$postdata =  implode("','", $data) ;

$insert = "INSERT INTO contact(Name,Email,Phone,Message) VALUES ('$postdata')";

【讨论】:

  • 你需要去掉$postdata周围的's
  • 是的 @b0s3 是正确的。这将在没有 $postdata 的情况下工作
【解决方案3】:

函数 implode 将数组元素与字符串连接起来。现在你的函数是implode(",",$data),它将使字符串变成这样

NameData,EmailData,PhoneData,MessageData 

而你会希望它变成这样

'NameData','EmailData','PhoneData','MessageData'

为此,您需要使您的内爆函数看起来像这样implode("','",$data),并且第一个和最后一个's 已经从您编写的代码中传入。

【讨论】:

  • 哇,我要找的东西很好的解释
猜你喜欢
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 2020-11-10
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多