当项目不存在in_array返回true [英] in_array returning true when item does not exist

查看:162
本文介绍了当项目不存在in_array返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,让我的用户根据需要编辑信息。作为这种形式的一部分,用户可以插入最多5种类型的证据。所有证明被存储在一个单独的表

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)

我也有以下的code更新或插入证明的项目。它通过循环的每个字段,并检查数组中的当前选择的项目是不是空的。如果为true,它会检查,看看是否$ i是存储所有号码为选定的禁令标识的数组中。

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.

该阵列看起来像这样的BANID 237:阵列([0] => [1] => 1)

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);
        }
    }           
}

不过,我的问题是,与上述阵列,行如果(!in_array($ I,$ proofnumberlist))终止返回false,因此不会进入如果语句时,$ I = 0。

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.

它适用于所有其他值(其中$ i = 1,...)等,但不是在$ I = 0。

It works for all other values (where $i = 1, ...) and so forth but not when $i = 0.

感谢您的阅读,我AP preciate您可以给我任何帮助。

Thank you for reading and I appreciate any help you may be able to give me.

乔恩。

推荐答案

在另一方面,你可以回避整个 in_array 通过检查 INSERT ...对重复密钥更新

On the other hand, you could sidestep the entire in_array check by using INSERT ... ON DUPLICATE KEY UPDATE.

这将使你的code更清楚一点。请参见 http://dev.mysql.com/doc/ refman / 5.0 / EN /插入式-duplicate.html

This would make your code a bit clearer. See http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

这篇关于当项目不存在in_array返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆