Problem checking user already exists before registering

0

I'm starting now, and I have a question. I had done to register a category, until then ok, but I realized that I was registering even with an existing category, then I decided to add a function to check in the database if what was entered already has it, but I can not.

<?php
session_start();
$adicionar_categoria = $_POST['categoria'];
include_once("../../config/conexao.php");

//Verifica se a categoria já existe.
    $vcategoria = mysql_query("SELECT * FROM categorias(nomeCategoria) LIMIT 1");

//Se o retorno for maior que 0, diz que já existe uma categoria com o mesmo nome.
if(mysql_num_rows($vcategoria) > 0){
    $_SESSION['categoriaErro'] = "Categoria existente.";
    header("Location: ../adicionar_categoria.php");
}else{
$resultCategoria = mysql_query("INSERT INTO categorias(nomeCategoria) VALUES ('$adicionar_categoria')");
}
//echo "Categoria: ".$resultCategoria['nomeCategoria'];

if(empty($resultCategoria)){
    $_SESSION['categoriaErro'] = "Por favor, adicione uma categoria válida.";
    header("Location: ../adicionar_categoria.php");
}else{
    $_SESSION['categoriaSucesso'] = "Categoria adicionada com sucesso.";
    header("Location: ../adicionar_categoria.php");
}
?>
    
asked by anonymous 25.11.2017 / 22:42

1 answer

0

Your search query is wrong, it should look like this:

SELECT * FROM categorias WHERE nomeCategoria = '$adicionar_categoria' LIMIT 1

BUT: Your code has serious compatibility and security issues . I strongly suggest looking at this other question: Why should not we use functions of type mysql_ *?

    
25.11.2017 / 23:06