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.
Perfect, Lipe. Thank you very much
– Thiago Vinícius