收到警告“未知列 favicon.ico”

问题描述

所以我想使用 express 和 MysqL2 创建一个简单的 crud 页面。当我请求 /:id 并运行查询以使用 WHERE 进行搜索时,即使我没有该列,我也会收到此警告。

"UnkNown column 'favicon.ico' in 'where clause'
at PromiseConnection.execute"

我的桌子

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int          | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255) | YES  |     | NULL    |                |
| description | varchar(255) | YES  |     | NULL    |                |
| price       | decimal(6,2) | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

我的 app.js

app.listen(3002,() => console.log(" Listening on Port 3002"));

app.use(express.urlencoded({ extended: true }));
app.set("view engine","ejs");
app.engine("ejs",ejsMate);
app.use(cookieParser("keyboard cat"));
app.use(session({ cookie: { maxAge: 60000 } }));
app.use(flash());

app.get("/",async (req,res) => {
  const connection = await MysqL.createConnection({
    host: "localhost",user: "root",database: "shop",password: "password",});
  const [products] = await connection.execute("SELECT id,name FROM products");
  connection.end();
  console.log(req.session);
  res.render("",{ products,message: req.flash("success") });
});

app.get("/add",(req,res) => {
  res.render("add");
});

app.get("/:id",res) => {
  const { id } = req.params;
  const connection = await MysqL.createConnection({
    host: "localhost",});
  const [product] = await connection.execute(
    `SELECT * FROM products WHERE id = ${id}`
  );
  connection.end();
  res.render("product",{ product });
});

之前谢谢。

解决方法

看起来您在 favicon.ico 所在的目录中没有 app.js 文件。浏览器将寻找“收藏夹图标”以显示在浏览器选项卡(和其他位置)中。

让我们看看这一行:

app.get("/:id",async (req,res) => {

:id 正在接受来自路径的任何东西。因此,如果 URL 是 http://localhost/app.js,那么浏览器将请求 http://localhost/favicon.ico:id 将采用 favicon.ico 作为值。

然后我们回到这部分代码:

  const [product] = await connection.execute(
    `SELECT * FROM products WHERE id = ${id}`
  );

你去吧。这就是您得到 Unknown column 'favicon.ico' in 'where clause' 的原因。 SQL 查询字面意思是:

SELECT * FROM products WHERE id = favicon.ico

这很危险。请勿将其发布到生产环境中。

该代码对 SQL 注入完全开放。

请务必向 :id 添加一些验证并在查询数据库时使用准备好的语句。这将减少——但不会消除——脚本小子毁了你一天的风险。