Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 插入到表中的重复数据集_Php_Mysql_Arrays_Loops - Fatal编程技术网

Php 插入到表中的重复数据集

Php 插入到表中的重复数据集,php,mysql,arrays,loops,Php,Mysql,Arrays,Loops,为什么这段代码会插入重复的数据集 下面是传递给函数的$特许经营数组(2=count($特许经营)): ( [0] => Array ( [franchise_id] => 3 [franchisor_id] => 3 [franchise_name] => Fitness Freaks ) [1] => Array (

为什么这段代码会插入重复的数据集

下面是传递给函数的
$特许经营
数组(
2=count($特许经营)
):

(
    [0] => Array
        (
            [franchise_id] => 3
            [franchisor_id] => 3
            [franchise_name] => Fitness Freaks
        )

    [1] => Array
        (
            [franchise_id] => 4
            [franchisor_id] => 3
            [franchise_name] => Gyms Galore
        )

)
下面是使用
for循环
将数据插入表中的函数:

public static function setLeadData($franchises)
{
   try
   {
        $db = static::getDB();

        $sql = "INSERT INTO leads_franchises SET
               franchise_id    = :franchise_id,
               franchisor_id   = :franchisor_id,
               franchise_name  = :franchise_name";
        $stmt = $db->prepare($sql);

        for($i = 0; $i < count($franchises); $i++)
        {
            $result = $stmt->execute([
                ':franchise_id'   => $franchises[$i]['franchise_id'],
                ':franchisor_id'  => $franchises[$i]['franchisor_id'],
                ':franchise_name' => $franchises[$i]['franchise_name']
            ]);
        }

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
下面是在
foreach循环中包含MySQL组件的函数:

public static function setLeadData($franchises)
{
   try
   {
        $db = static::getDB();

        foreach($franchises as $franchise)
        {
            $sql = "INSERT INTO leads_franchises SET
                   franchise_id    = :franchise_id,
                   franchisor_id   = :franchisor_id,
                   franchise_name  = :franchise_name";
            $stmt = $db->prepare($sql);
            $result = $stmt->execute([
                ':franchise_id'   => $franchise['franchise_id'],
                ':franchisor_id'  => $franchise['franchisor_id'],
                ':franchise_name' => $franchise['franchise_name']
            ]);
        }

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
public static function setLeadData($franchises)
{
   try
   {
        $db = static::getDB();

        $sql = "INSERT INTO leads_franchises SET
               franchise_id    = :franchise_id,
               franchisor_id   = :franchisor_id,
               franchise_name  = :franchise_name";

        foreach($franchises as $franchise)
        {                
            $stmt = $db->prepare($sql);
            $result = $stmt->execute([
                ':franchise_id'   => $franchise['franchise_id'],
                ':franchisor_id'  => $franchise['franchisor_id'],
                ':franchise_name' => $franchise['franchise_name']
            ]);
        }

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
public static function setLeadData($franchises)
{

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

        $sql = "INSERT INTO leads_franchises (franchise_id, franchisor_id, franchise_name)
                VALUES (:franchise_id, :franchisor_id, :franchise_name)";

        $stmt = $db->prepare($sql);

        foreach($franchises as $franchise)
        {
            $stmt->bindParam(':franchise_id', $franchise['franchise_id']);
            $stmt->bindParam(':franchisor_id', $franchise['franchisor_id']);
            $stmt->bindParam(':franchise_name', $franchise['franchise_name']);

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

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
下面是在
foreach循环中使用
stmt=$db->prepare($sql)
的函数:

public static function setLeadData($franchises)
{
   try
   {
        $db = static::getDB();

        foreach($franchises as $franchise)
        {
            $sql = "INSERT INTO leads_franchises SET
                   franchise_id    = :franchise_id,
                   franchisor_id   = :franchisor_id,
                   franchise_name  = :franchise_name";
            $stmt = $db->prepare($sql);
            $result = $stmt->execute([
                ':franchise_id'   => $franchise['franchise_id'],
                ':franchisor_id'  => $franchise['franchisor_id'],
                ':franchise_name' => $franchise['franchise_name']
            ]);
        }

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
public static function setLeadData($franchises)
{
   try
   {
        $db = static::getDB();

        $sql = "INSERT INTO leads_franchises SET
               franchise_id    = :franchise_id,
               franchisor_id   = :franchisor_id,
               franchise_name  = :franchise_name";

        foreach($franchises as $franchise)
        {                
            $stmt = $db->prepare($sql);
            $result = $stmt->execute([
                ':franchise_id'   => $franchise['franchise_id'],
                ':franchisor_id'  => $franchise['franchisor_id'],
                ':franchise_name' => $franchise['franchise_name']
            ]);
        }

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
public static function setLeadData($franchises)
{

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

        $sql = "INSERT INTO leads_franchises (franchise_id, franchisor_id, franchise_name)
                VALUES (:franchise_id, :franchisor_id, :franchise_name)";

        $stmt = $db->prepare($sql);

        foreach($franchises as $franchise)
        {
            $stmt->bindParam(':franchise_id', $franchise['franchise_id']);
            $stmt->bindParam(':franchisor_id', $franchise['franchisor_id']);
            $stmt->bindParam(':franchise_name', $franchise['franchise_name']);

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

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
(编辑添加):下面是在
foreach循环中使用
bindParam
的函数:

public static function setLeadData($franchises)
{
   try
   {
        $db = static::getDB();

        foreach($franchises as $franchise)
        {
            $sql = "INSERT INTO leads_franchises SET
                   franchise_id    = :franchise_id,
                   franchisor_id   = :franchisor_id,
                   franchise_name  = :franchise_name";
            $stmt = $db->prepare($sql);
            $result = $stmt->execute([
                ':franchise_id'   => $franchise['franchise_id'],
                ':franchisor_id'  => $franchise['franchisor_id'],
                ':franchise_name' => $franchise['franchise_name']
            ]);
        }

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
public static function setLeadData($franchises)
{
   try
   {
        $db = static::getDB();

        $sql = "INSERT INTO leads_franchises SET
               franchise_id    = :franchise_id,
               franchisor_id   = :franchisor_id,
               franchise_name  = :franchise_name";

        foreach($franchises as $franchise)
        {                
            $stmt = $db->prepare($sql);
            $result = $stmt->execute([
                ':franchise_id'   => $franchise['franchise_id'],
                ':franchisor_id'  => $franchise['franchisor_id'],
                ':franchise_name' => $franchise['franchise_name']
            ]);
        }

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
public static function setLeadData($franchises)
{

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

        $sql = "INSERT INTO leads_franchises (franchise_id, franchisor_id, franchise_name)
                VALUES (:franchise_id, :franchisor_id, :franchise_name)";

        $stmt = $db->prepare($sql);

        foreach($franchises as $franchise)
        {
            $stmt->bindParam(':franchise_id', $franchise['franchise_id']);
            $stmt->bindParam(':franchisor_id', $franchise['franchisor_id']);
            $stmt->bindParam(':franchise_name', $franchise['franchise_name']);

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

        return $result;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        exit();
    }
}
每个版本产生相同的副本

显然,我很沮丧。我非常希望理解为什么以及如何使用这段代码导致重复

非常感谢您的帮助

函数执行后的表:


更新

为了帮助任何可能遇到此问题的人,我想发布重复的原因。经验证,功能代码是正确的

错误出现在Ajax代码中。表格提交了两次。由提供的解决方案是

除了
e.preventDefault()
,我还需要添加
e.stopImmediatePropagation

jQuery Ajax的提交部分如下所示:

$("#form").submit(function(e) {
    e.preventDefault();
    e.stopImmediatePropagation();

    ....

我测试了以下样本,没有出现重复:

class Franchises
{
    protected static function getDB()
    {
        $conn = NULL;

        try
        {
            $conn = new PDO("mysql:host=127.0.0.1;dbname=franchises_db", "dbuser", "dbpassword");
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
            exit();
        }
        return $conn;
    } 
    public static function setLeadData($franchises)
    {
       try
       {
            $db = static::getDB();

            $sql = "INSERT INTO leads_franchises SET
                   franchise_id    = :franchise_id,
                   franchisor_id   = :franchisor_id,
                   franchise_name  = :franchise_name";
            $stmt = $db->prepare($sql);

            foreach($franchises as $franchise)
            {
                $result = $stmt->execute([
                    ':franchise_id'   => $franchise['franchise_id'],
                    ':franchisor_id'  => $franchise['franchisor_id'],
                    ':franchise_name' => $franchise['franchise_name']
                ]);
            }

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

$vals = [
    [
        "franchise_id" => 3,
        "franchisor_id" => 3,
        "franchise_name" => "Fitness Freaks"
    ],
    [
        "franchise_id" => 4,
        "franchisor_id" => 3,
        "franchise_name" => "Gyms Galore"
    ]
];

Franchises::setLeadData($vals);
leads_特许经营表创建代码为:

CREATE TABLE `leads_franchises` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `franchise_id` INT(11) NULL DEFAULT '0',
    `franchisor_id` INT(11) NULL DEFAULT '0',
    `franchise_name` VARCHAR(128) NULL DEFAULT '',
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

我弄糊涂了,哪一套有问题?您已经添加了4个sets@Ravi. 函数的每个变体都会产生相同的不需要的结果,即。复制我试图存储的记录。将
debug\u print\u backtrace()
放在函数开头,看看为什么要执行两次。@PaulSpiegel。谢谢我现在就来试试。@Spiegel函数将在哪里显示其结果?我的错误显示在console.log中,因为代码以jQuery Ajax请求开始。感谢您花时间完成此操作。我下一步计划在Ajax请求之外测试该功能,我想可能其中有个bug。我将复制您的测试,并让您知道我是否获得了预期的相同结果。我独立测试了该函数并获得了相同的结果,证明该函数是书面的。那么,我的问题的答案是,以各种形式编写的函数确实有效,因此我在其他地方有一个问题。谢谢你花时间回复我的帖子!