使用jSON插入数据库

时间:2012-11-03 04:51:37

标签: php jquery database json

尝试使用表单和JSON向我的数据库添加内容。我的查询运行良好通过phpmyadmin,但由于某种原因我的PHP文件无法从我的表单接收信息。

(请保存我典型的“不要使用mysql函数”。)

Chrome的网络预览向我展示了这一点:

      data: null
      debug: "SQL query was: INSERT INTO linktb (catName, title, desc, URL) VALUES ('1', '', '', '');<br>SQL query failed <br>Other output: "
      retval: 2

调试我的update.php:

      {"retval":2,"data":null,"debug":"SQL query was: INSERT INTO linktb (catName, title, desc, URL) VALUES ('', '', '', '');
      SQL query failed 
      Other output: "}

HTML&amp; JS:

      <form method="post" id="addURL" name="addURL" method="post">
            <select id="catList" name="catList">
            <option value="1">Milk</option>
            <option value="2">Coffee</option>
            <option value="3">Tea</option>
            </select><br />
            <input id="title" name="title" type="text" placeholder="Title"><br />
            <input id="desc" name="desc" type="text" placeholder="Description"><br />                
            <input id="URL" name="URL" type="text" placeholder="URL"><br />
            <input id="key" name="key" type="text" placeholder="Key"><br />
            <input type="submit" value="Submit">
      </form>


<script type="text/javascript">
$(document).ready(function(){   
    $("#addURL").submit(function(e) {
        e.preventDefault();
        var action = $("#addURL").attr('action');
        var form_data = {
            catList: $("#catList").val(),
            title: $("#title").val(),
            desc: $("#desc").val(),
            URL: $("#URL").val(),
            key: $("#key").val(),
        };
    $.getJSON("update.php",form_data,function(data){
        switch(data.retval){
            case 0: $("#status").html("Unable to update!");
                    $("#status").css("background-color","red");
            break;
            case 1: $("#status").html("Update successful!");
                    $("#status").css("background-color","green");
            break;
            default: $("#status").html("Database error, please try again.");
                    $("#status").css("background-color","red");
            break;
        }
    });
});         
});

PHP:

$json = array("retval" => 2, "data" => NULL, "debug" => "");

$catList = mysql_real_escape_string($_REQUEST['catList']);
$title = mysql_real_escape_string($_REQUEST['title']);
$desc = mysql_real_escape_string($_REQUEST['desc']);
$URL = mysql_real_escape_string($_REQUEST['URL']);
$key = mysql_real_escape_string($_REQUEST['key']);


$sql = "INSERT INTO linktb (catName, title, desc, URL) VALUES ('".$catList."', '".$title."', '".$desc."', '".$URL."');";

$json['debug'] .= "SQL query was: ".$sql."<br>";
$result=mysql_query($sql);
if (!$result) {
    $json['debug'] .= "SQL query failed <br>";
    $json['debug'] .= "Other output: ". ob_get_contents();
    ob_end_clean();
    die(json_encode($json));
}
$count=mysql_num_rows($result);

if($count==1){
    $json['retval'] = 0;
    $json['data'] = mysql_fetch_assoc($result);
} else {
    $json['retval'] = 1;
}
$json['debug'] .= "Other output: ". ob_get_contents();
ob_end_clean();
echo json_encode($json);

2 个答案:

答案 0 :(得分:1)

好好在这里捅一下。您似乎没有提交表单。我假设你想通过ajax这样做。

 $("#addURL").submit(function(e) {
    e.preventDefault();
    //now what? try serializing your form and sending it via json
    $formData = $(this).serialize()
    $.ajax(url, {
        data : JSON.stringify($formData),
        dataType: 'json',
        contentType : 'application/json',
        type : 'POST'}
    );
 };

答案 1 :(得分:0)

Javascript&amp;我使用的HTML很好,结果我在MYSQL中使用了“desc”这个词代表了降序而不是我想用它的var。