dt_gry Posted September 3, 2008 Share Posted September 3, 2008 I am attempting to move fields from a php form to a mysql database, however I would like to test if certain fields are going to create a duplicate entry in the DB like Username and Email Address. I have the rest of the script written I just want to test for the duplicate entry. <?php $database_connect = mysql_connect("mysql.********.com","username_here","password_here"); if (!$database_connect) { die('Could not connect: ' . mysql_error()); } mysql_select_db("usrmgmt", $database_connect); $entryUsername = username if SELECT * FROM user_info WHERE MATCH (`username`) AGAINST ('$entryUsername') { die(echo "That UserName already exists in our system! Please enter another username") } else { $sql="INSERT INTO user_info(first_name, last_name, billing_address, line_2, city, state_province, zip_postal_code, country, card_type, cc_num, cvv2, exp_month, exp_year, email_address, phone, username, password) VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[billing_address]', '$_POST[line_2]', '$_POST[city]', '$_POST[state_province]', '$_POST[zip_postal_code]', '$_POST[country]', '$_POST[card_type]', '$_POST[cc_num]', '$_POST[cvv2]', '$_POST[exp_month]', '$_POST[exp_year]', '$_POST[email_address]', '$_POST[phone]', '$_POST[username]', '$_POST[password]')"; if (!mysql_query($sql,$database_connect)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($database_connect); } ?> Thanks for the help everyone. George! Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/ Share on other sites More sharing options...
lemmin Posted September 3, 2008 Share Posted September 3, 2008 [code]You have to call the mysql_query() function to actually query the db. Also, die() echoes whatever string is passed to it. $sql = "SELECT * FROM user_info WHERE username = '$entryUsername'"; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result)) die("That UserName already exists in our system! Please enter another username"); $sql="INSERT INTO user_info....[/code] Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/#findComment-632774 Share on other sites More sharing options...
dt_gry Posted September 3, 2008 Author Share Posted September 3, 2008 Parse error: syntax error, unexpected T_VARIABLE on line $sql = "SELECT * FROM user_info WHERE username = '$entryUsername'"; any ideas? Thanks George Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/#findComment-632857 Share on other sites More sharing options...
lemmin Posted September 3, 2008 Share Posted September 3, 2008 You need a semi-colon on this line, like this: $entryUsername = username; Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/#findComment-632885 Share on other sites More sharing options...
dt_gry Posted September 5, 2008 Author Share Posted September 5, 2008 Okay I entered the above code in my script, and it still echos "1 Record Added" and thats with entering in a duplicate username. Could someone please help. I have included the entire code. Thanks <?php $database_connect = mysql_connect("mysql.thegayestwebsiteever.com","thegayestever","emit098nice054"); if (!$database_connect) { die('Could not connect: ' . mysql_error()); } mysql_select_db("usrmgmt", $database_connect); $entryUsername = username; $sql = "SELECT * FROM user_info WHERE username = '$entryUsername'"; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result)) { die("That UserName already exists in our system! Please enter another username"); } Else { $sql="INSERT INTO user_info(first_name, last_name, billing_address, line_2, city, state_province, zip_postal_code, country, card_type, cc_num, cvv2, exp_month, exp_year, email_address, phone, username, password) VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[billing_address]', '$_POST[line_2]', '$_POST[city]', '$_POST[state_province]', '$_POST[zip_postal_code]', '$_POST[country]', '$_POST[card_type]', '$_POST[cc_num]', '$_POST[cvv2]', '$_POST[exp_month]', '$_POST[exp_year]', '$_POST[email_address]', '$_POST[phone]', '$_POST[username]', '$_POST[password]')"; } if (!mysql_query($sql,$database_connect)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($database_connect); ?> Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/#findComment-634162 Share on other sites More sharing options...
lemmin Posted September 5, 2008 Share Posted September 5, 2008 Sorry, I wasn't paying attention to that line, I just noticed the lack of a semi-colon. Change this line from : $entryUsername = username; To: $entryUsername = $_POST['username']; Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/#findComment-634627 Share on other sites More sharing options...
dt_gry Posted September 5, 2008 Author Share Posted September 5, 2008 Thanks lemmin, Worked like a charm now just one more question, I have another database that holds the actual login information, I want to copy more information from that form into another database I know how that would work, but I need the password encrypted to md5 before it copys to login_script database. How would I achieve this? the code I have for this copy process is as follows: $db_con_login_script = mysql_connect("mysql.thegayestwebsiteever.com","thegayestever","emit098nice054); if (!database_connect) { die('Could not connect: ' . mysql_error()); } Else { $sql="INSERT INTO login_script(First_name, Last_name, User_name, Password) VALUES ('$_POST[First_name]', '$_POST[Last_name]', '$_POST[username]', '$_POST[Password]')"; } if (!mysql_query($sql,$db_con_login_script)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($db_con_login_script); Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/#findComment-634730 Share on other sites More sharing options...
lemmin Posted September 5, 2008 Share Posted September 5, 2008 There is an md5() function in PHP: http://us3.php.net/manual/en/function.md5.php Try the SQL like this: $sql="INSERT INTO login_script(First_name, Last_name, User_name, Password) VALUES ('$_POST[First_name]', '$_POST[Last_name]', '$_POST[username]', '".md5($_POST['Password'])."')"; Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/#findComment-634748 Share on other sites More sharing options...
alexisfromboston Posted September 11, 2008 Share Posted September 11, 2008 Im attempting to do the same thing but am having no luck - here is my code: <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO Event_Database (eName, eLink, nContact, pContact, mContact, eMonth, eLength, eCountry, eState, eRating, eNotes, eType, LastUpdate) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['eName'], "text"), GetSQLValueString($_POST['eLink'], "text"), GetSQLValueString($_POST['nContact'], "text"), GetSQLValueString($_POST['pContact'], "text"), GetSQLValueString($_POST['mContact'], "text"), GetSQLValueString($_POST['eMonth'], "text"), GetSQLValueString($_POST['eLength'], "text"), GetSQLValueString($_POST['eCountry'], "text"), GetSQLValueString($_POST['eState'], "text"), GetSQLValueString($_POST['eRating'], "text"), GetSQLValueString($_POST['eNotes'], "text"), GetSQLValueString($_POST['eType'], "text"), GetSQLValueString($_POST['LastUpdate'], "text")); mysql_select_db($database_EventsDatabase, $EventsDatabase); $Result1 = mysql_query($insertSQL, $EventsDatabase) or die(mysql_error()); $insertGoTo = "browse.php?action=addsuccess"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } ?> I'm supposing this is the part of the code that I need: $entryUsername = $_POST['username']; $sql = "SELECT * FROM user_info WHERE username = '$entryUsername'"; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result)) { die("That UserName already exists in our system! Please enter another username"); } Else { something like this? but where does it go $checkURL = $_POST['eLink']; $sql = "SELECT * FROM Event_Database WHERE eLink = '$checkURL'"; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result)) { die("That Link is already exists in our system! "); } Else { I'm using dreamweaver shortcuts for a lot of this - but I understand code enough - that a point in the right direction is all I'll probably need - so if anyone can help that would be great - otherwise I'll keep trying to decipher the code from this post. cheers- Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/#findComment-638897 Share on other sites More sharing options...
lemmin Posted September 11, 2008 Share Posted September 11, 2008 I'm not sure I understand what it is that you are asking. Link to comment https://forums.phpfreaks.com/topic/122558-php-form-to-mysql-testing-for-duplicates/#findComment-639241 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.