JSHINER Posted February 13, 2008 Share Posted February 13, 2008 How can I check to see if a record is already in the database? Here what I'm using for code: if (isset($_POST['submit'])) { $password = md5($_POST['password']); foreach ($_POST as $key => $value) { $_POST[$key] = $db->escape($value); } $query = ''; $query .= 'INSERT INTO'; $query .= " users SET username = '{$_POST['username']}', password = '{$password}', email = '{$_POST['email']}' "; if (!$db->query($query)) { $page['error'][] = 'Could not save user information.'; } if (!isset($page['error'])) { header('Location: register.php?status=created'); exit(); } } Link to comment https://forums.phpfreaks.com/topic/90947-checking-to-see-if-record-exists/ Share on other sites More sharing options...
cooldude832 Posted February 13, 2008 Share Posted February 13, 2008 run an initial SELECT query for what you want to make sure isn't already there (such as where username = $_POST['Username']) and then if mysql_num_rows on that query >0 then that record is already there. Link to comment https://forums.phpfreaks.com/topic/90947-checking-to-see-if-record-exists/#findComment-466117 Share on other sites More sharing options...
Isityou Posted February 13, 2008 Share Posted February 13, 2008 You could make the field unique. Link to comment https://forums.phpfreaks.com/topic/90947-checking-to-see-if-record-exists/#findComment-466119 Share on other sites More sharing options...
JSHINER Posted February 13, 2008 Author Share Posted February 13, 2008 When I make the field unique I get the MySQL error message - how can I turn that message into "username taken" ? Link to comment https://forums.phpfreaks.com/topic/90947-checking-to-see-if-record-exists/#findComment-466124 Share on other sites More sharing options...
revraz Posted February 13, 2008 Share Posted February 13, 2008 function checkUnique($table, $field, $compared){ if (get_magic_quotes_gpc()) { $table = stripslashes($table); $field = stripslashes($field); $compared = stripslashes($compared); } $table = mysql_real_escape_string($table); $field = mysql_real_escape_string($field); $compared = mysql_real_escape_string($compared); $result = mysql_query("SELECT $field FROM $table WHERE $field = '$compared'"); if(mysql_num_rows($result)==0) { return TRUE; } else { return FALSE; } } Link to comment https://forums.phpfreaks.com/topic/90947-checking-to-see-if-record-exists/#findComment-466132 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.