datoshway Posted July 14, 2011 Share Posted July 14, 2011 Hello guys! I need some help adding some validation to my php form. I need have this check to see if there is already an entry in the database otherwise throw a error or even better just forward the user to a error page. Here is my foundation process code in my process file that inserts the data and emails it out. Thanks in advance! function process() { $msg = "<strong>Registration Details</strong>: <br /> <br /> \n\n"; foreach($this->fields as $key => $field) $msg .= "<strong style='color:#666;'>$key</strong>: $field \n <br />"; $to = '[email protected]'; $subject = "Subject Line"; $from = "[email protected]"; mail($to, $subject, $msg, "Content-type:text/html;charset=utf-8\r\nFrom: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); mail($_POST['Contact'], $subject, '<html> <div style="background-color: #FFF; color: #536d86; margin-left:10px; height:1000px; font-size:15px; margin:0px; padding:0px; font-family: Arial, Helvetica, sans-serif;"><a href="http://XXX.com/"><img border="0" src="http://XXX.com/thanks.jpg" /></a><br />'. "$msg </div> </html>", "Content-type:text/html;charset=utf-8\r\nFrom: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); $db_host = "localhost"; $db_user = "XXX";$db_pass = "XXX";$db_name = "XXX";$db_table = "entries"; $link = mysql_connect($db_host,$db_user,$db_pass); if(!$link) die ('Could not connect to database: '.mysql_error()); mysql_select_db($db_name,$link); $query = "INSERT into `entries` (Hull,Location,Contact) VALUES ('" . $_POST['Hull'] . "','" . $_POST['Location'] . "','" . $_POST['Contact'] . "')"; mysql_query($query); mysql_close($link); } function set_error($field, $rule) { if ($this->is_xhr) { $this->error_list[$field] = $this->error_messages[$rule]; } else $this->error_list .= "<div class='error'>$field: " . $this->error_messages[$rule] . "</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/241941-check-for-duplicate-data/ Share on other sites More sharing options...
jasonc Posted July 14, 2011 Share Posted July 14, 2011 Maybe something like... Let me know if this works for you or if you need more help. I have added in some extra code to prevent MySQL injections... mysql_real_escape_string(...) function process() { // Connect to DB $db_host = "localhost"; $db_user = "XXX"; $db_pass = "XXX"; $db_name = "XXX"; $db_table = "entries"; $link = mysql_connect($db_host,$db_user,$db_pass); if(!$link) die ('Could not connect to database: '.mysql_error()); mysql_select_db($db_name,$link); // Check if Hull,Location,Contact already exists. $query = "SELECT * FROM `entries` WHERE `Hull` = '" . mysql_real_escape_string($_POST['Hull']) . "' AND `Location` = '" . mysql_real_escape_string($_POST['Location']) . "' AND `Contact` = '" . mysql_real_escape_string($_POST['Contact']) . "' LIMIT 1"; $checkExists = mysql_query($query); if(mysql_num_rows($checkExists )) { // Record already exists. ?>This record already exists.<? } else { // Does not exists so process form and add new unique entry. $msg = "<strong>Registration Details</strong>: <br /> <br /> \n\n"; foreach($this->fields as $key => $field) $msg .= "<strong style='color:#666;'>$key</strong>: $field \n <br />"; $to = '[email protected]'; $subject = "Subject Line"; $from = "[email protected]"; mail($to, $subject, $msg, "Content-type:text/html;charset=utf-8\r\nFrom: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); mail($_POST['Contact'], $subject, '<html> <div style="background-color: #FFF; color: #536d86; margin-left:10px; height:1000px; font-size:15px; margin:0px; padding:0px; font-family: Arial, Helvetica, sans-serif;"><a href="http://XXX.com/"><img border="0" src="http://XXX.com/thanks.jpg" /></a><br />'. "$msg </div> </html>", "Content-type:text/html;charset=utf-8\r\nFrom: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); $query = "INSERT into `entries` (Hull,Location,Contact) VALUES ('" . mysql_real_escape_string($_POST['Hull']) . "','" . mysql_real_escape_string($_POST['Location']) . "','" . mysql_real_escape_string($_POST['Contact']) . "')"; mysql_query($query); } // Close the DB connection. mysql_close($link); } Quote Link to comment https://forums.phpfreaks.com/topic/241941-check-for-duplicate-data/#findComment-1242562 Share on other sites More sharing options...
datoshway Posted July 14, 2011 Author Share Posted July 14, 2011 Thank you so much for checking that out and making some adjustments. The code works in preventing the entry but not echoing anything on the front end. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/241941-check-for-duplicate-data/#findComment-1242563 Share on other sites More sharing options...
jasonc Posted July 14, 2011 Share Posted July 14, 2011 PM me with a link so I can check the site Quote Link to comment https://forums.phpfreaks.com/topic/241941-check-for-duplicate-data/#findComment-1242565 Share on other sites More sharing options...
jasonc Posted July 14, 2011 Share Posted July 14, 2011 function process() { // Connect to DB $db_host = "localhost"; $db_user = "XXX"; $db_pass = "XXX"; $db_name = "XXX"; $db_table = "entries"; $link = mysql_connect($db_host,$db_user,$db_pass); if(!$link) die ('Could not connect to database: '.mysql_error()); mysql_select_db($db_name,$link); // Check if Hull,Location,Contact already exists. $query = "SELECT * FROM `entries` WHERE `Hull` = '" . mysql_real_escape_string($_POST['Hull']) . "' LIMIT 1"; $checkExists = mysql_query($query); if(mysql_num_rows($checkExists )) { // Record already exists. $msg = "<strong>This record already exists.</strong>: <br /> <br /> \n\n"; } else { // Does not exists so process form and add new unique entry. $msg = "<strong>Registration Details</strong>: <br /> <br /> \n\n"; foreach($this->fields as $key => $field) $msg .= "<strong style='color:#666;'>$key</strong>: $field \n <br />"; $to = '[email protected]'; $subject = "Subject Line"; $from = "[email protected]"; mail($to, $subject, $msg, "Content-type:text/html;charset=utf-8\r\nFrom: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); mail($_POST['Contact'], $subject, '<html> <div style="background-color: #FFF; color: #536d86; margin-left:10px; height:1000px; font-size:15px; margin:0px; padding:0px; font-family: Arial, Helvetica, sans-serif;"><a href="http://XXX.com/"><img border="0" src="http://XXX.com/thanks.jpg" /></a><br />'. "$msg </div> </html>", "Content-type:text/html;charset=utf-8\r\nFrom: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); $query = "INSERT into `entries` (Hull,Location,Contact) VALUES ('" . mysql_real_escape_string($_POST['Hull']) . "','" . mysql_real_escape_string($_POST['Location']) . "','" . mysql_real_escape_string($_POST['Contact']) . "')"; mysql_query($query); } // Close the DB connection. mysql_close($link); } Quote Link to comment https://forums.phpfreaks.com/topic/241941-check-for-duplicate-data/#findComment-1242567 Share on other sites More sharing options...
datoshway Posted July 14, 2011 Author Share Posted July 14, 2011 No go. Now it doesn't submit the form if it's duplicate but still takes me to the thank you page. Could it be because the thank you page is defined in the js file? Here is the js file code that handles that. $(".TTWForm").validator({effect:'labelMate'}).submit(function(e){ var form = $(this), checkRadioValidation = validateCheckRadio(); if(!e.isDefaultPrevented() && checkRadioValidation){ $.post(form.attr('action'), form.serialize(), function(data){ data = $.parseJSON(data); if(data.status == 'success'){ form.fadeOut('fast', function(){ window.location="thanks.php"; }); } else validator.invalidate(data.errors); }); } Quote Link to comment https://forums.phpfreaks.com/topic/241941-check-for-duplicate-data/#findComment-1242569 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.