PHP错误参数

发布于 2024-11-29 06:45:29 字数 699 浏览 0 评论 0原文

我正在尝试制作一个注册页面,但 PHP 告诉我参数错误,除非我需要为自动增量主 ID 键添加参数,否则这没有意义。

这是我的 SQL 查询调用:

mysql_query("INSERT INTO Users (username, password, fname, lname, email) VALUES ('%s', '%s', '%s', '%s, '%s')",
        mysql_real_escape_string($username),
        mysql_real_escape_string($password),
        mysql_real_escape_string($first),
        mysql_real_escape_string($last),
        mysql_real_escape_string($email)) or die(mysql_error());

它在该代码块的最后一行给出了错误的参数计数。有什么想法吗?我直接从数据库复制并粘贴行名称。

我的表如下:

id - int(11) - auto-incrementing
username - varchar(20)
password - varchar(20)
fname - varchar(35) 
lname - varchar(35)
email - varchar(254)

I'm trying to make a registration page but PHP is telling me that I have the wrong parameters, which doesn't make sense unless I need to add a parameter for the auto-incremental primary ID key.

Here's my SQL query call:

mysql_query("INSERT INTO Users (username, password, fname, lname, email) VALUES ('%s', '%s', '%s', '%s, '%s')",
        mysql_real_escape_string($username),
        mysql_real_escape_string($password),
        mysql_real_escape_string($first),
        mysql_real_escape_string($last),
        mysql_real_escape_string($email)) or die(mysql_error());

It gives me the wrong paramater count on the last line in this code block. Any ideas? I copied and pasted the row-names straight from my database.

my table is as follows:

id - int(11) - auto-incrementing
username - varchar(20)
password - varchar(20)
fname - varchar(35) 
lname - varchar(35)
email - varchar(254)

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

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

发布评论

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

评论(3

陈年往事 2024-12-06 06:45:29

您已将 SQL 查询格式化为 sprintf() 调用,但不调用 sprintf()

mysql_query(sprintf("INSERT INTO Users (username, password, fname, lname, email) VALUES ('%s', '%s', '%s', '%s', '%s')",
            mysql_real_escape_string($username),
            mysql_real_escape_string($password),
            mysql_real_escape_string($first),
            mysql_real_escape_string($last),
            mysql_real_escape_string($email))) or die(mysql_error());
  // also note some parentheses out of place ^^^^^^^^^^^^^^^^^^^^^^^^

You have formatted the SQL query as a sprintf() call, but don't call sprintf()

mysql_query(sprintf("INSERT INTO Users (username, password, fname, lname, email) VALUES ('%s', '%s', '%s', '%s', '%s')",
            mysql_real_escape_string($username),
            mysql_real_escape_string($password),
            mysql_real_escape_string($first),
            mysql_real_escape_string($last),
            mysql_real_escape_string($email))) or die(mysql_error());
  // also note some parentheses out of place ^^^^^^^^^^^^^^^^^^^^^^^^
旧伤慢歌 2024-12-06 06:45:29

是 PHP 告诉您有关参数的信息,而不是 MySQL。

您尝试过像 sprintf 一样使用 mysql_query,但事实并非如此。 mysql_query 接受可选的数据库资源标识符和查询字符串。两个参数。仅此而已。

如果您确实想要使用sprintf,那么就使用它:

mysql_query(
   sprintf(
      "INSERT INTO Users (username, password, fname, lname, email) VALUES ('%s', '%s', '%s', '%s, '%s')",
      mysql_real_escape_string($username),
      mysql_real_escape_string($password),
      mysql_real_escape_string($first),
      mysql_real_escape_string($last),
      mysql_real_escape_string($email)
   )
) or die(mysql_error());

但请记住mysql_query的第一个参数只是一个字符串。没有魔法。

It's PHP that's telling you off about parameters, not MySQL.

You've tried to use mysql_query like sprintf, which it is not. mysql_query accepts an optional database resource identifier, and the query string. Two parameters. That is all.

If you do want to use sprintf, then go for it:

mysql_query(
   sprintf(
      "INSERT INTO Users (username, password, fname, lname, email) VALUES ('%s', '%s', '%s', '%s, '%s')",
      mysql_real_escape_string($username),
      mysql_real_escape_string($password),
      mysql_real_escape_string($first),
      mysql_real_escape_string($last),
      mysql_real_escape_string($email)
   )
) or die(mysql_error());

But remember that the first argument to mysql_query is just a string. No magic.

不可一世的女人 2024-12-06 06:45:29
mysql_query(
    sprintf("INSERT INTO Users (username, password, fname, lname, email) VALUES ('%s', '%s', '%s', '%s, '%s')",
    mysql_real_escape_string($username),
    mysql_real_escape_string($password),
    mysql_real_escape_string($first),
    mysql_real_escape_string($last),
    mysql_real_escape_string($email)))

or die(mysql_error()); // sprintf to build a final string of your query by given format and "or die statement" is outside the mysql_query function call.
mysql_query(
    sprintf("INSERT INTO Users (username, password, fname, lname, email) VALUES ('%s', '%s', '%s', '%s, '%s')",
    mysql_real_escape_string($username),
    mysql_real_escape_string($password),
    mysql_real_escape_string($first),
    mysql_real_escape_string($last),
    mysql_real_escape_string($email)))

or die(mysql_error()); // sprintf to build a final string of your query by given format and "or die statement" is outside the mysql_query function call.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文