0

I am trying to code where if there is the same data in the mysql table as the on you are about to insert, then do nothing, but when if there isn't then insert into the table.

This is my code so far:

    for($i=0;$i<count($TitleArray);$i++){
        $sql = "SELECT * FROM ytable WHERE Title *LIKE the one I am about to insert"
if (Data about to insert already exists in table){
        "do nothing";
        }*
else{
        $sql = "INSERT INTO ytable (Title, Pubdate, Link, Tweeted, Created) 
                VALUES ('$TitleArray[$i]', '$PubdateArray[$i]', '$LinkArray[$i]', NULL, $created)";      
    mysql_query($sql,$db_con);
    }

I need help writing the code from the first * to the next *. Please help

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Masayuki Tonoki
  • 167
  • 2
  • 13
  • Refer this code. http://stackoverflow.com/questions/7719039/check-for-duplicates-before-inserting – Talk2Nit Aug 19 '15 at 06:56
  • @Nitin I have looked at that and I do not know if it would work because I need to compare it to an array instead of a single string. Do you have a way to compare to an array – Masayuki Tonoki Aug 19 '15 at 07:00

3 Answers3

0

Set the field "Title" as a unique key, and then simply use INSERT IGNORE

for($i=0;$i<count($TitleArray);$i++){
    $sql = "INSERT IGNORE INTO ytable (Title, Pubdate, Link, Tweeted, Created) VALUES ('$TitleArray[$i]', '$PubdateArray[$i]', '$LinkArray[$i]', NULL, $created)";
    mysql_query($sql,$db_con);
}

The sentence INSERT IGNORE will check if there is any problem with repeated keys. If there's any, it will not insert anything. If there is no problem, then will insert your data.

Edit

If you cant modify your table, as you said in the comment, then use this code. Your function is mysql_num_rows (You'd better use mysqli instead of mysql)

for($i=0;$i<count($TitleArray);$i++){
    $sql = 'SELECT * FROM ytable WHERE Title LIKE "the one I am about to insert"';
    $res = mysql_query($sql);
    if (empty(mysql_num_rows($res))){
        $sql_insert = "INSERT INTO ytable (Title, Pubdate, Link, Tweeted, Created) VALUES ('$TitleArray[$i]', '$PubdateArray[$i]', '$LinkArray[$i]', NULL, $created)";      
        $res_insert = mysql_query($sql_insert,$db_con);
    }
}
SpongePablo
  • 870
  • 10
  • 24
0

Try this

INSERT INTO ytable (Title, Pubdate, Link, Tweeted, Created) 
SELECT $TitleArray[$i], $PubdateArray[$i], $LinkArray[$i], NULL, $created FROM ytable
WHERE NOT EXISTS (
    SELECT title FROM ytable WHERE  Title='abcd'
) LIMIT 1;
Harshit
  • 5,147
  • 9
  • 46
  • 93
0

I think this will work for you.

for($i=0;$i<count($TitleArray);$i++){
    $sql = "SELECT * FROM ytable WHERE Title='".$TitleArray[$i]."' AND Pubdate='".$PubdateArray[$i]."' AND Link='".$LinkArray[$i]."' AND     Tweeted='NULL' AND Created='".$created."'";
    $dataAvailable = mysql_query($sql,$db_con);

    if (mysql_num_rows($sql) > 0) {
        "do nothing";
    }
    else{
        $sql = "INSERT INTO ytable (Title, Pubdate, Link, Tweeted, Created) VALUES ('$TitleArray[$i]', '$PubdateArray[$i]', '$LinkArray[$i]', NULL, $created)";      
        mysql_query($sql,$db_con);
    }

Check this.

Talk2Nit
  • 1,115
  • 3
  • 22
  • 38