Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How do I Check for duplicate entries in mysql

Avatar of peter-cooper
peter-cooper asked on
PHP
15 Comments1 Solution286 ViewsLast Modified:
Hello
I have a script that inserts items into mysql which works fine. However, what I need to do is check for duplicate entries and if found, issue alert and halt. I have coded some php but cannot seem to incorporate into my existing script.

I would be grateful if someone could help with this. Many thanks.

Exisitng code which works fine

<?php session_start(); ?>

    <?php

    $con = mysql_connect("localhost","root","");
    if(!$con) { die('Could not connect: ' . mysql_error()); }
    mysql_select_db("sample", $con);
    $dept = $_POST['dept'];
    $custref = $_POST['sub'];
		
		
    $query = "SELECT * FROM boxes WHERE department = '".$dept."' AND status = 1 AND custref = '".$custref."'";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result) or die(mysql_error());
    $r = $row['custref'];
		
    $str =  json_encode($r);
    echo trim($str, '"');

?>

New code to be i was thinking of using

<?php session_start();  ?>
    <?php

    $con = mysql_connect("localhost","root","");
    if(!$con) { die('Could not connect: ' . mysql_error()); }
    mysql_select_db("sample", $con);
		
    $custref = $_POST['sub'];
		
    $result = mysql_query("SELECT * FROM boxes WHERE custref = '".$custref."'");
    $found = mysql_num_rows($result);
		
     if ($found > 0)
		{
			
			echo "true";
			
		} else {
			
			echo "false";
			
		}

     ?>

jQuery code

$(function() {
				
$('#srcsubmit').click(function(e) {
e.preventDefault();
				 			 
if ($('#srcBox').val() == '') {

        notif({
        type: "error",
        msg: "<b>ERROR:<br /><br />You must enter a search term</b><p>Click anywhere to close</p>",
        height: 99,
        multiline: true,
        position: "middle,center",
        fade: true,
        timeout: 3000

        });
        return false;
        }
				 
				 
        $( "#submit" ).prop( "disabled", true );
        $( "#submit2" ).prop( "disabled", true );
        $( "#submit3" ).prop( "disabled", true );

        var value = $('#srcBox').val();
        var dept = '<?php echo $_GET['dept']; ?>';
				 
        var qString = 'sub=' + encodeURIComponent(value) + '&dept=' +encodeURIComponent(dept);

        $.post('sub_db_handler.php', qString, processResponse);
         });

   function processResponse(data) {
    
        $('#srcBoxRslt').val(data);

   };
		
});
		
	
</script>