0

I have made the following script to check if an article exists in another table, if it does it is supposed to echo that the article exists, if it doesnt it then adds the article id and article name to another table. The problem I am having is if I clear the workshoptabs table and run the update script it creates all the article id / names, but if I add a new article to the dbel6_content table and then run the update script again, it doesn't add the new articles to the workshoptabs. Also if I delete a record from workshoptabs and run the script it doesnt re add it, but if I clear the whole table it re imports everything then.

$resultB = mysql_query("SELECT * FROM dbel6_content WHERE catid='12'") or die(mysql_error());  
while($rowB = mysql_fetch_array( $resultB )) {
  $articleid = $rowB['id'];
  $articlename = $rowB['title'];

  $resultA = mysql_query("SELECT * FROM workshoptabs WHERE articleid='$articleid'") or die(mysql_error());  
  while($rowA = mysql_fetch_array( $resultA )) {
    $articleexists = $rowA['articleid'];
    echo $articleexists.' Exists';
  }

  if (empty($articleexists)){
    mysql_query("INSERT INTO workshoptabs (articleid, articlename) VALUES ('$articleid', '$articlename')") or die(mysql_error()); 
    echo 'Added Articles : '.$articleid.'-';    
  }
}

If I run the script I get : 16 Exists17 Exists20 Exists24 Exists25 Exists26 Exists27 Exists34 Exists

If I wipe the workshoptabs table and reimport everything I get : Added Articles : 16-Added Articles : 17-Added Articles : 18-Added Articles : 19-Added Articles : 20-Added Articles : 21-Added Articles : 22-Added Articles : 23-Added Articles : 24-Added Articles : 25-Added Articles : 26-Added Articles : 27-Added Articles : 34-

But if for example I now delete article 21 and run the script again it doesnt add it.

s_ha_dum
  • 2,840
  • 2
  • 18
  • 23
Iain Simpson
  • 8,011
  • 13
  • 47
  • 66
  • Possible that you need to empty $articleexists before checking each entry. It looks like it will never be empty again after finding its first existing record. – showdev Jan 21 '13 at 20:02
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Joseph Silber Jan 21 '13 at 20:03
  • ^^^^as true as it is, its getting a little tedious for regular S.O users to see it 10 times a day. –  Jan 21 '13 at 20:05
  • Thats all very well for you to say if you know PDO and have the time to learn it, I dont have the spare time to sit down with a books and re learnt it all from scratch. – Iain Simpson Jan 21 '13 at 20:09
  • @Dagon - I wish I wouldn't need to, but this is just ridiculous. I see hundreds of new questions being posted here *every single day* using the `mysql_*` functions. People looking for references consider SO to be an authoritative resource, and if they see discussions involving these functions without that notice, they'll think it's OK to use them. No matter how much time you (the OP) have, this is a must. Drop everything you're doing, and learn PDO/MySQLi. – Joseph Silber Jan 21 '13 at 20:10
  • @Dagon - If just *seeing* this 10 times a day is tedious, try *posting* it 10+ times a day. – Joseph Silber Jan 21 '13 at 20:12
  • @IainSimpson You may feel like you don't have time to learn the new MySQL mechanisms but sometime soon you won't have a choice. The old functions are deprecated and when they are removed your code will stop working. Then you are going to have to re-write it anyway. – s_ha_dum Jan 21 '13 at 20:18

1 Answers1

1

I suggest using the row count of your query to determine if a record exists:

<?php

  $resultB = mysql_query("SELECT * FROM dbel6_content WHERE catid='12'") or die(mysql_error());

  while($rowB = mysql_fetch_array( $resultB )) {

    $articleid = $rowB['id'];
    $articlename = $rowB['title'];

    $resultA = mysql_query("SELECT * FROM workshoptabs WHERE articleid='$articleid'") or die(mysql_error());

    if (mysql_num_rows($resultA)>0) {
      // exists
      echo $articleid.' Exists';
    } else {
      // doesnt exist
      mysql_query("INSERT INTO workshoptabs (articleid, articlename) VALUES ('$articleid', '$articlename')") or die(mysql_error()); 
      echo 'Added Articles : '.$articleid.'-';  
    }

  }

?>

Consider looking into PDO or MySQLi, as mentioned by Joseph.

showdev
  • 28,454
  • 37
  • 55
  • 73
  • Thanks, this adds a load of blank records, then if I run it again adds a load more blank records :-( – Iain Simpson Jan 21 '13 at 20:16
  • I originally forgot to assign values to articleid and articlename. I added them in an edit. Does that help? – showdev Jan 21 '13 at 20:17
  • Thanks very much for your help, that works perfectly. Will try and learn mysqli as PDO looks like a complete nightmare where is mysqli is half familiar looking :-) . – Iain Simpson Jan 21 '13 at 20:19
  • I understand the reluctance, but it's not so bad. I've always used mysql statements and I'm switching to PDO on current projects. In my opinion, the advantages outweigh the learning curve difficulties. You might even find it makes things easier on you! – showdev Jan 21 '13 at 20:21