1

I am having problems with this code:

<?php
$new_value = 'testing';

$con = mysql_connect("localhost","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

mysql_query("INSERT INTO myDB (myField) VALUES ('$new_value')");

mysql_close($con);

?>

2 issues are happening:

1st - 2 records are being inserted instead of 1

2nd - $new_value is not changed and it's creating multiple instances with the same $new_value value when I only want 1.

7
  • Where in your code is $new_value supposed to change? Or do you mean that you insert the same value into the DB over and over again? That is correct because I don't see you specifying anything about unique values or nothing. Commented Mar 30, 2012 at 10:37
  • Correct I inserted the same value into the DB over and over again and only want one Commented Mar 30, 2012 at 10:38
  • 1
    Are you sure you are not executing this code segment twice(e.g. include/require)? It seems impossible for two records to be inserted instead of single one.... Commented Mar 30, 2012 at 10:39
  • Is this the whole code? or maybe just a snippet and this code is somewhere in a loop? Commented Mar 30, 2012 at 10:40
  • I'm sure....because I just commented the code above and nothing was inserted into the db Commented Mar 30, 2012 at 10:44

3 Answers 3

3

I think this is something similar to what you are after, after reading your comment:

INSERT IGNORE INTO tablename (fields)
VALUES(values);

Use INSERT IGNORE rather than INSERT. If a record doesn't duplicate an existing record, MySQL inserts it as usual. If the record is a duplicate, the IGNORE keyword tells MySQL to discard it silently without generating an error.

edit Or as Vytautas points out; if you want to replace the value use REPLACE in stead of INSERT

Sign up to request clarification or add additional context in comments.

1 Comment

INSERT IGNORE is still adding it twice
1

Why dont you try using transactions? It helps you to avoid some of insert/update errors:

 try
    {

        // Start TRANSACTION
        mysqli_autocommit($con, false);

        mysqli_query('INSERT INTO table SET field = "'.$fieldValue.'"');

        // If there is no error then COMMIT
        mysqli_commit($con);      

    }
    catch(Exception $e)
    {

        // If there is an error, then rollback any INSERTS or UPDATES in the TRY block
        mysqli_rollback($con);

        $theError = $e->getMessage();
    }

Its highly recommended that you use Try/Catch blocks in your code when working with databases.

Comments

0

2nd - $new_value is not changed and it's creating multiple instances with the same $new_value value when I only want 1.

Where is $new_value changed ? How is it changed ? Are you sure you are not pressing the browser refresh button many times ?

1 Comment

There will only be 1 instance of $new_value so, say $new_value = 'Value1'; ... The code is adding itself twice in the db table, I want to avoid duplicates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.