System to identify the user owns the PHP + Mysql topic

Asked

Viewed 123 times

1

GOOD AFTERNOON. I’m doing a help forum on my website, and I came across a question. I tried to make sure that when the topic owner had the question answered, he himself could close the topic. However, I have a certain message when trying to display the message for it to click. I tried that:

    <?php
        $connect = mysql_connect('localhost','root','');
        $db = mysql_select_db('registro');
        $id = $_GET["id"];
    $sql = "SELECT `Autor` FROM topicos WHERE `ID` = '$id'";
    $limite = mysql_query("$sql");
    if ($sql = $login_cookie ) { // o $login_cookie é minha variavel que seleciona o nome de usuário, quando ele está ativo na conta
// então, tentei comparar o nome de usuário com o nome do autor do tópico
        echo "Fechar tópico";
    } else {
        echo "oi"; // mensagem só para certificar se funcionou
    }
    ?>

But I didn’t get any results.

I don’t want a complete code, I just want to know how I could do it, that is, how I could do it.

Well, through this edition, I have come to say that this system would help me in several other functions also in my forum. I forgot to inform also what is happening: By entering this code, the message appears regardless of who is logged in. I don’t know if an image should help, but I believe it shows better what I do: http://i.imgur.com/Xrexho6.png

1 answer

1


In the if are two signs of = for comparison. You didn’t get the result of yours query, for that I used the mysql_fetch_array().

<?php
    $connect = mysql_connect('localhost','root','');
    $db = mysql_select_db('registro');
    $id = $_GET["id"];

    $sql = "SELECT Autor FROM topicos WHERE ID = $id";
    $limite = mysql_query($sql);
    $res = mysql_fetch_array($limite);

    if ($res['Autor'] == $login_cookie ) { 
        echo "Fechar tópico";
    } else {
        echo "oi";
    }
?>

Verification in it SELECT:

SELECT Autor FROM topicos WHERE ID = $id AND Autor = '$login_cookie';

You are checking above to see if the specific topic was created by the logged-in Author.

Then you do another check asking how many rows have been returned. In this case return 1 or 0.

So:

$limite = mysql_query($sql);
$numlinha = mysql_num_rows($limite);

if($limite == 1)
    echo "Criou";
else
    echo "Não criou";
  • 1

    I will try here and I have already put results. Thank you for the reply, from now on.

  • It worked great. Thanks, buddy. In short: I needed to take the result of querry, instead of trying to compare the select and by two equal signs in comparison, right?

  • 1

    Yes. Knowing right is important, but it’s also important that you understand what you did wrong. In addition, there are several ways to do this check. This is one of them and even not the most suitable. You can do it all in the same SELECT.

  • Thank you. Once again

Browser other questions tagged

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