MySQL 语法错误 - 查询数组

发布于 2024-12-08 20:40:17 字数 1489 浏览 0 评论 0 原文

我有一个根据一些测试数据创建的预构造数组,因为我尚未设置发布表单。该数组如下所示:

$ud = array('name' => 'name', 'username' => 'username', 'password' => 'password', 'location' => 'london', 'platform' => 'mobile', 'developer_or_designer' => 'developer', 'tags' => 'hello', 'paypal_email' => '[email protected]', 'developer_or_client' => 'developer', 'email' => '[email protected]');

foreach ($ud as $key => $value) {
    $value = mysql_real_escape_string($value);
}

然后,我尝试通过该数组将数据通过 MySQL 查询插入到我的数据库中:

$query = mysql_query("INSERT INTO `Developers` (`Name`,`Email`,`Username`,`Password`,`Location`,`Platform`,`Developer_or_Designer`,`Tags`, `Paypal_Email`) VALUES (" . $ud['name'] . ", " . $ud['email'] . ", " . $ud['username'] . ", " .$ud['password'] . ", " . $ud['location'] . ", " . $ud['platform'] . ", " . $ud['developer_or_designer'] . ", " . $ud['tags'] . ", " . $ud['paypal_email'] . ")") or die(mysql_error());

但是,它因以下错误而终止:

您的 SQL 语法中有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 '@email.com、用户名、密码、伦敦、移动、开发人员、你好、[电子邮件受保护])'位于第 1 行

请您告诉我哪里出了问题?

I have a pre-constructed array created from some test data as I have not yet set up a post form. The array looks like this:

$ud = array('name' => 'name', 'username' => 'username', 'password' => 'password', 'location' => 'london', 'platform' => 'mobile', 'developer_or_designer' => 'developer', 'tags' => 'hello', 'paypal_email' => '[email protected]', 'developer_or_client' => 'developer', 'email' => '[email protected]');

foreach ($ud as $key => $value) {
    $value = mysql_real_escape_string($value);
}

From this array, I then try to insert the data via a MySQL query into my database:

$query = mysql_query("INSERT INTO `Developers` (`Name`,`Email`,`Username`,`Password`,`Location`,`Platform`,`Developer_or_Designer`,`Tags`, `Paypal_Email`) VALUES (" . $ud['name'] . ", " . $ud['email'] . ", " . $ud['username'] . ", " .$ud['password'] . ", " . $ud['location'] . ", " . $ud['platform'] . ", " . $ud['developer_or_designer'] . ", " . $ud['tags'] . ", " . $ud['paypal_email'] . ")") or die(mysql_error());

However, it dies with the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@email.com, username, password, london, mobile, developer, hello, [email protected])' at line 1

Please can you tell me where I am going wrong?

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

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

发布评论

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

评论(5

末が日狂欢 2024-12-15 20:40:17

括号中的每个值都需要引号

You need quotes around each value in parenthases

孤云独去闲 2024-12-15 20:40:17

有两件事:

  1. 正如 Jeff 指出的,你需要加上引号围绕琴弦。
  2. 在将引号括起来之前,您需要通过 mysql_real_escape_sring()

Two things:

  1. As Jeff notes, you need to put quotes around the strings.
  2. Before putting quotes around them, you need to pass each string through mysql_real_escape_sring().
走过海棠暮 2024-12-15 20:40:17
$query = mysql_query("INSERT INTO `Developers` (`Name`,`Email`,`Username`,`Password`,`Location`,`Platform`,`Developer_or_Designer`,`Tags`, `Paypal_Email`) VALUES ('" . $ud['name'] . "', '" . $ud['email'] . "', '" . $ud['username'] . "', '" .$ud['password'] . "', '" . $ud['location'] . "', '" . $ud['platform'] . "', '" . $ud['developer_or_designer'] . "', '" . $ud['tags'] . "', '" . $ud['paypal_email'] . "')") or die(mysql_error());

尝试一下:)

$query = mysql_query("INSERT INTO `Developers` (`Name`,`Email`,`Username`,`Password`,`Location`,`Platform`,`Developer_or_Designer`,`Tags`, `Paypal_Email`) VALUES ('" . $ud['name'] . "', '" . $ud['email'] . "', '" . $ud['username'] . "', '" .$ud['password'] . "', '" . $ud['location'] . "', '" . $ud['platform'] . "', '" . $ud['developer_or_designer'] . "', '" . $ud['tags'] . "', '" . $ud['paypal_email'] . "')") or die(mysql_error());

try it:)

寂寞花火° 2024-12-15 20:40:17

从列名的声音来看,这些是 varchar 列类型,因此您需要用引号将值括起来:

$query = mysql_query("INSERT INTO `Developers` (`Name`,`Email`,`Username`,`Password`,`Location`,`Platform`,`Developer_or_Designer`,`Tags`, `Paypal_Email`) VALUES ('" . $ud['name'] . "', '" . $ud['email'] . "', '" . $ud['username'] . "', '" .$ud['password'] . "', '" . $ud['location'] . "', '" . $ud['platform'] . "', '" . $ud['developer_or_designer'] . "', '" . $ud['tags'] . "', '" . $ud['paypal_email'] . "')") or die(mysql_error());

此外,如果值来自用户输入,您应该通过 mysql_real_escape_string 运行每个值code> 帮助防止 SQL 注入攻击

From the sounds of the column names those are varchar column types so you need to wrap your values with quotes:

$query = mysql_query("INSERT INTO `Developers` (`Name`,`Email`,`Username`,`Password`,`Location`,`Platform`,`Developer_or_Designer`,`Tags`, `Paypal_Email`) VALUES ('" . $ud['name'] . "', '" . $ud['email'] . "', '" . $ud['username'] . "', '" .$ud['password'] . "', '" . $ud['location'] . "', '" . $ud['platform'] . "', '" . $ud['developer_or_designer'] . "', '" . $ud['tags'] . "', '" . $ud['paypal_email'] . "')") or die(mysql_error());

Also if the values are coming from user input you should run each value through mysql_real_escape_string to help prevent against SQL injection attacks

谈下烟灰 2024-12-15 20:40:17

请参阅:

VALUES (" . $ud['name'] . ",

Nedd that:

VALUES ('" . $ud['name'] . "',

对于其他列也是如此(如果不是数字)

See this:

VALUES (" . $ud['name'] . ",

Nedd that:

VALUES ('" . $ud['name'] . "',

And for other columns too (if is not numberic)

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