php和sql select case - >只保存部分值

时间:2018-05-01 11:28:16

标签: php sql select case

我不能给我的问题起一个名字,所以我真的无法弄明白 有两个表标识和标记 我希望能够通过基于php的网页输入所有值 并且所有数值都应该在表格中给出 如果我用cmdline做所有事情就可以了:

insert into tag (chipnr1,chipnr2,ringing_tel) values ("","3","3");
insert into identification (name, species_nr, sex_nr, tag_nr) 
    values ("test3", 
    (select species_nr from species where sp_term="Hund"), 
    (select sex_nr from sex where sx_term="weiblich"),
    (select case
    when exists (select tag_nr from tag where chipnr1="")
    then (select tag_nr from tag where chipnr2="3")
    else 'NULL'
    end
    )
    );

如果我使用php,它只保存标签中的值,而不是标识

if(isset($_GET['eingabe'])) { 
    try{
        // Statement einfügen
        $sql = "insert into tag (chipnr1,chipnr2,ringing_tel) values ((:chipnr1),(:chipnr2),(:ringing_tel)); 
        insert into identification (name, species_nr, date_of_birth, sex_nr, breed, color, hair_nr, characteristics, tag_nr) 
        values (
        (:name), 
        (select species_nr from species where sp_term=(:sp_term)), 
        (:date_of_birth), 
        (select sex_nr from sex where sx_term=(:sx_term)),
        (:breed),
        (:color),
        (select hair_nr from hair where h_term=(:h_term)),
        (:characteristics),
        (select case
        when exists (select tag_nr from tag where chipnr1=(:chipnr1))
        then (select tag_nr from tag where chipnr2=(:chipnr2))
        else 'NULL'
        end))
        )";


        $stmt = $pdo->prepare($sql);        
        $stmt->bindParam(':name', $_REQUEST['name']);
        $stmt->bindParam(':sp_term', $_REQUEST['sp_term']);
        $stmt->bindParam(':date_of_birth', $_REQUEST['date_of_birth']);
        $stmt->bindParam(':sx_term', $_REQUEST['sx_term']);
        $stmt->bindParam(':breed', $_REQUEST['breed']);
        $stmt->bindParam(':color', $_REQUEST['color']);
        $stmt->bindParam(':h_term', $_REQUEST['h_term']);
        $stmt->bindParam(':characteristics', $_REQUEST['characteristics']);
        $stmt->bindParam(':chipnr1', $_REQUEST['chipnr1']);
        $stmt->bindParam(':chipnr2', $_REQUEST['chipnr2']);
        $stmt->bindParam(':ringing_tel', $_REQUEST['ringing_tel']);

        $stmt->execute();
        $res = $stmt; 
        if($res) {
            echo '<p align=center><h1>Tier erfolgreich hinzugefügt <meta http-equiv="refresh" content="3;URL=eingabe_tier.php"></h1></p>';
            $showFormular = false ; 
        }
            else {
                echo 'Das Hinzufügen eines neuen Datensatzes ist fehlgeschlagen';
            }
        echo "Tier erfolgreich hinzugefügt";

    } catch(PDOException $e){
        die("ERROR: Could not able to execute $sql. " .      $e->getMessage());
    }    

我会很感激任何建议

0 个答案:

没有答案