Form to change value in the database

Asked

Viewed 83 times

0

I have it in the database

inserir a descrição da imagem aqui

I wanted a form that by clicking the button Enviar change the value of pontos user selected. But for now I only have this, I do not know how to make the code in PHP.

    <?php
$connect = mysql_connect('xxxxxxxx','xxxxxxx','xxxxxxxxx'); $db = mysql_select_db('xxxxxxx');

<form>
Qual usuário deseja mudar?<br>
  <select name="usuario">
    <option value="todos usuários do banco de dados aqui">todos usuários do banco de dados aqui</option>
  </select><br>
 Novo valor:<br>
  <input type="text" name="pts"><br>
   <input type="submit" value="Enviar">
</form>

2 answers

1


The old functions mysql_* were removed from PHP. You must use mysqli_* or PDO. I’ll make an example using Mysqli structured.

But for now I only have this, I don’t know how to do the code in PHP

1) One simple way is to make a file with the database connection and include it where and when you need it:

Filing cabinet conexao_db.php:

<?php

$conexao = mysqli_connect(
    // Altere conforme necessário
    'xxxxxxxx',    // Host do banco de dados
    'xxxxxxxx',    // Usuário
    'xxxxxxxx',    // Senha
    'xxxxxxxx'     // Nome do banco de dados
);

if (mysqli_connect_errno()) {
    printf(
        'Erro na conexão com o banco de dados: %s',
        mysqli_connect_error()
    );
    die();
}

2) Display users on select:

Filing cabinet index php.:

<?php

require_once('conexao_db.php');

$query = mysqli_query($conexao, 'SELECT * FROM `usuarios`;');

$usuarios = mysqli_fetch_all($query, MYSQLI_ASSOC);

?>

<form action="/setpontos.php" method="post">
    Qual usuário deseja mudar?<br>
    <select name="usuario">
        <?php
        foreach ($usuarios as $usuario) { ?>
            <option value="<?= $usuario['ID']; ?>"><?= $usuario['login']; ?></option>
        <?php
        } ?>
    </select><br>
    Novo valor:<br>
    <input type="text" name="pts"><br>
    <input type="submit" value="Enviar">
</form>

3) Note that in index php., the action of form is the file setpontos.php and the method is post. Let’s make this file now

Filing cabinet setpontos.php:

<?php

require_once('conexao_db.php');

$sql_query = (
    "UPDATE `usuarios`
    SET `pontos` = '".$_POST['pts']."'
    WHERE `ID` = '".$_POST['usuario']."';"
);

if (!($query = mysqli_query($conexao, $sql_query))) {
    printf(
        'Erro ao executar consulta no banco de dados: %s',
        mysqli_error($conexao)
    );
} else {
    echo 'Pontos do usuário definidos com sucesso!';
}

If you "Ctrl+C » Ctrl+V" correctly, everything should work. Remembering that all the files should be in the root directory.

Project available in my Github/lipespry/sopt-formulario-mudar-pontos-usuario.

Is this the best way? No. But it will be a starter for you to understand how it works.

-1

Talks Iago blz, not working with php, more work with java, my tip would be, you search by id, after returning the query with the fields you can update to the points, there are several ways to update, with fixed value in the code itself(ex: vc has a variable with a fixed value, vc arrow it "Update 'table name' SET = 'value of the variable' where id = 'desired id'" ) or you can fetch the last value of the points column save in a variable then update with its variable with the stored value and increment -- +1, +2 --- so on....

  • The theory is very important, but: -"don’t know how to make the code in PHP".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.