我正在尝试编写一个查询,将用户所在的当前页面发送到我的数据库表click_count。我不确定我做错了什么,因为我现在的错误报告中没有任何错误。没有执行任何查询,也没有显示任何回显错误。
我不确定问题是否与我正在构建会话的方式有关。 SELECT
。我要做的是执行INSERT
查询以查看是否有记录,如果没有,则UPDATE
一个,然后从那一点page_count
每次click_count
CREATE TABLE `click_count` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`page_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`page_count` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
用户进入页面。
有没有人看到任何指出可能有问题的东西?
表click_count:
session_start();
//create current page constant
$curPage = $_SERVER['PHP_SELF'];
//echo $_SERVER['REQUEST_URI'];
//set number of clicks variable to 0
$clicks = 0;
//set current page as session variable
$_SESSION['page'] = $curPage;
//do not recount if page currently loaded
if($_SESSION['page'] != $curPage) {
$click_sql = "
SELECT *
FROM click_count
WHERE user_id = ?
AND page_url = ?
";
if (!$click_stmt = $con->prepare($click_sql)) {
$click_stmt->execute(array($user_id, $curPage));
$click_stmt_rows = $click_stmt->fetchAll(PDO::FETCH_ASSOC);
$page_count = $click_stmt->rowCount();
if (!$click_stmt->errno) {
echo "Error selecting page count.";
}
} else if ($page_count == 0) {
//try to create new record and set count for new page to 1
//output error message if problem encountered
$click_insert_stmt = "
INSERT INTO click_count
(page_url, page_count)
VALUES(?, ?)";
if(!$click_stmt = $con->prepare($click_insert_stmt)) {
$click_insert_stmt->execute(array($curPage, 1));
echo "Could not create new click counter.";
}
else {
$clicks = 1;
}
} else {
//get number of clicks for page and add 1 fetch(PDO::FETCH_BOTH)
while($click_row = $click_insert_stmt->fetch(PDO::FETCH_BOTH)) {
$clicks = $row['page_count'] + 1;
//update click count in database;
//report error if not updated
$click_update_stmt = "
UPDATE click_count
SET page_count=page_count+1
WHERE page_url = ?
";
if(!$click_stmt = $con->prepare($click_update_stmt)) {
$click_update_stmt->execute(array($curPage));
echo "Could not save new click count for this page.";
}
}
}
}
代码:
$curPage = $_SERVER['PHP_SELF'];
$clicks = 0;
$click_sql = "
SELECT *
FROM click_count
WHERE user_id = ?
AND page_url = ?
";
$click_stmt = $con->prepare($click_sql);
$click_stmt->execute(array($user_id, $curPage));
$click_stmt_rows = $click_stmt->fetchAll(PDO::FETCH_ASSOC);
$page_count = $click_stmt->rowCount();
if ($page_count == 0) {
//try to create new record and set count for new page to 1
//output error message if problem encountered
$click_insert_stmt = "
INSERT INTO click_count
(user_id, page_url, page_count)
VALUES(?, ?, ?)";
if(!$click_stmt = $con->prepare($click_insert_stmt)) {
$click_insert_stmt->execute(array($user_id, $curPage, 1));
echo "Could not create new click counter.";
}
else {
$clicks = 1;
}
} else {
//get number of clicks for page and add 1 fetch(PDO::FETCH_BOTH)
while($click_row = $click_insert_stmt->fetch(PDO::FETCH_BOTH)) {
$clicks = $row['page_count'] + 1;
//update click count in database;
//report error if not updated
$click_update_stmt = "
UPDATE click_count
SET page_count=page_count+1
WHERE user_id = ?
AND page_url = ?
";
if(!$click_stmt = $con->prepare($click_update_stmt)) {
$click_update_stmt->execute(array($user_id, $curPage));
echo "Could not save new click count for this page.";
}
}
}
更新代码:
$curPage = $_SERVER['PHP_SELF'];
$clicks = 0;
$click_sql = "
SELECT *
FROM click_count
WHERE user_id = ?
AND page_url = ?
";
$click_stmt = $con->prepare($click_sql);
$click_stmt->execute(array($user_id, $curPage));
$click_stmt_rows = $click_stmt->fetchAll(PDO::FETCH_ASSOC);
$page_count = $click_stmt->rowCount();
if ($page_count == 0) {
$click_insert_sql = "
INSERT INTO click_count
(user_id, page_url, page_count)
VALUES(?, ?, ?)
ON DUPLICATE KEY UPDATE page_count=page_count+1;
";
$click_insert_stmt = $con->prepare($click_insert_sql);
$click_insert_stmt->execute(array($user_id, $curPage, 1));
}
最新代码:
x
答案 0 :(得分:0)
这是一个失败点:
//set current page as session variable
$_SESSION['page'] = $curPage;
//do not recount if page currently loaded
if($_SESSION['page'] != $curPage) {
您将$ _SESSION ['page']的值设置为$ curPage,然后检查该值是否与$ curPage不同。你只需将它设置为该值,因此它不能是不同的
你应该检查$ _SESSION ['page']是否先设置,如果没有设置,设置是否设置,如果已经设置,则检查它的值
if (isset($_SESSION['page']) && $_SESSION['page'] != $curPage) {
// increment counter
} else {
$_SESSION['page'] = $curPage;
// add new record
}
或者,如果您只使用INSERT ON DUPLICATE KEY UPDATE,则可以跳过所有逻辑并执行:
INSERT INTO click_count (user_id, page_url, page_count) VALUES (?, ?,?)
ON DUPLICATE KEY UPDATE page_count=page_count+1;
您无需检查该页面是否有记录,您无需查看当前值。查询将处理它。 您必须在user_id和page_url
上定义组合的唯一键答案 1 :(得分:0)
<强> 更改 强>
1)删除if($_SESSION['page'] != $curPage) {
2)我不知道你为什么在多个地方写这个if(!$click_stmt=$con->prepare($click_insert_stmt)){
。不幸的是,如果这些准备陈述失败了,那么,它将如何准备&amp;执行声明。
3) $user_id
对我来说是一个谜。因为,我没有看到你初始化它的任何地方。
我已经更新了你的代码。看看。
更新代码
$click_sql = "SELECT * FROM click_count WHERE user_id = ? AND page_url = ?";
$click_stmt = $con->prepare($click_sql);
$click_stmt->execute(array($user_id, $curPage));
$page_count = $click_stmt->rowCount();
if ($page_count == 0) {
$page_count ++;
$click_insert_stmt = "INSERT INTO click_count (page_url, page_count, user_id) VALUES(?, ?, ?)";
$click_stmt = $con->prepare($click_insert_stmt);
if(!$click_insert_stmt->execute(array($curPage, $page_count, $user_id))){
echo "Could not create new click counter.";
}
} else {
$click_update_stmt = "UPDATE click_count SET page_count= page_count+1 WHERE page_url = ? AND user_id = ?";
$click_stmt = $con->prepare($click_update_stmt);
if(!$click_update_stmt->execute(array($curPage,$user_id))){
echo "Could not save new click count for this page.";
}
}