Я новичек в 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->prepare( '
SELECT
*
FROM
users
WHERE
email = :email
' );
$statement->setFetchMode( PDO::FETCH_ASSOC );
$statement->execute( array(
'email' => trim( $email )
) );
$user = $statement->fetch();
return $user;
}
function signUserUp ( $info ) {
$databaseConnection = getDatabaseConnection();
$statement = $databaseConnection->prepare( '
INSERT INTO
users (
email,
first_name,
last_name,
password,
key_value
)
VALUES (
:email,
:first_name,
:last_name,
:password,
:key_value
)
' );
$statement->execute( array(
'email' => trim( $info['email'] ),
'first_name' => trim( $info['first_name'] ),
'last_name' => trim( $info['last_name'] ),
'password' => hashedPassword( $info['password'] ),
'key_value' => newKey(),
) );
return $databaseConnection->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>
<script>
$( 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 = "login.php";
} else if ( 'fail' == data.status ) {
$( '#error_message' ).html( data.message );
}
}
} );
} else {
$( '#error_message' ).html( 'All fields must be filled in.' );
$( window ).scrollTop( 0 );
}
}
</script>

