0
Description
First, the field Seleciona jogo
returns all registered games (This field works as the selection of the game ID to update given game) and what I would like to know is how do so that the other fields below the Seleciona jogo
the values in the database columns are dynamically filled in to facilitate the update process.
Advantages of this:
- Saves time as the user chooses the field he wants to update without having to fill in change the other fields.
Example:
- Selects game:
Watch Dogs 2
(ID). - Name:
Watch Dogs 2
(value that is in the column game tablenome
). - Price:
23,99
(value that is in the column game tablepreco
is automatically shown in the fieldPreço
). - Platform:
PC
(value that is in the column game tableplataforma
is automatically shown in the fieldPlataforma
). - Developer:
Ubisoft
(value that is in the column game tabledesenvolvedora
is automatically shown in the fieldDesenvolvedora
). - The only field I will update is the
preco
, of23,99
for19,99
. After that I press the buttonAtualizar jogo
I didn’t even have to fill in the other fields I didn’t want to update (because if I leave the blank fields the columns are empty in the database, except for the columnpreco
).
The image of the form
PHP code
<?php
require_once 'conexao.php';
if(isset($_POST['Atualizar'])){
$cd_jogo = $_POST['cd_jogo'];
$nome = $_POST['nome'];
$preco = $_POST['preco'];
$plataforma = $_POST['plataforma'];
$desenvolvedora = $_POST['desenvolvedora'];
try {
$update = "UPDATE jogo SET nome = :nome, cpf = :cpf, preco = :preco,
plataforma = :plataforma, desenvolvedora = :desenvolvedora WHERE cd_jogo = :cd_jogo";
$atualiza = $conexao->prepare($update);
$atualiza->bindValue(':cd_jogo',$cd_jogo);
$atualiza->bindValue(':nome',$nome);
$atualiza->bindValue(':preco',$preco);
$atualiza->bindValue(':plataforma',$plataforma);
$atualiza->bindValue(':desenvolvedora',$desenvolvedora);
$atualiza->execute();
} catch (PDOException $falha) {
echo "A atualização não foi feita".$falha->getMessage();
}
}
$seleciona = $conexao->query("SELECT cd_jogo, nome FROM jogo");
$resultado = $seleciona->fetchAll();
?>
Form
<form method="POST">
<p> Seleciona jogo:
<select name="cd_jogo" required="">
<option value=""> </option>
<?php
foreach ($resultado as $v) {
echo "<option value='{$v['cd_jogo']}'>{$v['nome']}</option>";
}
?>
</select>
</p>
<p> Nome: <input type="text" name="nome" size=30 maxlength="30" required=""> </p>
<p> Preço: <input type="number" name="preco" step="10.00" required=""> </p>
<p> Plataforma: <input type="text" name="plataforma" size=30 maxlength="30" required=""> </p>
<p> Desenvolvedora: <input type="text" name="desenvolvedora" size=30 maxlength="30" required=""> </p>
<p> <input type="submit" name="Atualizar" value="Atualizar jogo"> </p>
</form>
If possible, try to be more objective and specific in your doubt. From what I understand, you don’t need this context
– Leonardo Negrão
For my question not to be closed I had to bring as much information as possible of my doubt to people understand.
– user191392
The information needs to be specific to the doubt. But come on, you want to select a select option with the label "Select Game" and the fields would be filled dynamically; that’s it?
– Leonardo Negrão
@Leonardonegrão exactly, according to what is filled in the columns of a given game ID.
– user191392
All right, I’ll try to help you
– Leonardo Negrão
Dynamic fill also works for type fields
<select>
? I wanted to do it in the future.– user191392
Column-to-column
Plataforma
I wanted to put a<select>
with a<option>
with the options ofPC
,PS4
andXbox One
and make this dynamic fill in this<select>
, for the<option>
by default will always be empty.– user191392