0

Я новичек в PHP и хотел задать вопрос по поводу работы с БД. Я пытаюсь реализовать систему регистрации, но после заполнения формы регистрации строка в таблицу "users" в базе данных не добавляется. Как это можно исправить?

functions.php:

<?php
include 'C:\OpenServer\domains\localhost\mtalk_includes\config.php';

function getDatabaseConnection() { try { $conn = new PDO( 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS); return $conn; } catch ( PDOException $e ) { return $e->getMessage(); } }

function getUserWithEmailAddress( $email ) { $databaseConnection = getDatabaseConnection();

$statement = $databaseConnection-&gt;prepare( '
    SELECT
        *
    FROM
        users
    WHERE
        email = :email
' );

$statement-&gt;setFetchMode( PDO::FETCH_ASSOC );
$statement-&gt;execute( array(
    'email' =&gt; trim( $email )
) );

$user = $statement-&gt;fetch();
return $user;

}

function signUserUp ( $info ) { $databaseConnection = getDatabaseConnection();

$statement = $databaseConnection-&gt;prepare( '
    INSERT INTO 
        users (
            email,
            first_name,
            last_name,
            password,
            key_value
        )
    VALUES (
            :email,
            :first_name,
            :last_name,
            :password,
            :key_value
    )
' );

$statement-&gt;execute( array(
    'email' =&gt; trim( $info['email'] ),
    'first_name' =&gt; trim( $info['first_name'] ),
    'last_name' =&gt; trim( $info['last_name'] ),
    'password' =&gt; hashedPassword( $info['password'] ),
    'key_value' =&gt; newKey(),
) );

return $databaseConnection-&gt;lastInsertId();


}

function newKey( $length = 32 ) { $time = md5( uniqid() ) . microtime(); return substr( md5( $time ), 0, $length ); }

function hashedPassword( $password ) { $random = openssl_random_pseudo_bytes( 18 ); $salt = sprintf( '$2y$%02d$%s', 12, substr( strtr( base64_encode( $random ), '+', '.' ), 0, 22 ) );

$hash = crypt( $password, $salt );

return $hash;

}

config.php:

<?php
define( 'DB_HOST', 'localhost' );
define( 'DB_NAME', 'test' );
define( 'DB_USER', 'root'); 
define( 'DB_PASS', '');

process_signup.php:

<?php
include 'functions.php';

$userInfo = getUserWithEmailAddress( trim( $_POST['email'] ) );

$status = 'ok'; $message = 'valid';

$userId = signUserUp( $_POST );

echo json_encode( array( 'status' => $status, 'message' => $message ) );

signup.php:

<script type="text/javascript" src="js/loader.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
    &lt;script&gt;
    $( function() {

        $( '#signup_button' ).on( 'click', function() {
            processSignup();
        } );

        $( '.form-input' ).keyup( function( e ) {
            if ( e.keyCode == 13 ) {
                processSignup();
            }
        } );
    } );

    function processSignup() {
        $( '#error_message' ).html( '' );
        $( 'input' ).removeClass( 'invalid-input' );

        var allFieldsFilledIn = true;

        $( 'input' ).each( function() {
            if ( '' == $( this ).val() ) {
                $( this ).addClass( 'invalid-input ');
                allFieldsFilledIn = false;
            }
        } );

        if ( allFieldsFilledIn ) {

            $.ajax( {
                url: 'php/process_signup.php',
                data: $( '#signup_form' ).serialize(),
                type: 'post',
                dataType: 'json',
                success: function( data ) {
                    if ( 'ok' == data.status ) {
                        loader.hideLoader();
                        window.location.href = &quot;login.php&quot;;
                    } else if ( 'fail' == data.status ) {
                        $( '#error_message' ).html( data.message );
                    }
                }
            } );
        } else {
            $( '#error_message' ).html( 'All fields must be filled in.' );
            $( window ).scrollTop( 0 );
        }
    }
&lt;/script&gt;

Скриншот структуры таблицы users

Скриншот структуры БД

0 Answers0