数据保存:使用Ajax和Json写入MySQL数据库

时间:2015-02-24 18:11:31

标签: javascript php mysql ajax json

我完全迷失了。怎么了?我尝试INSERT INTO mysql。

它在MySQL中添加了一行,但它没有填充数据。

我坐了几天就不知道了。我检查了数百个网页。

如何将数据发送到PHP部分?这段代码有什么问题?

...

以下是完整代码:

使用Javascript:

function jsRecordInsertWrite()
{

    var jsObject = {
        "ID": document.form_articles.ID.value,
        "Item": document.form_articles.Item.value,
        "ItemNo": document.form_articles.ItemNo.value,
        "Material": document.form_articles.Material.value,
        "Age": document.form_articles.Age.value,
        "ItemSize": document.form_articles.ItemSize.value,
        "Price": document.form_articles.Price.value,
        "Info": document.form_articles.Info.value,
        "InfoRed": document.form_articles.InfoRed.value,
        "ArrivalDate": document.form_articles.ArrivalDate.value,
        "ArrivalDateShown": document.form_articles.ArrivalDateShown.value,
        "MainPicLink": document.form_articles.MainPicLink.value,
        "ItemCondition": document.form_articles.ItemCondition.value,
        "ItemTimestamp": document.form_articles.ItemTimestamp.value,
        "ItemCategory": document.form_articles.ItemCategory.value
    };

    // ... the AJAX request is successful
    var updatePage = function (response) {
        alert("insert record successful");
    };
    // ... the AJAX request fail
    var printError = function (req, status, err) {
        alert("insert record failed");
    };
    // Create an object to describe the AJAX request
    $.ajax({
        url        : 'insertarticle.php',
        dataType   : 'json',
        contentType: 'application/json; charset=UTF-8', 
        // This is the money shot
        data       : jsObject,
        type       : 'POST',
        success: updatePage,
        error: printError
    });
  }

insertarticle.php

<?php

 $link = mysql_connect('localhost', 'admin0', 'star1star1star0');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('sob', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}

//read the json file contents
$ID =  $_POST['ID'];
$Item =  $_POST['Item'];
$ItemNo =  $_POST['ItemNo'];
$Material =  $_POST['Material'];
$Age =  $_POST['Age'];
$ItemSize =  $_POST['ItemSize'];
$Price =  $_POST['Price'];
$Info =  $_POST['Info'];
$InfoRed =  $_POST['InfoRed'];
$ArrivalDate =  $_POST['ArrivalDate'];
$ArrivalDateShown  =  $_POST['ArrivalDateShown'];
$MainPicLink =  $_POST['MainPicLink'];
$ItemCondition =  $_POST['ItemCondition'];
$ItemTimestamp =  $_POST['timestamp'];
$ItemCategory =  $_POST['ItemCategory'];

//insert into mysql table
$sql = "INSERT INTO articles(ID, Item, ItemNo, Material, Age, ItemSize,
 Price, Info, InfoRed, ArrivalDate, ArrivalDateShown, MainPicLink, 
ItemCondition, ItemTimestamp, ItemCategory)   
VALUES(NULL,'$Item','$ItemNo','$Material','$Age',
'$ItemSize','$Price','$Info','$InfoRed','$ArrivalDate',
'$ArrivalDateShown','$MainPicLink','$ItemCondition',
'$ItemTimestamp','$ItemCategory')";

if(!mysql_query($sql))
{
die('Error : ' . mysql_error());
}

//database connection close
mysql_close($link);

//}

?>

//+++++++++++++++++++++++++++++++++++++++++++++++++
//The first NULL is for  autoincrement ID,
//the other NULL is for automatic timestamp 

0 个答案:

没有答案