我正在尝试使用AdonisJs将值插入XAMPP MySQL服务器,但是出现以下错误:
插入posts
(body
,created_at
,title
,updated_at
)值中(DEFAULT,'2018-06-28 15:06:02 ','帖子4','这是帖子4','2018-06-28 15:06:02')-ER_WRONG_VALUE_COUNT_ON_ROW:列计数与第1行的值计数不匹配
此错误告诉我的是,我为包含4个值的表提供5个值。实际上,表“ posts”确实包含5个值,它们是:
id,标题,正文,created_at,updated_at
我可以使用phpMyAdmin手动将值输入到id,标题,正文中,该表将被更新,并且我的应用程序将反映更改。
我可以跟踪到的唯一问题似乎是与PostController.js文件中的增量()方法有关。它的工作做得不好吗?
我如何继续弄清这里发生了什么?
迁移文件posts_schema.js:
'use strict'
const Schema = use('Schema')
class PostsSchema extends Schema {
up () {
this.create('posts', (table) => {
table.increments()
table.string('title')
table.string('body')
table.timestamps()
})
}
down () {
this.drop('posts')
}
}
module.exports = PostsSchema
控制器PostsController.js:
'use strict'
// Bring in model
const Post = use('App/Models/Post')
class PostController {
async index({ view }){
// const posts = [
// {title:'Post One', body:'This is post one'},
// {title:'Post Two', body:'This is post one'},
// {title:'Post Three', body:'This is post one'}
// ]
const posts = await Post.all();
return view.render('posts.index', {
title: 'Latest Posts',
posts: posts.toJSON()
})
}
async details({ params, view}) {
const post = await Post.find(params.id)
return view.render('posts.details', {
post: post
})
}
async add({ view }) {
return view.render('posts.add')
}
async store({ request, response, session }) {
const post = new Post();
post.title = request.input('title')
post.body = request.input('body')
await post.save()
session.flash({ notification: 'Posted Success'})
return response.redirect('/posts')
}
}
module.exports = PostController
视图add.edge:
@layout('main')
@section('content')
<a href="/posts">Go Back</a>
<hr>
<h1>Add Post</h1>
<form action="/posts" method="POST">
{{ csrfField() }}
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Title">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="title" class="form-control" placeholder="Body"></textarea>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
@endsection
路由文件route.js:
'use strict'
const Route = use('Route')
Route.on('/').render('home')
Route.get('/posts', 'PostController.index')
Route.get('/posts/add', 'PostController.add')
Route.get('/posts/:id', 'PostController.details')
Route.post('/posts', 'PostController.store')
我几乎没有配置XAMPP。我对config.inc.php的更改很简单:
$cfg['Servers'][$i]['controluser'] = 'root';
$cfg['Servers'][$i]['controlpass'] = '123456';
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '12345';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = true;
可以通过以下代码来学习此代码和教程: AdonisJs入门 https://www.youtube.com/watch?v=SaXhwbuMTu4
答案 0 :(得分:0)
如果这是您当前插入的内容:
insert into posts (body, created_at, title, updated_at) values (DEFAULT, '2018-06-28 15:06:02', 'post 4', 'this is post 4', '2018-06-28 15:06:02')
应为:
insert into posts (body, created_at, title, updated_at) values ('this is post 4', '2018-06-28 15:06:02', 'post 4', '2018-06-28 15:06:02')
答案 1 :(得分:0)
<textarea name="title" class="form-control" placeholder="Body"></textarea>
看起来name
属性应该是body
而不是title
。