Problem when checking if user already exists before registering

Asked

Viewed 142 times

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 the database if what was typed already has there, but I’m not getting.

<?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");
}
?>

1 answer

0


Your search query is wrong, it should be so:

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

HOWEVER: your code has serious compatibility and safety problems. I strongly suggest consulting this other question: Why should we not use mysql type functions_*?

  • For those who are starting it is difficult to rewrite mysql for mysqli, but thanks, solved there.

Browser other questions tagged

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