-1
How can I fix the code below?
It takes the spaces between the commas of the genres or if I have these genres "action, adventure, romance" it puts so "action, adventure, romance" right? Only if I do the reverse order (like: "romance, adventure, action") or place with more spaces between the commas in an altered or equal way (plus the less so: "action,adventure, romance" or "action, romance, adventure") keeps giving error of inserting the same item in Mysql table genres.
How can I do in case no matter the order I put the genders or spaces between commas make the single addition of a single gender and if you have already registered in Mysql do not add it again.
How can I fix this?
<?php $generoMinusculo = strtolower(trim($_POST["genero"]));
if ($generoMinusculo) {
$generoMinusculo = str_replace(' ,',',',$generoMinusculo);
$generoMinusculo = str_replace(' , ',',',$generoMinusculo);
$generoMinusculo = str_replace(' , ',',',$generoMinusculo);
$generoMinusculo = str_replace(' , ',',',$generoMinusculo);
$generoMinusculo = str_replace(' , ',',',$generoMinusculo);
$generos = explode("," , $generoMinusculo);
foreach ($generos as $item) {
$Generos = mysql_query("SELECT * FROM `generos` WHERE `genero`='".$item."'");
$verificar = mysql_fetch_array($Generos);
$verificarGenero = $verificar["genero"];
$generoPostado = $item;
if ($generoPostado != $verificarGenero){
mysql_query("INSERT INTO `generos` SET `genero`='".$item."'");
}elseif ($generoPostado == $verificarGenero){ }
else { } } ?>
If possible, add the current table data to the question to see how it is.
– Tony
If you have recorded
romance, aventura, ação
and will give an insight intoaventura
, you will need to select this table and pass the data to an array withexplode
. Then just check ifaventura
is in the array to insert or not.– Papa Charlie