Execute a query when clicking the result of another query [closed]

0

I have the following column structure from my database: account_id, name and appearance. Image: link

I had the following code before:

<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 fine, got the player's name typed into the form field, checked to match the database name, and updated each field with its value. However, I'm trying to change the method, instead of the person having to enter the name of the character, I would like the page to list all the characters that the person owns and that when the person clicks on his name, execute the query that changes the characters. field values. That is, even just code that instead of input to the name, list the names as buttons. The listing of the characters and application of a button for each one is easy, I already managed to get 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 when the person clicks a button with the character's name, change the database in the line of that name to change the appearance column to 1? If possible I would like to continue using the method inside the form, as 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));
?>
    
asked by anonymous 02.04.2015 / 13:01

1 answer

2

The question has been edited and shows that there are several issues. In fact every question is compromised.

You have two major problems, among several other small ones that I will not even try to solve.

The first big problem is that you are not listing the characters. As the original question is not about this, I will not go into details about this. Open another question to solve this problem.

The second is what I'm going to focus on because it was the original intent of the question.

To list the items you have to put a link for this name and call another page to show it. I will not even complain about the old style of your HTML. So:

<p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#009900" size="3">
    <a href="#">#                                    
02.04.2015 / 13:30