如何判斷資料已存在

edited 八月 2014 in PHP新手區
請問我的資料表欄位有一欄是 companyid 另一欄是 shopid,我利用while去掃,假設我companyid=1 我資料表裡面共有四筆資料的話,那我如何去判斷完再決定如何insert ,我有試過我利用while去掃,他掃出來有四筆資料,但是這四筆都沒有我要的資料,我該在哪邊下insert ,以免發生重複四次的情況發生

評論

  • 		$wc="1";
    		$no=$_POST['check_shop'];
    		$shop =$_POST['shopsn'];
    		$result = mysql_query("select * from wc_save_company where welfarecommittee_ID = '$wc' ");
    		$rs_company = mysql_fetch_row($result);
    		//echo $rs_company[1];
    		$result1 = mysql_query("select * from shop_company where company_ID = '$rs_company[1]'");		
    		$rs_shop = mysql_fetch_row($result1);
    		foreach($no as $id){
    			
    			if($rs_shop[1] != $id){
    				echo "$id";
    				echo "不重複";
    			}else{
    				echo "重複";
    			}
    		}
    
    這是我的程式碼,他都只抓到 $rs_shop[1] 的第一筆資料
  • $wc = "1";
    $no = $_POST['check_shop'];
    $shop = $_POST['shopsn'];
    $result = mysql_query("select * from wc_save_company where welfarecommittee_ID = '$wc' ");
    $rs_company = mysql_fetch_row($result);
    //echo $rs_company[1];
    $result1 = mysql_query("select * from shop_company where company_ID = '$rs_company[1]'");
    while ($rs_shop = mysql_fetch_row($result1)) {
        foreach ($no as $id) {
            if ($rs_shop[1] != $id) {
                echo "$id";
                echo "不重複";
            } else {
                echo "重複";
            }
        }
    }
    
  • edited 八月 2014
    $wc = 1;
    $no = $_POST['check_shop'];
    $shop = $_POST['shopsn'];
    $str1 = implode (',',$no );
    $str2 = implode (',',$shop );
    $sql ="
    select * from shop_company a left join  wc_save_company b on a. company_ID = b. company_ID  where company_ID in ($str1) and shopid in ( $str2) and  b.welfarecommittee_ID = $wc " ;
    ......
    /*
    sql 出的結果有資料,表示資料庫根據判斷條件($wc,$no ,$shop ) 已有這筆資料
    那就不用insert 了
    */
    
Sign In or Register to comment.