插入零时pdo抛出错误

时间:2013-07-29 00:26:49

标签: php pdo int sqlbindparameter

一些表字段是int(11),如果用户填写表单并插入1或更高版本就可以正常工作,但是当他们插入零时我遇到了大麻烦并且它有效。 如何让以下脚本插入0作为int

 <?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';
    }

    }
    }               

    ?>

1 个答案:

答案 0 :(得分:0)

使用松散比较(==),emtpy字符串""等于整数0。它不应该是表单提交的问题,因为它们总是字符串,但我不知道你是否将值类型转换为int。请尝试使用严格比较(===),例如:

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

您还有一个额外的{ $flag = 'error'; }没有if