如何在插入数据库时​​检查条件

时间:2017-02-03 08:55:45

标签: php php-5.6

我有一个多维数组,这个值我想插入数据库所以我这样做,它工作正常,但我需要一个逻辑像之前插入数据库中的值,我想检查两个条件,如< strong> badge_number和pf_month 已经存在意味着我想省略剩余的行我要插入的行怎么办?

删除while循环,如果条件表示工作正常,但现在不能正常工作

更新代码

foreach($userDetails as $user){
    $sql = mysql_query("INSERT IGNORE INTO loan_history(pf_month,badge_number,first_name,pf_amount,loan_amount)VALUES('".$user['month']."','".$user['badgeNumber']."','".$user['name']."','".$user['pfAmount']."','".$user['loanAmount']."')");
}

$userDetails=
Array
(
    [0] => Array
        (
            [id] => 1
            [month] => 01-2017
            [badgeNumber] => 4563215
            [name] => Kanniyappan
            [pfAmount] => 5000
            [loanAmount] => 9800
        )

    [1] => Array
        (
            [id] => 2
            [month] => 01-2017
            [badgeNumber] => 1453215
            [name] => Mahesh
            [pfAmount] => 6000
            [loanAmount] => 22000
        )

    [2] => Array
        (
            [id] => 3
            [month] => 01-2017
            [badgeNumber] => 7783215
            [name] => Asif
            [pfAmount] => 30000
            [loanAmount] => 92000
        )

    [3] => Array
        (
            [id] => 4
            [month] => 01-2017
            [badgeNumber] => 5632154
            [name] => Avinash
            [pfAmount] => 100000
            [loanAmount] => 650000
        )

    [4] => Array
        (
            [id] => 5
            [month] => 01-2017
            [badgeNumber] => 7832152
            [name] => Rohit
            [pfAmount] => 4100
            [loanAmount] => 5052
        )

    [5] => Array
        (
            [id] => 6
            [month] => 01-2017
            [badgeNumber] => 7754544
            [name] => Rajesh
            [pfAmount] => 64443
            [loanAmount] => 77454
        )

    [6] => Array
        (
            [id] => 7
            [month] => 01-2017
            [badgeNumber] => 9686786
            [name] => Suresh
            [pfAmount] => 98747
            [loanAmount] => 54451
        )

    [7] => Array
        (
            [id] => 8
            [month] => 01-2017
            [badgeNumber] => 9686786
            [name] => Mohan
            [pfAmount] => 85565
            [loanAmount] => 74754
        )

)

       foreach($userDetails as $user){
		$check = mysql_query("SELECT * FROM loan_history");
			while($loan = mysql_fetch_assoc($check)){
				$avail_month = $loan['pf_month'];
				$avail_badge = $loan['badge_number'];
				
				if($avail_month != $user['month'] && $avail_badge != $user['badgeNumber']){
					$sql = mysql_query("INSERT INTO loan_history(pf_month,badge_number,first_name,pf_amount,loan_amount)VALUES('".$user['month']."','".$user['badgeNumber']."','".$user['name']."','".$user['pfAmount']."','".$user['loanAmount']."')");
					if($sql){
						echo "success";
					}else{
						echo "Error".mysql_error();
					}
				}else{
					echo "Error".mysql_error();
				}
				
			}
		
	}

1 个答案:

答案 0 :(得分:0)

您可以使用INSERT IGNORE

INSERT IGNORE :如果记录没有复制现有记录,MySQL会像往常一样插入它。如果记录是重复的,则IGNORE关键字告诉MySQL以静默方式丢弃它而不会产生错误。

INSERT IGNORE INTO person_tbl (last_name, first_name) VALUES( 'Jay','Thomas');