63689ae0e9
Converted MySQLi Database.php class to PHP_PDO objects
40 lines
1.4 KiB
PHP
40 lines
1.4 KiB
PHP
<?php
|
|
if ( ! isset ( $_SESSION [ 'user_id' ] ) ) {
|
|
header ( 'Content-Type: application/json' );
|
|
http_response_code ( 401 );
|
|
$response = [
|
|
'status' => 401,
|
|
'error_message' => 'You either are not logged in or you do not have permissions to insert new URLs.'
|
|
];
|
|
echo ( json_encode ( $response ) );
|
|
exit;
|
|
}
|
|
if ( ! isset ( $_POST [ 'url' ] ) ) {
|
|
header ( 'Content-Type: application/json' );
|
|
http_response_code ( 400 );
|
|
$response = [
|
|
'status' => 400,
|
|
'error_message' => 'You either did not provide a URL or you provided an invalid one.'
|
|
];
|
|
echo ( json_encode ( $response ) );
|
|
exit;
|
|
}
|
|
$database = new Database ( $GLOBALS [ 'config' ] [ 'db' ] );
|
|
$database->connect ( );
|
|
$url = $_POST [ 'url' ];
|
|
$database->prepare ( "INSERT INTO `links` ( `URL`, `created_by` ) VALUES ( :url, " . $_SESSION [ 'user_id' ] . " );" );
|
|
$database->bind ( [ ':url' => $url ] );
|
|
$database->execute ( );
|
|
$database->prepare ( "SELECT MAX( links.ID ) as `last` FROM links;");
|
|
$database->execute ( );
|
|
$result = $database->get_result ( );
|
|
$row = $result->fetchAll ( ) [ 0 ];
|
|
header ( 'Content-Type: application/json' );
|
|
$response = [
|
|
'status' => 200,
|
|
'message' => 'URL inserted correctly.',
|
|
'last_insert' => $row [ 'last' ],
|
|
];
|
|
echo ( json_encode ( $response ) );
|
|
exit;
|