Jump to content

Help with checking duplicates


lingo5

Recommended Posts

Hi, I`m inserting anew user in the DB like this:

 

$query = "INSERT INTO database.t_usuario (usuario_nombre,usuario,password) ".
"VALUES ('$usuario_nombre','$usuario','$password')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 


header("Location: PC_users_display.php"); 
}

what I need to do is to check if the inserted usuario_nombre already exists on the DB and if so echo a message. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/239893-help-with-checking-duplicates/
Share on other sites

$query = "INSERT IGNORE database.t_usuario (usuario_nombre,usuario,password) ".
"VALUES ('$usuario_nombre','$usuario','$password')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

if(mysql_affected_rows==1)
header("Location: PC_users_display.php"); 
}
else{
echo "Duplicate Entry Detected.";
}
}

Sorry... missed the opening { bracket on the if statement:

 

$query = "INSERT IGNORE database.t_usuario (usuario_nombre,usuario,password) ".
"VALUES ('$usuario_nombre','$usuario','$password')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

if(mysql_affected_rows==1){
header("Location: PC_users_display.php"); 
}
else{
echo "Duplicate Entry Detected.";
}

I've changed my mind and I want to insert users in a different table. This is how I insert a nw user at the moment:

$query = "INSERT INTO database.t_clientes (company_name,cliente_calle,cliente_numero,cliente_local,cliente_poblacion,cliente_cp,cliente_provincia,cliente_tel,cliente_fax,cliente_movil,cliente_personadecontacto,cliente_email,cliente_password,cliente_logo) ".

"VALUES ('$company_name','$cliente_calle','$cliente_numero','$cliente_local','$cliente_poblacion','$cliente_cp','$cliente_provincia','$cliente_tel','$cliente_fax','$cliente_movil','$cliente_personadecontacto','$cliente_email','$cliente_password','$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

header("Location: PC_clientes_display.php"); 
}

 

I want to check if '$cliente_email' (client's email address) already exists before inserting the new user. If so, print an error message.

I assume you are using MySQL.

 

Create a UNIQUE INDEX on the field that you do not want duplicated. (see MySQL manual).

 

 

Wrap your code that actually inserts in a TRY{...}CATCH{...} block. If it enters the CATCH block,

examine the error message, and perform further processing. Or just notify the user of the error message.

 

 

Alternatively, before inserting, perform a

 

SELECT * FROM t_clientes  where cliente_email= '$cliente_email'

 

It that query returned rows (i.e., row >= 1)

 

throw a "email already used" exception.

Thanks a lot ebmigue, this is how i've finally done it and it works:

 

mysql_select_db($database_MySQLconnect, $MySQLconnect);

$query_checkdup_RS = sprintf("SELECT * FROM t_clientes WHERE cliente_email = %s", GetSQLValueString($colname_checkdup_RS, "text"));

$checkdup_RS = mysql_query($query_checkdup_RS, $MySQLconnect) or die(mysql_error());

$row_checkdup_RS = mysql_fetch_assoc($checkdup_RS);

$totalRows_checkdup_RS = mysql_num_rows($checkdup_RS);

 

if($totalRows_checkdup_RS==1)

{

echo "Duplicate Entry Detected.";

}

 

else{

 

$query = "INSERT INTO databse.t_clientes (company_name,cliente_calle,cliente_numero,cliente_local,cliente_poblacion,cliente_cp,cliente_provincia,cliente_tel,cliente_fax,cliente_movil,cliente_personadecontacto,cliente_email,cliente_password,cliente_logo) ".

 

"VALUES ('$company_name','$cliente_calle','$cliente_numero','$cliente_local','$cliente_poblacion','$cliente_cp','$cliente_provincia','$cliente_tel','$cliente_fax','$cliente_movil','$cliente_personadecontacto','$cliente_email','$cliente_password','$filePath')";

 

mysql_query($query) or die('Error, query failed : ' . mysql_error());

 

header("Location: PC_clientes_display.php");

}

}

 

Now I want to print the error message within the html code instead of inside the head section.

I have tried this


$duplicate = "<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="CP_SiNoText">la direccón de email ya existe en la base de datos.</td>
</tr>
</table>";

 

and then rinting it inside the html section like this

 

<?php echo $duplicate;?>

but nothing happens. What am I doing wrong?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.