Php 使用PDO插入会使值出错

Php 使用PDO插入会使值出错,php,mysql,pdo,Php,Mysql,Pdo,我将一些值传递给一个php文件,然后该文件应该使用PDO将数据插入MySQL表。 文件如下: <?php function makeInsert($query, $paramArray){ include 'db.php'; try { $pdo = new PDO("mysql:host=localhost;dbname=$db_name", $db_user, $db_pass); $stmt

我将一些值传递给一个php文件,然后该文件应该使用PDO将数据插入MySQL表。 文件如下:

<?php
    function makeInsert($query, $paramArray){
        include 'db.php';

        try {
            $pdo = new PDO("mysql:host=localhost;dbname=$db_name", $db_user, $db_pass);

            $stmt = $pdo->prepare($query);
            foreach($paramArray as $k => $v){
                $stmt->bindParam($k, $v, PDO::PARAM_STR);
            }

            $stmt->execute();

            return $stmt;
        } catch (PDOException $e) {
            $error = "Error!: " . $e->getMessage();

            return $error;
            die();
        }
    }
?>

我从此函数调用makeInsert:

<?php 
    include "./insert.php";

    function run(){
        $registration = "success";
        $ammountInput= "12.34";
        $kanalInput= "9";
        $datumInput = "08.2017";
        $datumArray = explode(".", $datumInput);

        if ($registration == "success"){
            $response_array['status'] = 'success';
            $indexnameYear      = ":year";
            $indexnameMonth     = ":month";
            $indexnameAmount    = ":amount";
            $indexnameChannel   = ":channel";

            $parameter = array($indexnameYear => $datumArray[1], $indexnameMonth => $datumArray[0], $indexnameAmount => $ammountInput, $indexnameChannel => $kanalInput);

            $insertIncomeQuery = "INSERT INTO `income`(`id`, `year`, `month`, `amount`, `channel`) VALUES (NULL, " . $indexnameYear . ", ". $indexnameMonth .", ". $indexnameAmount .", ". $indexnameChannel .")";
            //make the insert :-)
            $returnValue = makeInsert($insertIncomeQuery, $parameter);

            if($returnValue === "Success"){
                echo "All done.";
            }else if (strpos($returnValue, "Error!") !== false){
                echo "Sorry!:" . $returnValue;
            }

        }else{
            echo "something happened";

        }
    }

?>

bindParam
替换为
bindValue
回答如下: