0
How do I check if a field of my table is equal to the one typed ? I’m trying to make a friendly URL, only I’m having trouble checking the URL if it already exists in the database, what am I doing wrong ? is only printing the message of Esta URL não está disponível
even though I typed one that wasn’t in the bank
INDEX.PHP
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="jquery.min.js"></script>
<title>Bem Vindo</title>
</head>
<body>
<input type="text" id="url"/>
<div id="resultados"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#url").on('keyup', function(){
var url = $("#url").val();
$.ajax({
url: 'buscaURL.php',
type: 'POST',
data: {urlParaMontar: url},
beforeSend: function(){
$("#resultados").html("Carregando...");
},
success: function(data){
$("#resultados").html(data);
},
error: function(){
$("#resultados").html("Ouve um erro ao enviar sua URL");
}
});//ajax
});
});
</script>
</body>
</html>
searchURL.php
<?php
$url = $_POST['urlParaMontar'];
$a = array('A', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'B', 'C', 'Ç', 'D', 'E', 'È', 'É', 'Ê', 'Ë', 'F', 'G', 'H', 'I', 'Ì', 'Í', 'Î', 'Ï', 'J', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Z', 'W', 'Y', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 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u', 'u', 'u', 'y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'ij', 'ij', 'j', 'j', 'k', 'k', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'oe', 'oe', 'r', 'r', 'r', 'r', 'r', 'r', 's', 's', 's', 's', 's', 's', 's', 's', 't', 't', 't', 't', 't', 't', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'w', 'w', 'y', 'y', 'y', 'z', 'z', 'z', 'z', 'z', 'z', 's', 'f', 'o', 'o', 'u', 'u', 'a', 'a', 'i', 'i', 'o', 'o', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'a', 'a', 'ae', 'ae', 'o', 'o','e','-','-','-','-','-','-','-','-');
$urlNova = str_replace($a,$b,$url);
$conexao = new PDO('mysql:host=localhost;dbname=noticias',"root","");
$sql = $conexao->prepare("SELECT * FROM `artigos`");
$sql->execute();
$fetchAll = $sql->fetchAll();
if(isset($urlNova) && $urlNova === $urlNova){
echo "Esta URL não está disponível";
}else{
echo $urlNova;
}
?>
and in the table artigos
only has the field url
that has the value url-teste-do-site
There’s a WHERE missing?
– Sam
where url = '$url'
? i am not passing any parameter in the URL, I have an input that type any url, if it is not in the database it shows it normally, if it is showing this error message, however it is showing the error even if the url is not in the database– goio
Well eh, I find it strange not to check if it exists in the bank with WHERE.
– Sam
the only solution was to leave the field
unico
no bank, but I didn’t want to do it, I wanted a nice check, if you know a solution put there friend, thank you– goio
leave some examples for us, otherwise it becomes difficult, what you want is to check if some parameter of the url is equal to a field in the table would be this?
– danilo
the code you posted doesn’t make sense as @dvd has commented, the way you posted it, it will always fall on the condition that print is not available
– danilo
A nice check would be to see if the bank has such a URL with WHERE. From what I’m seeing, you’re doing nothing but make a simple bank consultation with
"SELECT * FROM
articles")
– Sam
what’s in your articles table? and what’s in your url?
– danilo
A cute question would be to also post HTML to avoid that we have extra work in typing the same.
– user60252
Not to mention that if doesn’t make any sense.
– Sam
edited the question, see if they understand better now, what I want is just to check if the input field is already registered in the database, I thought to use the
where
I would have to pass some parameter onurl
– goio
Blz, I’m gonna study this case.
– user60252