0

尝试使用表单和 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 & 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);
4

2 回答 2

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'}
    );
 };
于 2012-11-03T05:08:25.357 回答
0

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

于 2012-11-04T20:36:55.917 回答