Reputation: 755
I have a form which allows my user to edit the information as required. As part of this form, the user can insert up to 5 types of proof. All proof is stored in a seperate table:
Table: Proof
proofid - primary key
banid - foreign key -> links to banlist table which stores all other information
proof - the link to which contains the proof
type - the type of proof (image, demo, league, ...)
number - the index number of the proof (0 up to 4)
I also have the following code to update or insert the item of proof. It loops through each field, and checks if the current selected item in the array is not empty. If true, it checks to see if $i is within the array which stores all the numbers for the selected ban id.
The array looks like this for banid 237: Array ( [0] => [1] => 1 )
This in effect says that the first proof field is empty, but the second is not and that a record exists in the table.
for($i = 0; $i < 5; $i++)
{
$proof = $checkprooflist[$i];
$prooftype = $checkprooftypelist[$i];
if (!empty($proof))
{
if(!in_array($i, $proofnumberlist))
{
$query2 = "INSERT INTO proof (banid, proof, type, number) VALUE ($id, '$proof', '$prooftype', $i)";
$result2 = mysql_query($query2);
}
else
{
$query2 = "UPDATE proof SET proof = '$proof', type = '$prooftype' WHERE number = $i AND banid = $id";
$result2 = mysql_query($query2);
}
}
}
The problem I have however is that with the above array, the line if(!in_array($i, $proofnumberlist))
is returning false and thus not entering the if statement when $i = 0.
It works for all other values (where $i = 1, ...) and so forth but not when $i = 0.
Thank you for reading and I appreciate any help you may be able to give me.
Jon.
Upvotes: 1
Views: 1162
Reputation: 1
On the other hand, you could sidestep the entire in_array
check by using INSERT ... ON DUPLICATE KEY UPDATE
.
This would make your code a bit clearer. See http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Upvotes: 2
Reputation: 3696
There is no 0 in this array:
Array ( [0] => [1] => 1 )
The element at index 0 is null, not 0. Where did you get this array in the first place?
Upvotes: 1
Reputation: 173562
You have to use the $strict
parameter of in_array()
, it's the third one (default = false).
Due to type juggling, an empty string can be equal to 0, etc. The $strict
parameter makes sure to also test the types.
if (!in_array($i, $proofnumberlist, true))
Upvotes: 3