Execute SQL Query with button click

  mysql, php, sql

i’m learning how to use buttons in different ways with PHP and MYSQL, but i am stucked.
Is this a good way to put my PHP QUERY or its better to do it in other php file?
Why doesn’t the button works?
Thanks.

<form action="" method = "POST">
    <input type='submit' name='submit' />
    <?php
    if(isset($_POST['submit'])){
        // My query
    $validator = array('success' => false, 'messages' => array());
    $mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);
    $query="UPDATE clientes
    SET estado= case when id < 11 then 'MOROSOS'
    ELSE estado
    END;";
    $resultado=$mysqli->query($query);
    if($resultado === TRUE) {
        $validator['success'] = true;
        $validator['messages'] = "Banco creado";
    } else {
        $validator['success'] = false;
        $validator['messages'] = "Error al agregar la informacion";
    }
    echo json_encode($validator);
    }
    ?>
</form>

Source: Ask PHP

LEAVE A COMMENT