我一直在查看其他问题,并根据相应的答案提供我的代码,但我似乎无法完成这项工作。如果有比我更有经验的人能帮我看清楚这一点,我将不胜感激。
jQuery的:
$(function () {
$(".going-decision").click(function () {
var clickBtnValue = $(this).val();
alert('clicked');
$.ajax({
type: "POST",
url: "http://localhost/townbuddies/public_html/event_going_ajax.php",
data: {'btn-clicked': clickBtnValue},
success: function () {
alert('success');
}
});
});
});
event_going_ajax.php:
if (isset($_POST['btn-clicked'])) {
$decision = $_POST['btn-clicked'];
//Logic that writes to database
}
我的HTML按钮确实有“正在做出决定”的课程。包含了jQuery库。
结果:点击了'警报正在显示,以及“成功”。一。但是,数据库未更新。
任何人都暗示这里发生了什么?感谢。
编辑,这是我的PHP代码。我知道它是非安全代码,我目前正在学习PHP。
<?php
session_start();
$user_id = $_SESSION['user'];
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'townbuddies');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (isset($_POST['btn-clicked'])) {
//Value will be in this format: 'decision.event_id'
$value = $_POST['btn-clicked'];
$length = strlen($value);
$findme = '.';
$pos = strpos($value, $findme);
//Separating the decision (going/not going) and the event_id
$decision = substr($value, 0, $pos);
$event_id = substr($value, $pos + 1);
//Verify if user is already 'going' to the event
$result = verifyDatabase($con, $user_id, $event_id);
//Depending on user decision
switch ($decision) {
case 'going':
going($con, $result, $user_id, $event_id);
break;
case 'not_going':
notGoing($con, $result, $user_id, $event_id);
break;
}
}
function verifyDatabase($con, $user_id, $event_id) {
//Verify if already in database
$sql = "SELECT * "
. "FROM user_events "
. "WHERE event_id = "
. "'$event_id'"
. "AND user_id = "
. "'$user_id'";
$result = mysqli_query($con, $sql);
return $result;
}
function going($con, $result, $user_id, $event_id) {
//If already in database as 'going', then do not duplicate. Do nothing.
if (!empty($result)) {
header("Location: http://localhost/townbuddies/public_html/event.php?event=" . $event_id);
exit;
}
//Not in database as 'going', so add it.
else {
$sql = "INSERT INTO user_events (user_id,event_id) "
. "VALUES ("
. "'$user_id'"
. ","
. "'$event_id'"
. ")";
if (!mysqli_query($con, $sql)) {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
//Update amount of people going.
$sql =
mysqli_close($con);
}
exit;
}
function notGoing($con, $result, $user_id, $event_id) {
//If already in database as 'going', then delete
if (!empty($result)) {
$sql = "DELETE FROM user_events "
. "WHERE user_id = "
. "'$user_id'"
. "AND event_id = "
. "'$event_id'";
if (!mysqli_query($con, $sql)) {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
mysqli_close($con);
}
exit;
}
?>
编辑#2:表格
<!DOCTYPE html>
<html lang = "en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="generator" content="Bootply" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Event</title>
<link href = "css/main.css" rel = "stylesheet" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"> </script>
<script src="js/event.js"></script>
</head>
<body>
<button type = "submit" id = "go-btn" class = "going-decision" value="going.' . $event_id . '">Going</button>
<button type = "submit" id = "no-go-btn" class = "going-decision" value="not_going.' . $event_id . '">Not Going</button>
</body>
编辑#3:解决方案
问题确实来自我的PHP脚本。在函数going()和notGoing()中,语句
if (!empty($result))
将无法按预期工作...如果我交换if和else语句,一切正常......
为什么mySQL的空结果不会为该语句为空?
答案 0 :(得分:1)
我希望我错了,但你的SQL查询有错误。
$sql = "INSERT INTO user_events (user_id,event_id) "
. "VALUES ("
. "'$user_id'"
. ","
. "'$event_id'"; // should be ."'$event_id')"
没有条件的结束括号,这会破坏查询。