Miko Posted October 22, 2009 Share Posted October 22, 2009 Hello, I'm creating a script that should check in my MySQL DB if a value is allready present or not, if it is not present it should insert it into the database. Was thinking of doing like this: SELECT * FROM dates WHERE date = $date AND depot = $depot then add an IF statement if($sql_date != $date){ "insert into db statement and code } Seems that it isn't a correct way to do it? Maybe work with array? Quote Link to comment https://forums.phpfreaks.com/topic/178614-insert-if-not-in-db/ Share on other sites More sharing options...
Alex Posted October 22, 2009 Share Posted October 22, 2009 Your conditional would look more like this: $result = mysql_query("Query to select what you're looking for.."); if(!mysql_num_rows($result)) { //Doesn't exist, insert it. } Quote Link to comment https://forums.phpfreaks.com/topic/178614-insert-if-not-in-db/#findComment-942047 Share on other sites More sharing options...
dreamlove Posted October 22, 2009 Share Posted October 22, 2009 $result = mysql_query("Query to select what you're looking for.."); $row = mysql_fetch_row($result); if(empty($row)) { //Doesn't exist, insert it. } Quote Link to comment https://forums.phpfreaks.com/topic/178614-insert-if-not-in-db/#findComment-942062 Share on other sites More sharing options...
dreamlove Posted October 22, 2009 Share Posted October 22, 2009 perhaps you can set 'date - depot ' as a unique key in your database. THen , no need to `select`, just `INSERT` directly. Quote Link to comment https://forums.phpfreaks.com/topic/178614-insert-if-not-in-db/#findComment-942069 Share on other sites More sharing options...
Miko Posted October 22, 2009 Author Share Posted October 22, 2009 thanks both of the methods are working I could used directly the INSERT query, but this would give errors and our server is logging every error, so I don't like it if my errorlogfile is getting bigger Quote Link to comment https://forums.phpfreaks.com/topic/178614-insert-if-not-in-db/#findComment-942071 Share on other sites More sharing options...
PFMaBiSmAd Posted October 22, 2009 Share Posted October 22, 2009 ... but this would give errors ... Not necessarily - INSERT IGNORE INTO tbl_name .... Quote Link to comment https://forums.phpfreaks.com/topic/178614-insert-if-not-in-db/#findComment-942076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.