PHP MYSQL:每次在浏览器中加载页面时增加视图列的正确代码

发布于 2024-12-29 22:00:26 字数 529 浏览 1 评论 0原文

我正在尝试在我的图书网站上创建一个“视图”系统。

我有以下表格,其中包含以下列:

Books
-bookid
-bookname
-authorid
-views

我的网页设置为显示基于 $_GET['bookid'] 变量的书籍,我想添加 1(增加 views 针对该特定书籍的一列)

我尝试使用以下代码,但它没有更新我的表:

<?php $sql = "UPDATE `books` \n" . "SET views = views+1 WHERE" . $_GET['bookid'] .= "bookid"; ?>

另外:我使用 dreamweaver 来运行记录集查询)所以也许有些不同。

请帮忙!

旁注:您能为像我这样的绝对初学者推荐一本学习 php 和 mysql 的好书/视频或书面教程吗!

I am trying to create a "views" system on my books website.

I have the following tables with the following columns:

Books
-bookid
-bookname
-authorid
-views

my webpage is set up to display a book based on the $_GET['bookid'] variable and I want to add 1 (increment the views column by one for that particular book)

I tried using the following code but it didn't update my table:

<?php $sql = "UPDATE `books` \n" . "SET views = views+1 WHERE" . $_GET['bookid'] .= "bookid"; ?>

ALSO: I used dreamweaver to run the recordset query) so maybe something is different.

Please Help!

Sidenote: Can you please recommend a good book/video or written tutorial to learn php and mysql for absolute beginners like my self!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌辣 2025-01-05 22:00:26

这很重要:不要在 SQL 查询中直接包含 $_GET 参数。

这会使您的网站容易受到 SQL 注入攻击。通过使用以下方式清理您的输入:

$book_id = mysql_real_escape_string($_GET['book_id']); // If it is a string
$book_id = intval($_GET['book_id']); // It it is an integer

// Assuming it is an integer
$sql = "UPDATE books SET views = views+1 WHERE bookid = $book_id"; 

您显然需要执行该查询,您正在这样做吗?

$user="username";
$password="password";
$database="database";
mysql_connect(localhost,$user,$password);
mysql_select_db($database) or die( "Unable to select database");

mysql_query($sql);
mysql_close();

编辑:
另外,只是一个提示,因为您使用的是 $_GET,所以您应该执行类似 yourscript.php?book_id=12345 的内容,这就是您正在做的事情吗?

This is important: don't include $_GET paramaters directly in your SQL query.

This makes your website vulnerable to an SQL Injection attack. Sanatise your inputs by using:

$book_id = mysql_real_escape_string($_GET['book_id']); // If it is a string
$book_id = intval($_GET['book_id']); // It it is an integer

// Assuming it is an integer
$sql = "UPDATE books SET views = views+1 WHERE bookid = $book_id"; 

You obviously need to execute that query, are you doing that?

$user="username";
$password="password";
$database="database";
mysql_connect(localhost,$user,$password);
mysql_select_db($database) or die( "Unable to select database");

mysql_query($sql);
mysql_close();

EDIT:
Also, just a tip, since you're using $_GET you should be executing something like yourscript.php?book_id=12345, is that what you're doing?

倾听心声的旋律 2025-01-05 22:00:26

您已经找到了一些学习 PHP 的最佳方法:编写代码,当您不了解更多信息时来到这里:)(除此之外,我手上没有真正好的教程;)
至于你的问题:

  • 检查 $_GET['bookid'] 的值,
  • 检查 $sql 的值,
  • 如果一切正常,则直接运行查询

哦,等等。
你实际上并没有在代码中执行sql,只是用查询生成一个字符串。您需要打开连接等,还是您正在这样做并将其留在这里?

you've already found some of the best ways to learn PHP: writing code and coming here when you don't know further :) (don't have a real good tutorial on my hands beyond that ;)
As for your question:

  • check the value of $_GET['bookid']
  • check the value of $sql
  • if all looks as intended, run the query directly

oh wait.
you're not actually executing the sql in your code, just generating a string with the query. you need to open a connection etc, or are you doing that and leaving it out here?

云柯 2025-01-05 22:00:26

你的查询看起来有点不对劲。试试这个:
$sql = '更新书籍 SET 视图 = 视图+1 WHERE bookid = ' . intval($_GET['book_id']);

Your query looks slightly off. Try this:
$sql = 'UPDATE books SET views = views+1 WHERE bookid = ' . intval($_GET['book_id']);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文