-1

I have read some tutorials and videos. I just downloaded and installed laravel. Trying to create a simple registration form.

welcome.blade.php

<form action="{{URL::to('/store')}}" method="post">
       {{csrf_field()}}
       <input type="text" name="clientFirstname" >
       <input type="text" name="clientSurname" >
       <input type="text" name="clientEmail">
       <input type="hidden" name="token" value="">
       <button type="submit" name="button">add</button>
</form>

web.php

Route::view('/', 'welcome');
Route::post('/store', "Controller@store");

Controller.php

class Controller extends BaseController {

    public function store(Request $request) {
        //print_r ($request->input());
        //print_r ($request->input('clientFirstname'));
        $result=DB::insert("INSERT INTO test('firstname', 'surname', 'email') VALUES(?, ?, ?)", $request->input('clientFirstname'), $request->input('clientSurname'), $request->input('clientEmail'));
        echo $result;
    }
}

.env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=root
DB_PASSWORD=

database.php

        'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'testdb'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
        ],

i have created a db named testdb through phpmyadmin. Let me inform you that I can print the form fields but when i insert code as above that inser to db something i got the following error.

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) Type error: Argument 1 passed to Illuminate\Database\Connection::prepareBindings() must be of the type array, string given, called in C:\Users\regor\ExampleProject\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 665

I would appreciate any help. :)

Nesku
  • 501
  • 1
  • 6
  • 12
Anonymous
  • 11
  • 8
  • 1
    There's another issue in your query as well. You should read this post [When to use single quotes, double quotes, and back ticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql) – M. Eriksson Nov 21 '18 at 21:09

1 Answers1

2

The DB::insert() method accepts an array as the second argument.

You should use it like this:

DB::insert("INSERT INTO test(`firstname`, `surname`, `email`) VALUES(?, ?, ?)", [
    $request->input('clientFirstname'),
    $request->input('clientSurname'),
    $request->input('clientEmail')
]);

Also, use back ticks for column identifiers instead of single quotes.

Martin Heralecký
  • 5,649
  • 3
  • 27
  • 65