Jump to content

Check for duplicate data


datoshway

Recommended Posts

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 = 'xxx@xx.com';
         $subject = "Subject Line";
         $from = "xx@xx.com";
         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>";
    }

 

Link to comment
Share on other sites

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 = 'xxx@xx.com';
         $subject = "Subject Line";
         $from = "xx@xx.com";
         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);
    }

Link to comment
Share on other sites

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 = 'xxx@xx.com';
         $subject = "Subject Line";
         $from = "xx@xx.com";
         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);
    }

Link to comment
Share on other sites

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);

            });
        }

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.