xhelle Posted November 4, 2009 Share Posted November 4, 2009 Hi everyone... Can somebody help me to solve my problem. I have this data needed to be insert into database, but first I must check if the mobile number is already exist on may table, if not it will continue to insert. What is the correct query for this problem...Hope somebody help me... Here's my old query : insert into tgt_table select * from src_table where mobilenum NOT IN (select distinct mobilenum from tgt_table) Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/180245-how-to-check-with-php-if-a-table-is-empty/ Share on other sites More sharing options...
Mchl Posted November 4, 2009 Share Posted November 4, 2009 INSERT ... ON DUPLICATE KEY UPDATE Quote Link to comment https://forums.phpfreaks.com/topic/180245-how-to-check-with-php-if-a-table-is-empty/#findComment-950818 Share on other sites More sharing options...
xhelle Posted November 4, 2009 Author Share Posted November 4, 2009 Hi Mchl, Thanks for the reply but I dont think the insert...on duplicate key update will do...Here's my code: $sqlmobile = "SELECT mobile_num FROM user"; $query = mysql_query($sqlmobile); $data = mysql_fetch_array($query); if ($data["mobile_num"] == $temp[1]) { echo "Mobile Number already exist!"; } But its still insert into database during i run my catcher...the example i use is already exist on user table, therefore it should not insert...or it should send an error message...Hope you can help me to solve my problem... Thanks again.... Quote Link to comment https://forums.phpfreaks.com/topic/180245-how-to-check-with-php-if-a-table-is-empty/#findComment-950868 Share on other sites More sharing options...
Mchl Posted November 4, 2009 Share Posted November 4, 2009 You can do this with INSERT ON DUPLICATE KEY UPDATE. If you want mobile_num to be unique in the table, then you should create a UNIQUE index on this column. Then you run a query like this. INSERT INTO table (mobile_num) VALUES ('$mobile_num') ON DUPLICATE KEY UPDATE SET mobile_num = mobile_num Quote Link to comment https://forums.phpfreaks.com/topic/180245-how-to-check-with-php-if-a-table-is-empty/#findComment-950877 Share on other sites More sharing options...
xhelle Posted November 4, 2009 Author Share Posted November 4, 2009 Hi again Mchl, Thanks for your help. I already solve my problem...^^all i need to do is add the WHERE mobile_num = '".$temp[1].'" //select mobile number in database.... $sqlmobile = "SELECT mobile_num FROM user WHERE mobile_num = '".$temp[1]."'"; $query = mysql_query($sqlmobile); $data = mysql_fetch_array($query); if($data["mobile_num"] == $temp[1]) { echo "Mobile Number already exist!"; } Thanks again^^ Quote Link to comment https://forums.phpfreaks.com/topic/180245-how-to-check-with-php-if-a-table-is-empty/#findComment-950882 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.