There is a working code with user registration and a button that displays a random image from the database. So the question is, how can I make it so that when the button for displaying this image on the site is pressed, the data of this image (name and id) are written to the table for the user. index.php

<!DOCTYPE html><htmllang="eng">   <head> <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="ie=edge">        <meta name="viewport" content="width=device-width, initial-scale=1">    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">       <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">    <link rel="stylesheet" href="css/style.css">    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>    </head><body> <div class="container">            <button class="btn btn-primary" data-toggle="modal" data-target="#myModal">Login</button>            <div class="modal fade" id="myModal">                <div class="modal-dialog">                    <div class="modal-content">                        <form id="form" role="form">                            <div class="modal-header">                                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&
times;</span><span class="sr-only">Close</span></button>                                <h4 class="modal-title">Login</h4>                            </div>                            <div class="modal-body">                                <div id="messages"></div>                                <input type="text" class="form-control" name="login"
               id="login" placeholder="vvedi login">                            </div>                            <div class="modal-footer">                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>                                <button type="submit" class="btn btn-primary">Login</button>                            </div>                        </form>                    </div>                </div>            </div>        </div>        <form id="box1" method="post" class="small-form">           <div><button class="btn btn-info" name="img">mint</button></div>        </form>        <form id="box2" method="post" class="small-form">                <div><button class="btn btn-info">mint</button></div>             </form>             <form id="box3" method="post" class="small-form">                     <div><button class="btn btn-info">mint</button></div>                  </form>        </div>        <div class="text-center img-block" id="img" name="img" >        </div>        <script src="http://code.jquery.com/jquery.js"></script>        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>         <script src="post-ajax.js"></script>        <script>            $('#form').submit(function(e) {
                var form= $(this);
                formdata= false;if(window.FormData){ formdata= new FormData(form[0]);
                var formAction= form.attr('action');
                    type : 'POST'
                    url : 'check.php',
                    cache : false
                    data : form data ? formdata : form.serialize(),
                    contentType : false
                    processData : false
                    dataType: 'json'
                    success: function(response) {
                        if(response.type== 'success') {
                            $('#messages').addClass('alert alert-success').text(response.message);
                        } else {
                            $('#messages').addClass('alert alert-danger').text(response.message);
        </script>    </body></html>


$login= filter_var(trim($_POST['login']),
$mysql= new mysqli('localhost','root','root','register-bd');
$mysql->query("INSERT INTO `users` (`login` ,`coin`) VALUES('$login' , '$coin')");
header('Location: /');


$connect= mysqli_connect('localhost', 'root', 'root', 'register-bd');
//If the connection did not work, display an error message
if (!$connect)
    echo 'Error Code: ' . mysqli_connect_errno() . '<br>';
    echo 'Error Message: ' . mysqli_connect_error() . '<br>';
        //Create a query
        $query= 'SELECT id,name,filename
            FROM box1
            ORDER BY rand() LIMIT 1';
        //Execute the query
        $result= mysqli_query($connect, $query);
        //If there is no result, display an error message
        if (!$result)
            echo 'Error Message: ' . mysqli_error($connect) . '<br>';
        //Display the number of recirds found
        echo '

The query found ' . mysqli_num_rows($result) . 'rows:

'; //Loop through the records found while ($record= mysqli_fetch_assoc($result)) { //Output the record using if statements and echo echo '<hr>'; echo '<div class=table name:img>'.$record['name'].'</div>'; echo '<img src="'.$record['filename'].'" width="300">'; } ?>


$("#box1").submit(function(e) {
    $.post("box1.php", {
        typeImg: $('[name="typeImg"]:checked').val()
    .done(function(result) {
        img= $(".img-block img");
        if (img.length > 0) {
            img.one('load', function() {
                console.log('img load');
        } else $('.loader').hide();
    .fail(function() {

Well, in the code for getting a picture from the database, add the code for writing information to the database. What exactly is the problem?

ArchDemon2022-01-25 20:21:13

in that apparently ajax starts to somehow conflict with the action and an error is generated instead of the desired one

user7345642022-01-25 20:21:13

$mysql->query("INSERT INTO inv (img) VALUES('$name')"); I understand that you need to add this line to the new table, where the name of the picture will be attached to the authorized user, but something does not work

user7345642022-01-25 20:21:13

hard to understand without code where and what you added. And without the text of the error, it is completely impossible to understand why it does not work.

ArchDemon2022-01-25 20:21:13

added what I did in the code

user7345642022-01-25 20:21:13