duandang6111 2019-04-13 19:30
浏览 261
已采纳

如何将表单数据提交到MySQL数据库

I am relatively new to Laravel and coding in general. I am trying to use Eloquent and to pass data from my form into a MySQL database. I can "submit" without any errors but don't see the data in the table. I feel like my code is getting messier as I am trying to figure this out and I would love a review of my code. I've tried several different ways of doing this by reading through tutorials, books and reviewing other people's code.

Route

Route::post('submit', function (Request $request) {
    $data = $request->validate([
        'title' => 'required|max:255',
        'category' => 'required|max:255',
        'content' => 'required|max:900',
    ]);
    return redirect('submit');
});

Controller

<?php

public function store(Request $request)
{
    Post::create([
        'title' => $request->title,
        'category' => $request->category,
        'content' => $request->content,
    ]);

    return redirect('views.submit');
}

View/Blade

<div>
    <form action='' method='post'>
        @csrf
        <p><label>Title</label><br/>
            <input type='text' name='title' value=''><?php if (isset($error)) {
                echo $_POST['title'];
            }?></p>
        <p><label>Category</label><br/>
            <textarea name='category' cols='60' rows='10'><?php if (isset($error)) {
                    echo $_POST['category'];
                }?></textarea></p>
        <p><label>Content</label><br/>
            <textarea name='content' cols='60' rows='10'><?php if (isset($error)) {
                    echo $_POST['content'];
                }?></textarea></p>
        <p><input type='submit' name='submit' value='Submit'></p>
    </form>
</div>

I'm expecting this to pass data to my table named "blogs."

  • 写回答

1条回答 默认 最新

  • duanjianl183188 2019-05-17 17:38
    关注

    So I finally solved this after making several changes.

    The first thing I did was moved my controller to /app/books.php

    I simplified the code to the following:

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class books extends Model
    {
        protected $fillable = [
            'title',
            'author',
            'category',
        ];
    }
    

    I then modified the route to NOT include the redirect:

    Route::post('/submitbook', function (Request $request) {
        $data = $request->validate([
            'title' => 'required|max:255',
            'author' => 'required|max:255',
            'category' => 'required|max:255',
        ]);
    

    Perhaps the biggest resolution happened by a fluke. I was working on a separate issue and had to clear my known_hosts file and everything worked after.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!