收到警告 "unknown column favicon.ico"

getting warning "unknown column favicon.ico"

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

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

我的table

+-------------+--------------+------+-----+---------+----------------+
| 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 });
});

先谢谢了。

看起来 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

这很危险。不要将其发布到生产环境。

代码是wide-open到SQL次注射。

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