Reputation: 27
I'm wrapping up my website, but I've an issue with breaking if statement inside foreach statement, I want when a student try to pick a job that he/she already had chosen, the if() fail and break from the WHOLE foreach() loop, this is my code,the break inside the if() only break out the if(), not the whole foreach please please help me. thank you
foreach($_POST['JobId'] AS $i){
$sqlCheckingBeofrAdding ="SELECT * FROM JobsLists WHERE JobId = '".$i."' AND SSU = '".$SSU."' ";
$rs = mysqli_query($dbCIE ,$sqlCheckingBeofrAdding);
$row1 = mysqli_fetch_assoc($rs);
if($row1 > 0){
echo "You Already Have this Job In your List";
break;
}
//insert into junction table..
$sqlUpdate = "UPDATE Jobs SET NoStudent=NoStudent-1 WHERE JobId = '" . $i . "'";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));
$sqlInsert ="INSERT INTO JobsLists(`JobID` , `SSU`) VALUES(".$i.",'".$SSU."' )";
$MyQuery= mysqli_query($dbCIE, $sqlInsert) or die(mysqli_error($dbCIE));
}
Upvotes: 2
Views: 176
Reputation: 910
Break does not break ifs. This is how the language works.
Ensure you have a debug line before and after the if instead of simple MySQL operations. This seems to be a problem with the code.
For example, add some echo lines before and after the if and you'll have a better idea on what's going on.
The code itself seems to have a lot of problems: 1-You don't need to retrieve all fields (select *) in order to see if at least one entry exists. This is a misuse of the database layer.
2-In case you need to check if one entry exists, add LIMIT 1 to your SQL query. This will avoid MySQL to scan the entire table in case the query is not using any unique key, which is very likely.
3-mysqli_fetch_assoc returns an associative array, but then you compare the array against "> 0". Would you confirm that Array() > 0 ?
4-You seem to be using camelCased variables, but then all of the sudden you have $MyQuery, when it should be $myQuery to match the convention you're using.
5-You're storing the result of the query, in a variable named $MyQuery. This is a bad practice, as the variable name should describe what it contains. Therefore you'll want to call it $myResult or $rs as you did in the first query.
6-None of the variables used in the SQL statements are escaped.
Double check your code using the rubber duck technique and I'm sure you'll find the issue is not related to the if/break; but to something else. Not to offend, but this code seems very immature.
Upvotes: 0
Reputation: 529
The php manual says:
break ends execution of the current for, foreach, while, do-while or switch structure.
I do not think your break
is breaking out of the if
. I suspect a different problem with your code.
Upvotes: 0
Reputation: 16117
If your query is only return the single student record than you can use return
and this will not execute the further code inside the loop.
If your query returns multiple students record than you can use continue
For point 1:
if($row1 > 0){
echo "You Already Have this Job In your List";
return;
}
For point 2:
if(condition){
echo "You Already Have this Job In your List";
continue;
}
Upvotes: 1
Reputation: 514
The break statement in PHP accepts a numeric argument.
Try changing your break;
to break 2;
or use a return;
Upvotes: 0
Reputation: 543
You can use break;
OR return;
(if this is a function.) Let me know if you need code.
Upvotes: 0