我有以下代码将 AJAX POST 发送到我服务器上的 php 文件以将行添加到 MySQL 表中。当用户将某个项目标记为他们的最爱时,此函数将运行:
$.ajax({
type: 'POST',
data: 'school='+school+'&token='+token,
url: 'http://www.codekraken.com/testing/snowday/database9.php',
success: function(data){
console.log(data);
},
error: function(){
console.log(data);
}
});
我也有一个类似的功能,当他们删除收藏夹时会运行:
data: 'school=&token='+token,
这有效,但并非始终如一。PHP代码如下:
<?php
$server = "localhost";
$username = "";
$password = "";
$database = "";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);
$token = $_POST["token"];
$school = $_POST["school"];
if (strlen($token) == 0){
die('Incorrect token');
}
if (strlen($school) == 0){
die('Invalid school name');
}
$sql = "INSERT INTO Snow (Token, School, Skip) ";
$sql .= "VALUES ('$token', '$school', '0') ON DUPLICATE KEY UPDATE School='$school'";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
} else {
echo "$token, $school = Success";
}
mysql_close($con);
?>
我的意思是它不能始终如一地工作是,如果我将一个项目标记为收藏,它将创建一行。如果我取消标记它,它将变为$school
无。如果我随后标记不同的项目,它将更改为该项目。但是,如果,我尝试将第一个项目标记为收藏,它不会更新,但会保留最后的更改。
因此,似乎它只是在每次发生新事情时才更新行,但如果该值已经存在,即使它当前不存在,也不会更新该行。
注意:列Token
被索引为UNIQUE PRIMARY
相关的 JS 代码var school
:
function setFavorite() {
var threshold = {
x: 30,
y: 10
};
var originalCoord = {
x: 0,
y: 0
};
var finalCoord = {
x: 0,
y: 0
};
function touchMove() {
finalCoord.x = event.targetTouches[0].pageX;
changeX = originalCoord.x - finalCoord.x;
var changeY = originalCoord.y - finalCoord.y;
if (changeY < threshold.y && changeY > (threshold.y * -1)) {
changeX = originalCoord.x - finalCoord.x;
if (changeX > threshold.x) {
window.removeEventListener('touchmove', touchMove, false);
$(document).off("touchmove", ".row");
if ($(event.target).attr("class") === "row-inside") {
var element = $(event.target);
}
if ($(event.target).attr("class") === "row-l") {
var element = $(event.target).parent();
}
if ($(event.target).attr("class") === "row-r") {
var element = $(event.target).parent();
}
var text = $(element).find(".row-l").text();
var token = window.deviceToken;
var school = $(element).find(".row-l").text();
var favstatus = $(element).find(".row-r").text();
var thisStar = $(element).parent().find(".star-inside");
$(element).css("margin-left", "-75px");
if ($(thisStar).hasClass("favorite")) {
$.ajax({
type: 'POST',
data: 'school=&token=' + token,
url: 'http://www.codekraken.com/testing/snowday/database10.php',
success: function (data) {
console.log(data);
},
error: function () {
console.log(data);
}
});
$(".clear span").text("");
$(thisStar).removeClass("favorite");
localStorage.removeItem("favorite");
localStorage.removeItem("favorite-status");
} else {
$.ajax({
type: 'POST',
data: 'school=' + school + '&token=' + token,
url: 'http://www.codekraken.com/testing/snowday/database10.php',
success: function (data) {
console.log(data);
},
error: function () {
console.log(data);
}
});
$(".clear span").text("\"" + text + "\"");
localStorage.setItem("favorite", text);
localStorage.setItem("favorite-status", favstatus);
$(".star-inside").not(thisStar).removeClass("favorite");
$(thisStar).addClass("favorite");
}
setTimeout(function () {
$(element).css("margin-left", "0px");
}, 500);
setTimeout(function () {
$(document).on("touchmove", ".row", function () {
touchMove();
});
}, 800);
}
}
}
function touchStart() {
originalCoord.x = event.targetTouches[0].pageX;
finalCoord.x = originalCoord.x;
}
$(document).on("touchmove", ".row", function () {
touchMove();
});
$(document).on("touchstart", ".row", function () {
touchStart();
});
}
MySQL 输出:
CREATE TABLE `Snow` (
`Token` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`School` text COLLATE utf8_unicode_ci NOT NULL,
`Skip` int(1) NOT NULL,
PRIMARY KEY (`Token`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicod