0
I have the following column structure from my database: account_id, name and appearance. Imagery: http://puu.sh/gYLVL/4083073484.png
I had the following code previously:
<font size="3">Por favor digite o nome do personagem que deseja Resetar a Aparencia</font>
<br><br><br>
<form method="post" action="" id="ajax_form3" class="formulario">
<label for="senha">Nome do Personagem:</label>
<input name="nome" id="nome" type="text" size="40" />
<br /><br /><br />
<input type="submit" name="Submit" value="Resetar Aparencia!" style="cursor:pointer;">
</form>
And your PHP:
<?php
$conexao = mysqli_connect("localhost","root","wamp","ragnarok");
if(trim($_POST["nome"]) == "")
{
echo "<div class='alert alert-danger' role='alert'><img src='images/erro.png'> Por favor digite o nome do personagem!</div>";
exit();
}
if (strlen($_POST["nome"]) < 4)
{
echo "<div class='alert alert-danger' role='alert'><img src='images/erro.png'> Nome curto demais!</div>";
exit();
}
if (strlen($_POST["nome"]) > 23)
{
echo "<div class='alert alert-danger' role='alert'><img src='images/erro.png'> Nome grande demais!</div>";
exit();
}
$query = "SELECT * FROM `char` WHERE name = '".$_POST["nome"]."'";
$resultado = mysqli_query($conexao, $query) or die(mysqli_error($conexao));
$campo = mysqli_fetch_array($resultado);
if ($_POST['nome'] === $campo['name']) {
$query = "UPDATE `char` SET aparencia = '1' WHERE name = '".$_POST["nome"]."'";
$resultado = mysqli_query($conexao, $query) or die(mysqli_error($conexao));
$Uid = mysqli_insert_id($conexao);
echo "<div class='alert alert-success' role='alert'><img src='images/check.png'> Aparencia resetada com sucesso!</div>";
}
else
{
echo "<div class='alert alert-danger' role='alert'><img src='images/erro.png'> Nome do personagem inválido!</div>";
}
mysqli_close($conexao);
?>
Well, this code worked perfectly, took the name of the player typed in the form field, checked if it matched the name of the database and updated each field with its value. However, I am trying to change the method, instead of the person having to type the character’s name, I would like the page to list all the characters that the person has and that when the person clicked on his name, execute the query that changes the values of the fields. That is, same code only instead of input to the name, list the names as buttons. The listing of the characters and application of a button for each is easy, I already managed getting this code:
<form method="post" action="" id="ajax_form3" class="formulario">
<table class="vertical-table th">
<tr align="center">
<th width="10%">
<p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="4"><b>Escolha o personagem que deseja resetar a aparência</b></font></th>
</tr>';
$idconta = $_SESSION['account_id'];
$sql = "SELECT name FROM `char` WHERE account_id = '$idconta' order by `char_id` ASC";
$limite = mysqli_query($db, $sql);
while ($sql = mysqli_fetch_array($limite)) {
$name = $sql['name'];
echo '
<tr>
<td>
<br>
<button style="margin-top: 0; margin-bottom: 0; font-size:3; cursor:pointer;" class="botao" id="'.$name.'">'.$name.'</button></td>
</tr>
</table>
</form>
The problem now is, how do I stop when the person clicks on a button with the character’s name, change the database in the row of that name to change the appearance column to 1? If possible I would like to continue using the method within the form, because it returns through ajax a success message without the need to reload the page.
Current code:
<table class="vertical-table th">
<tr align="center">
<th width="10%">
<p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="4"><b>Escolha o personagem que deseja resetar a aparência</b></font></th>
</tr>';
$idconta = $_SESSION['account_id'];
$sql = "SELECT name FROM `char` WHERE account_id = '$idconta' order by `char_id` ASC";
$limite = mysqli_query($db, $sql);
while ($sql = mysqli_fetch_array($limite)) {
$name = $sql['name'];
echo '
<tr>
<td>
<br>
<p style="margin-top: 0; margin-bottom: 0; font-size:3; cursor:pointer;"><a href="alteraraparencia.php?id="'.$sql['name'].'">'.$name.'</a></p></td>
</tr>
</table>
PHP:
<?php
$conexao = mysqli_connect("localhost","root","wamp","ragnarok");
mysqli_query($conexao, "UPDATE `char` SET aparencia = '1' WHERE name = " . $_GET['name'])
or die(mysqli_error($conexao));
?>
Do you want to change the value of the "appearance" column when the person clicks on the name? And what value would it be changed to? Where would you take it from? Since you did not specify more details of how you would like this update to be (without updating the page / redirecting), it is difficult to indicate anything.
– Rafael Withoeft
Exactly, change the appearance value when the person clicks on the name! The value would be changed from 0 to 1 in the case. This is precisely my question, how will I get the value of this button that has the name of the person and execute the query? If it were a text field you could pick up by the $_POST method but as it is a boot as I get this value?
– GGirotto
Any answers solved the problem? Don’t forget to accept any.
– Maniero