Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mozew's avatar
Level 6

How to save info with ajax in php?

Look at my codes.

<form id="addAddress" action="ShoppingController/store" method="post">
    <span class="close"></span></h4>
    <div class="group">
        <div class="right">
            <label for="first_last_name">first_last_name:</label>
        </div>
        <div class="left">
            <input id="first_last_name" name="first_last_name" type="text" />
        </div>
    </div>
    <div class="group">
        <div class="right">
            <label for="mobile>mobile:</label>
        </div>
        <div class="left">
            <input id="mobile" name="mobile" type="text" />
        </div>
    </div>
    <div class="group">
        <div class="right">
            <label for="telephone">phone:</label>
        </div>
        <div class="left">
            <input id="telephone" name="telephone" type="text" />
        </div>
    </div>
    <div class="group">
        <div class="right">
            <label for="state_id">state:</label>
        </div>
        <div class="left">
            <select id="state_id" name="state_id">
                <option value="1">1</option>
                <option value="2">آ2</option>
                <option value="3">3</option>
                <option value="4">3</option>
            </select>
        </div>
    </div>
    <div class="group">
        <div class="right">
            <label for="city_id">city:</label>
        </div>
        <div class="left">
            <select id="city_id" name="city_id">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
            </select>
        </div>
    </div>
    <div class="group">
        <div class="right">
            <label for="address">address:</label>
        </div>
        <div class="left">
            <input id="address" name="address" type="text" />
        </div>
    </div>
    <div class="group">
        <div class="right">
            <label for="postal_code">postal_code</label>
        </div>
        <div class="left">
            <input id="postal_code" name="postal_code" type="text" />
        </div>
    </div>
    <button class="btn-success">save</button>
</form>

ShoppingController.php

function store()
{
    $this->model->save($_POST);
}

Shopping.php

function save($data)
{
    Model::session_start();
    $user_id = Model::session_get('user_id');
    $first_last_name = $data['first_last_name'];
    $mobile = $data['mobile'];
    $telephone = $data['telephone'];
    $state_id = $data['state_id'];
    $city_id = $data['city_id'];
    $address = $data['address'];
    $postal_code = $data['postal_code'];
    $sql = "INSERT INTO user_address (user_id, first_last_name, mobile, telephone, state_id, city_id, address, postal_code)";
    $params = [$user_id, $first_last_name, $mobile, $telephone, $state_id, $city_id, $address, $postal_code];
    $this->doQuery($sql, $params);
}

I am using with php mvc,I did not with laravel

I get this error

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 in C:\xampp3\htdocs\projects\digikala\php\core\Model.php:62 Stack trace: #0 C:\xampp3\htdocs\projects\digikala\php\core\Model.php(62): PDOStatement->execute() #1 C:\xampp3\htdocs\projects\digikala\php\models\Shopping.php(23): Model->doQuery('INSERT INTO use...', Array) #2 C:\xampp3\htdocs\projects\digikala\php\controllers\ShoppingController.php(12): Shopping->save(Array) #3 C:\xampp3\htdocs\projects\digikala\php\core\App.php(28): ShoppingController->store() #4 C:\xampp3\htdocs\projects\digikala\php\index.php(6): App->__construct() #5 {main} thrown in C:\xampp3\htdocs\projects\digikala\php\core\Model.php on line 62

0 likes
1 reply
rodrigo.pedra's avatar
Level 56

In your case you are missing the VALUES part on your SQL's INSERT statement:

    $sql = "INSERT INTO user_address (user_id, first_last_name, mobile, telephone, state_id, city_id, address, postal_code) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

But I would also consider changing the code on your ShoppingController.php to this:

function store()
{
    $this->model->save(request()->all());
}

Avoid using superglobals with Laravel. Laravel wraps superglobals in the Request object for extra security checks and conveniences, such as validation, and other useful features.

1 like

Please or to participate in this conversation.