$ prepStatement->执行(array_values($ INV));不起作用

时间:2017-06-19 16:23:57

标签: php mysql json pdo execute

我找不到解决方案。 $ prepStatement->执行(array_values($ INV));不起作用 我在这一行得到了一个错误。

我的php脚本:

<?php

$response=array();
$json = $_REQUEST['json'];
$data = json_decode($json);

var_dump(json_decode($json, true));

print "Starting request". "<br/>";
if($_SERVER['REQUEST_METHOD'] == "POST" && (json_last_error() === JSON_ERROR_NONE))
{   

    if($data->connection && $data->wykazy)
    {
        print "connection and wykazy exist". "<br/>";
        if ($data->connection->Host && $data->connection->db && $data->connection->User && $data->connection->password) {
            print "connection Host, db, User, password are OK". "<br/>";
            try
            {
                $opt = [
                        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                        PDO::ATTR_EMULATE_PREPARES   => false,
                        ];
                $dbObj = new PDO('mysql:host='.$data->connection->Host.';dbname='.$data->connection->db, $data->connection->User, $data->connection->password, $opt);
                print "Proba ustanowienia polaczenia! Bledy: " . $dbObj->errorCode() . "<br/>";
                createWykaz($dbObj, $data);
            }
            catch (PDOException $e)
            {
                print "Blad podczas tworzeniu obiektu dbObj!: " . $e->getMessage() . "<br/>";
                $response["success"]=0;
                $response["message"]="Can not connect to database!: " . $e->getMessage();
                echo json_encode($response);
                die();
            }
            //$mysqli = new mysqli($data->connection->Host, $data->connection->User, $data->connection->password, $data->connection->db);

        }
    }
    else 
    {
        print "connection i wykazy nie istnieją! " . "<br/>";
        $response["success"]=0;
        $response["message"]="Error: Required fields are missing!";
        echo json_encode($response);
    }
}

function createWykaz($dbObj, $data)
{
    try{    
        $allowedVariables = array(
                'wyk_ilosc',
                'wyk_czasod',
                'wyk_czasdo',
                'wyk_id_czynnosc',
                'wyk_id_pracownik',
                'wyk_id_partia',
                'wyk_status',
        );

        $sql = "insert into wykaz (". implode(',', $allowedVariables) .")";
        $sql.= " VALUES (". substr(str_repeat('?,', sizeof($allowedVariables)), 0, -1) .");";
        print "sql: ". $sql . "<br/>";
        if($prepStatement = $dbObj->prepare($sql)){
            //edit

            $dbObj->beginTransaction();


            foreach ( $data->wykazy as $inv ) {
                $res = $prepStatement->execute(array_values($inv));
            }
        /*
            $prepStatement = $dbObj->prepare($sql)
            foreach ($data->wykazy as $row) {
            $dbObj->executeMultiple($prepStatement, $row);//return DB_OK
         }
         */

            $response["success"]=1;
            $response["message"]="Wszystkie wykazy successfully added to Database!";
            echo json_encode($response);

            $dbObj->commit();

        }
        else {
            $response["success"]=0;
            $response["message"]="Błąd podczas przygotowania zapytania!"+ $prepStatement;
            echo json_encode($response);
        }
        $dbObj=null;
        $prepStatement=null;

    }catch(PDOException $e){
        //Error handling goes here
        echo "Sorry, the website is experiencing problems.";
        echo "Error: Our query failed to execute and here is why: \n";
        echo "Query: " . $sql . "\n";
        echo "Errno: " . $e->getMessage() . "\n";
        $response["success"]=0;
        $response["message"]="Our query failed to execute!". $e->getMessage();
        echo json_encode($response);
        $dbObj=null;
        $prepStatement=null;
    }

}

?>

直到: $ prepStatement-&GT;执行(array_values($ INV)); 我在这个位置遇到错误,但$ data-&gt; wykazy是一个数组。

我可以将我的代码更改为:

$sth = $db->prepare('INSERT INTO numbers VALUES (?, ?, ?)');
foreach ($alldata as $row) {
    $db->execute($sth, $row);
}

但我会尝试另一种方式。 感谢帮助 塞巴斯蒂安

Postman的输出:

Postman part1 Postman part2

0 个答案:

没有答案