2013-07-29 55 views
0

如果用戶填寫表單並插入1或更高版本,它可以正常工作,但是當它們插入零時,一些表字段爲int(11),但是當它們插入零時,我遇到了很大的麻煩,工作。 我如何獲得下面的腳本插入0作爲INTpdo插入零時出現錯誤

<?php 

    class add_qual { 

    private $db; 

    public function __construct(){ 
    $this->db = new connection(); 
    $this->db = $this->db->dbconnect(); 

    } 

    public function new_qual($user_id, 
    $providers, 
    $code, 
    $title, 
    $title_under, 
    $full_title, 
    $description, 
    $comments, 
    $level, 
    $expires, 
    $period_months 
    $is_superceded) { 


    $flag=''; 
    if($user_id==''){$flag='error'; } 
    if($providers==''){$flag='error'; } 
    if($title==''){$flag='error'; } 
    if($expires==''){$flag='error'; } 
    {$flag='error'; } 

    if ($flag=='') { 

    //if($user_id!=''){ 

    $st = $this->db->prepare("INSERT INTO national_body_qualifications_list 
    (user_id, 
    providers, 
    code, 
    title, 
    title_under, 
    full_title, 
    description, 
    comments, 
    level, 
    expires, 
    period_months, 
    is_superceded) 

    VALUES (?,?,?,?,?,?,?,?,?,?)"); 
    $st->bindParam(1, $user_id); 
    $st->bindParam(2, $providers); 
    $st->bindParam(3, $code); 
    $st->bindParam(4, $title); 
    $st->bindParam(5, $title_under); 
    $st->bindParam(6, $full_title); 
    $st->bindParam(7, $description); 
    $st->bindParam(8, $comments); 
    $st->bindParam(9, $level); 
    $st->bindParam(10, $expires); 
    $st->bindParam(11, $period_months); 
    $st->bindParam(12, $is_superceded); 

    $st->execute(); 
    echo 'Yay it worked'; 

    } else { 
     echo 'Max Fail'; 
    } 

    } 
    }    

    ?> 
+0

標題和描述沒有多大意義。無論如何,你爲什麼不嘗試只嘗試和捕捉異常呢? – samayo

+0

只需要註釋'$ flag = 0'而不是'$ flag ='''。而'if($ user_id ===''){$ flag = 1; }'等 –

回答

0

使用比較寬鬆(==)的emtpy串""等於整數0。它不應該是表單提交的問題,因爲它們總是字符串,但我不知道你是否已經將這些值輸入爲int。嘗試使用嚴格比較(===)來代替,如:

if ($title === '') { $flag = 'error'; } 

您也有一個額外的{ $flag = 'error'; }沒有if

+0

我已經嘗試了不同的檢查方法,甚至取出如果檢查統計所有在一起.. – user1425394

+0

如果tryed各種不同的方式來檢查,我甚至拿出檢查語句,我的問題是讓這個腳本插入$作爲int的var等於0。 – user1425394

+0

如果您運行上面的代碼,它是否回顯Yay語句或Max Fail語句?您是否嘗試將bindParam()的data_type參數設置爲PDO :: PARAM_INT?另外請注意,你有12個字段有10個。 – ironcito