【问题标题】:getting warning "unknown column favicon.ico"收到警告“未知列 favicon.ico”
【发布时间】:2021-05-07 09:01:26
【问题描述】:

所以我想使用 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", async (req, res) => {
  const { id } = req.params;
  const connection = await mysql.createConnection({
    host: "localhost",
    user: "root",
    database: "shop",
    password: "password",
  });
  const [product] = await connection.execute(
    `SELECT * FROM products WHERE id = ${id}`
  );
  connection.end();
  res.render("product", { product });
});

之前谢谢。

【问题讨论】:

    标签: mysql node.js express mysql2


    【解决方案1】:

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

    让我们看看这一行:

    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 中添加一些验证,并在查询数据库时使用准备好的语句。这将减少(但不会消除)脚本小子破坏您一天的风险。

    【讨论】:

    • 哦,我明白了,我认为“get”只会抓取 url 栏。通过验证“:id”,您是指mongo sanitize之类的东西吗?
    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 2019-08-23
    • 2016-12-26
    • 2017-05-31
    • 1970-01-01
    • 2014-02-27
    • 2018-04-10
    • 1970-01-01
    相关资源
    最近更新 更多