内爆计数大于 2 的数组时 PHP INSERT 错误
当尝试将一行插入我的 mysql 数据库时,我遇到了这个奇怪的问题。
这就是想法。列出了一系列锻炼,您可以添加数量并检查要添加此锻炼的日期(周日、周一、周二、周三、周四、周五、周六),然后将其添加到您的日记中。
这是我的代码。
$addDiary = array();
$addTimestamp = array();
$addDiary[] = '('.$current_user->id.', '.$_POST['exid_'.$i].', '.$_POST['qty_'.$i].', '.$calories_mod.', '.implode('.', $addTimestamp).')';
$queryAddDiary = mysql_query("INSERT INTO LHNZ_FT_DIARY (USER_ID, EXERCISE_ID, TIME, CALORIES, TIMESTAMPS) VALUES ".implode(',', $addDiary));
我已经回显了 addDiary 数组和 addTimestamp 数组,它对我来说看起来很好。
$addTimestamp: 1311206400.1311292800.1311379200
$addDiary: (2, 1, 1, 678, 1311206400.1311292800.1311379200)
如果我的 $addTimestamp 数组的长度为 1 或 2,则该查询效果很好。 length 大于 2,它会消除此错误。
您的 SQL 语法有错误;检查手册 与您的 MySQL 服务器版本相对应,以便使用正确的语法 第 1 行“.1311379200)”附近
。如果我使用“.”,则会显示此错误或 ':' 作为我对 $addTimestamp 的内爆。如果我使用字母或“_”,则会抛出此错误。
“字段列表”中存在未知列“1311206400_1311292800_1311379200”
任何想法我如何使用长度大于2的$addTimestamp(意味着我可以每周做三天练习......这就是错误的地方)。
谢谢,
- 莱顿
I have this weird problem when trying to INSERT a row into my mysql database.
This is the idea. A range of exercise is listed and you can add a quantity and check the days you want to add this exercise to (Sun, Mon, Tue, Wed, Thu, Fri, Sat) and that adds it to your diary.
This is my code.
$addDiary = array();
$addTimestamp = array();
$addDiary[] = '('.$current_user->id.', '.$_POST['exid_'.$i].', '.$_POST['qty_'.$i].', '.$calories_mod.', '.implode('.', $addTimestamp).')';
$queryAddDiary = mysql_query("INSERT INTO LHNZ_FT_DIARY (USER_ID, EXERCISE_ID, TIME, CALORIES, TIMESTAMPS) VALUES ".implode(',', $addDiary));
I have echoed out the addDiary array and the addTimestamp array and it looks fine to me.
$addTimestamp: 1311206400.1311292800.1311379200
$addDiary: (2, 1, 1, 678, 1311206400.1311292800.1311379200)
That query works beautifully if my $addTimestamp array has a length of 1 or 2. However when the length is greater than 2, it chucks this 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 '.1311379200)' at line 1
Now. This error is shown if I use '.' or ':' as my implode for $addTimestamp. If I use a letter, or '_' it chucks this error.
Unknown column '1311206400_1311292800_1311379200' in 'field list'
Any ideas how I can use $addTimestamp with a length greater than 2 (meaning I may do that exercise three days a week... this is where it errors).
Thanks,
- leighton
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MySQL 查询中的字符串值必须用单引号引起来。
而不是:
做:
虽然我强烈建议您也通过 mysql_real_escape_string(),然后将其粘合到 sql 查询。
String values in MySQL queries must be enclosed in single quotes.
Instead of:
Do:
Although I strongly suggest that you also pass every value through mysql_real_escape_string() before gluing it to sql query.