Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 用Laravel在数据库中插入数据_Html_Mysql_Laravel - Fatal编程技术网

Html 用Laravel在数据库中插入数据

Html 用Laravel在数据库中插入数据,html,mysql,laravel,Html,Mysql,Laravel,我正在制作一个带有表单的web应用程序,该表单可以将数据插入数据库。我正在用Laravel制作这个web应用程序。现在我制作了表单和插入代码,但它不工作,它给出了一个未找到的错误。希望有人能给我一些关于如何解决这个问题的建议 我的看法是: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=e

我正在制作一个带有表单的web应用程序,该表单可以将数据插入数据库。我正在用Laravel制作这个web应用程序。现在我制作了表单和插入代码,但它不工作,它给出了一个未找到的错误。希望有人能给我一些关于如何解决这个问题的建议

我的看法是:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Spelers Toevoegen</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="main.js"></script>
</head>
<body>
 <form id="form" action="/add" method="post">

 @csrf

   <div class="form-group">
     <h1>Speler toevoegen</h1>
     <label>Naam:<input name="naam" type="text" value="" class="form-control"></label><br>
     <label>Aantal:<input name="aantal" type="text" value="" class="form-control"></label><br>
            <label>Positie:<input name="positie" type="text" value="" class="form-control"></label><br>
            <label>club:<input name="club" type="text" value="{{$naam}}" class="form-control"></label><br>
            <button type="submit" class="btn btn-primary"><a id="btn_link">Voeg spelers toe</a></button>
        </div>
 </form>
这是我的路线:

Route::post('/add', 'VoetbalController@Add');

首先,你应该用小写而不是大写来命名你的方法

因此,您的路线应该如下所示:

Route::post('/add', 'VoetbalController@add');
然后将其保存到数据库中:

public function add(Request $request) {
    $data = SpelerSelectie::create($request->all());

    if($data) {
        return redirect()->route('add.index')->with('message', 'Success.');
    }

    return redirect()->route('add.index')->with('message', 'error.');
}
此外,您应该在文件顶部的控制器中导入模型:

use App\SpelerSelectie;
此外,还需要在模型的可填充数组中添加要插入/更新到模型中的字段:

protected $fillable = ['field1']; // you need to add your field name (tablenames)

我假设您创建了正确的迁移,如果没有,您必须首先创建正确的迁移。

未找到错误,但什么错误/找不到?它正在尝试转到页面/添加。这正是它给我的错误信息。在这台服务器上找不到请求的URL/add。我有点不明白你所说的可填充数组是什么意思,我应该把代码放在哪里?另外,我还没有进行适当的迁移,这是否有必要?我认为问题在于我在表单中作为操作放置的/add,这可能是问题吗?@stefandeboer如果您没有创建迁移,您发布的本地数据库中可能没有适当的表。我希望您没有手动创建,这在本例中是一种不好的做法。最好是查看迁移文档或观看有关迁移的视频。迁移只是帮助您创建适当的数据库表,模型中的$fillable数组告诉模型实际应存储/更新哪些数据,因此,如果需要存储名称,请将名称添加到$fillable数组中,依此类推。但是,如果不迁移,它不也可以工作吗?我查看了WAMP服务器上的表,似乎一切正常。如果表存在,则大多数人会创建迁移来设置数据库,但如果手动设置数据库,则仍然可以工作。如果你说你的数据库看起来是正确的,那么你的错误就在别处。我认为问题在于我在表单中填写我的操作属性的方式,因为错误表明/add在服务器上找不到。为什么action属性不起作用?
protected $fillable = ['field1']; // you need to add your field name (tablenames)