Greetings. If there are experts on this micro-framework, I will be grateful for the help. You need to create a script to add an account to the database.

<form> <input type="text" name="email" placeholder="email" required ></br> <input type="submit" name="submit" value="submit"> </form> Flight::route('POST /hello', function() { $db=Flight::db()->connect; Flight::register('usersmanager','UsersManager'); $createuser=Flight::usersmanager()->createUser(); $db=Flight::db()->disconnect; Flight::redirect('/'); }); class UsersManager { function createUser($info) { $data=$_POST; if(isset($data['submit'])){ $email = trim($data['email']); $query="INSERT INTO users('email') VALUES('$email')"; mysql_query($query); } 

Doesn't add any data, can that be overlooked ??

    1 answer 1

    1. Make sure you have the correct database connection settings. For example, I have:

     $driver_options = [PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'UTF8'"]; Flight::register('db', 'PDO', ['mysql:host=localhost;dbname=DBNAME','DBUSER','DBPASS', $driver_options ]); 

    The code should be located before Flight::route

    2. Remove connect / disconnect strings.

    3. It is not necessary to use the global $_POST array → all request data can be obtained via Flight::request()->data for example

     $email = trim( Flight::request()->data->email ); 

    4. Do not check isset($data['submit']) → you already know that you received a POST request with data. → It is better to check the correctness of the data itself.

    5. It is better to perform a query to the database by means of the framework having previously prepared the query:

      $query = Flight::db()->prepare("INSERT INTO users ( 'email' ) VALUES( :useremail );"); $query->execute(['useremail'=>$email]);