tartis Posted August 4, 2009 Share Posted August 4, 2009 I am trying to check for category names in my database using PHP and MYSQL. The problem is that if I enter a new platform name in lower case, I am still able to enter a duplicate in upper case, or just the opposite. As an example, if I create a platform named xxx, my code keeps me from creating a new one in the same case. If I try XXX it creates the new duplicate record. I can never again use xxx, but I can always duplicate XXX. I hope that that makes sense. Here is the code: <?php $con = mysql_connect("servername", "username", "password"); if (!$con) { echo "<h2>Sorry, we cannot process your request at this time, please try again later</h2>\n"; echo "<a href=\"index.php?content=newquestion\">Try again</a><br>\n"; echo "<a href=\"index.php\">Return to Home</a>\n"; exit; }mysql_select_db("knowledge", $con) or die('Could not connect to database'); $plat_name = $_POST['plat_name']; $baduser = 0; // Check if platform name was entered if (trim($plat_name) == '') { echo "<h2><br><br>Sorry, you must enter a new platform name.</h2><br>\n"; echo "<a href=\"index.php?content=newplatform\">Try again</a><br>\n"; echo "<a href=\"index.php\">Return to Home</a>\n"; $baduser = 1; exit; } //Check if Platform is already in database $query = "SELECT plat_name from platform where plat_name = '$plat_name'"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_ASSOC);if ($row['plat_name'] == $plat_name) { echo "<h2>Sorry, that Platform name is already taken.</h2><br>\n"; echo "<a href=\"index.php?content=newplatform\">Try again</a><br>\n"; echo "<a href=\"index.php\">Return to Home</a>\n"; $baduser = 1; }if ($baduser != 1) { //Everything passed, enter question into database $query = "INSERT into platform (plat_name) VALUES ('$plat_name')"; $result = mysql_query($query) or die('Sorry, we are unable to process your request.' . mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168843-solved-checking-for-duplicate-records/ Share on other sites More sharing options...
premiso Posted August 4, 2009 Share Posted August 4, 2009 Run a check against the DB and use LOWER() in the SQL on the field you do not want duplicated and check it against the field entered. Use strtolower in php to create the same effect so that you know both items are being checked case insensitive. That way it does not matter if they enter it all upper or lower etc, it should still show that the record is in the database. Another item you can try is adding the "UNIQUE" identifier to the table column's attribute when creating the table. It should make the field be required to be unique, but I would still do the above as an extra measure for case sensitivity. Quote Link to comment https://forums.phpfreaks.com/topic/168843-solved-checking-for-duplicate-records/#findComment-890844 Share on other sites More sharing options...
_DarkLink_ Posted August 4, 2009 Share Posted August 4, 2009 You can always use strtolower() or strtoupper() to convert everything in both variables to upper case or lowercase. Try this: <?php $con = mysql_connect("servername", "username", "password"); if (!$con) { echo "<h2>Sorry, we cannot process your request at this time, please try again later</h2>\n"; echo "<a href=\"index.php?content=newquestion\">Try again</a><br>\n"; echo "<a href=\"index.php\">Return to Home</a>\n"; exit; }mysql_select_db("knowledge", $con) or die('Could not connect to database'); $plat_name = $_POST['plat_name']; $baduser = 0; // Check if platform name was entered if (trim($plat_name) == '') { echo "<h2><br><br>Sorry, you must enter a new platform name.</h2><br>\n"; echo "<a href=\"index.php?content=newplatform\">Try again</a><br>\n"; echo "<a href=\"index.php\">Return to Home</a>\n"; $baduser = 1; exit; } //Check if Platform is already in database $query = "SELECT plat_name from platform where plat_name = '$plat_name'"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_ASSOC);if (strtolower($row['plat_name']) == strtolower($plat_name))//Changed this... { echo "<h2>Sorry, that Platform name is already taken.</h2><br>\n"; echo "<a href=\"index.php?content=newplatform\">Try again</a><br>\n"; echo "<a href=\"index.php\">Return to Home</a>\n"; $baduser = 1; }if ($baduser != 1) { //Everything passed, enter question into database $query = "INSERT into platform (plat_name) VALUES ('$plat_name')"; $result = mysql_query($query) or die('Sorry, we are unable to process your request.' . mysql_error()); } ?> [EDIT]Bah, went a little fasetr than me there. =/[/EDIT] Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/168843-solved-checking-for-duplicate-records/#findComment-890847 Share on other sites More sharing options...
tartis Posted August 4, 2009 Author Share Posted August 4, 2009 strtolower() did the trick. Thanks to both of you. Quote Link to comment https://forums.phpfreaks.com/topic/168843-solved-checking-for-duplicate-records/#findComment-890875 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.