只执行第一个SQL语句。为什么这样?

时间:2017-06-22 07:10:33

标签: php mysql slim

我正在为我的API使用Slim PHP框架。在这个函数中,我使用了两个语句$sql$sql1来执行两个不同的查询。 $sql已成功插入,$sql1未成功插入。我无法调试此代码。

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app->post('/insert/work_experience', function (Request $request, Response $response) {

$user_id        = $_POST[0];
$company_name   = $_POST[1];
$start_date = $_POST[2];
$end_date       = $_POST[3];
$designation    = $_POST[4];
$vertical       = $_POST[5];
$skillstring    = $_POST[6];

$skill = unserialize($skillstring); 

$sql = "INSERT INTO work_experience (user_id, company_name, start_date, end_date, designation, vertical)
VALUES (:user_id,:company_name,:start_date,:end_date,:designation,:vertical);";

$sql1 = "INSERT INTO skills (user_id, start_date, end_date, skill)
VALUES (:user_id,:start_date,:end_date,:skill);"; 

try{
    //make db object
    $db = new db();

    //connection
    $db = $db->connect();

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

    $stmt->bindParam(':user_id',        $user_id);
    $stmt->bindParam(':company_name',   $company_name);
    $stmt->bindParam(':start_date',     $start_date);
    $stmt->bindParam(':end_date',       $end_date);
    $stmt->bindParam(':designation',    $designation);
    $stmt->bindParam(':vertical',       $vertical);

    $stmt->execute();  //This statement is being executed successfully 

    $stmt1 = $db->prepare($sql1);

    $stmt1->bindParam(':user_id',       $user_id);
    $stmt1->bindParam(':start_date',    $start_date);
    $stmt1->bindParam(':end_date',      $end_date);


    $i=0;
    while($i<count($skill)){
        $skill1 = $skill[$i];
        $stmt1->bindParam(':skill', $skill1);
        $i++;
        $stmt1->execute(); //This statement is not being executed
    }

    echo '{"notice": {"text": "Data Added"}'; 

} catch(PDOException $e){
    echo '{"error": {"text": '.$e->getMessage().'}';
} 

});

0 个答案:

没有答案