使用foreach循环

时间:2018-01-27 17:21:35

标签: php mysql arrays foreach

我无法弄清楚为什么foreach循环迭代的次数多于元素数。

该函数传递两个关联数组:表单数据和特许经营数据。表单数据元素将转换为变量。我想遍历特许经营阵列(未知长度)并插入每个唯一特许经营ID的表格数据。每条记录将包含相同的表单数据(来自用户),但不同的数据包括franchise_id,franchisor_id和franchise_name字段。

正在发生的事情是,如果选择一个特许经营权,则会插入两个相同的记录。如果是两个,那么总共插入四个(两个副本)。但是,如果选择了三个,则会插入九行(三个选项中的三个)。这,尽管数组计数为3.

我已经无数次地修改了代码而没有成功。

这是功能:

public static function setLeadData($franchises, $formData)
{

    $first_name    = $formData[0]['value'];
    $last_name     = $formData[1]['value'];
    $telephone     = $formData[2]['value'];
    $email         = $formData[3]['value'];
    $zipcode       = $formData[4]['value'];
    $networth      = $formData[5]['value'];
    $liquidcapital = $formData[6]['value'];
    $message       = $formData[7]['value'];

    try
    {
        $db = static::getDB();

        $sql = "INSERT INTO leads_franchises SET
                franchise_id    = :franchise_id,
                franchisor_id   = :franchisor_id,
                franchise_name  = :franchise_name,
                first_name      = :first_name,
                last_name       = :last_name,
                telephone       = :telephone,
                email           = :email,
                zipcode         = :zipcode,
                networth        = :networth,
                liquidcapital   = :liquidcapital,
                message         = :message";
        $stmt = $db->prepare($sql);

        foreach($franchises as $franchise)
        {
            $parameters = [
                ':franchise_id'   => $franchise['franchise_id'],
                ':franchisor_id'  => $franchise['franchisor_id'],
                ':franchise_name' => $franchise['franchise_name'],
                ':first_name'     => $first_name,
                ':last_name'      => $last_name,
                ':telephone'      => $telephone,
                ':email'          => $email,
                ':zipcode'        => $zipcode,
                ':networth'       => $networth,
                ':liquidcapital'  => $liquidcapital,
                ':message'        => $message
            ];

            $result = $stmt->execute($parameters);
        }

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}

这是特许经营[],有三个选择:

[0] => Array
    (
        [franchise_id] => 2
        [franchisor_id] => 3
        [franchise_name] => Franchise One
        [logo] => image2.jpg
        [logo_thumb] => thumb_image2.jpg
        [min_capital] => 200000
        [description] => description
        [website] => example2.com
        [inquiry_email] => info@gmail.com
        [created_at] => 2018-01-17 13:05:51
        [updated_at] => 2018-01-25 17:09:51
    )

[1] => Array
    (
        [franchise_id] => 3
        [franchisor_id] => 3
        [franchise_name] => Franchise Two
        [logo] => image3.jpg
        [logo_thumb] => thumb_image3.jpg
        [min_capital] => 100000
        [description] => description
        [website] => example3.com
        [inquiry_email] => info@gmail.com
        [created_at] => 2018-01-21 18:03:39
        [updated_at] => 2018-01-25 17:10:02
    )

[2] => Array
    (
        [franchise_id] => 4
        [franchisor_id] => 3
        [franchise_name] => Franchise Three
        [logo] => image4.jpg
        [logo_thumb] => thumb_image4.jpg
        [min_capital] => 100000
        [description] => description
        [website] => example4.com
        [inquiry_email] => info@gmail.com
        [created_at] => 2018-01-25 10:25:52
        [updated_at] => 2018-01-25 17:58:08
    )

如果有人能在代码或我的方法中看到我的错误,我会非常感激。

2 个答案:

答案 0 :(得分:1)

添加:

 $stmt->reset();

到此行之后的循环结束:

$result = $stmt->execute($parameters);

答案 1 :(得分:0)

我无法从问题中判断OP是在询问500错误还是重复行。如果它是重复的行,我不得不问“franchise_id是你的主键的唯一组成部分,还是你有一个复合主键,可能包括其他字段(如franchisor_id)?”。如果是500错误,我会说要远离“INSERT INTO tablename SET”。我不知道你正在使用的MySQL版本,但“SET”更适合UPDATE语句。 INSERT应该有“INSERT INTO tablename(col1,col2,col3)VALUES(int_1,'char2''varchar3')或类似的东西。你需要使用'?'值的占位符或绑定一些要使用的变量。请参阅https://www.w3schools.com/php/php_mysql_prepared_statements.asp