【问题标题】:pdo throw error when inserting a zero插入零时 pdo 抛出错误
【发布时间】:2013-07-28 16:12:45
【问题描述】:

如果用户填写表格并插入 1 或更高版本,则一些表格字段是 int(11) 可以正常工作,但是当他们插入零时,我遇到了很大的麻烦并且它不起作用。 我如何获得以下脚本以将 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';
    }

    }
    }               

    ?>

【问题讨论】:

  • 标题和描述没有多大意义。无论如何,您为什么不尝试仅处理 try{}catch() 例外情况?
  • 只是一个笔记集$flag = 0 不是$flag =''。还有if($user_id===''){$flag=1; }etc

标签: php pdo int sqlbindparameter


【解决方案1】:

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

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

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

【讨论】:

  • 我尝试了不同的检查方法,甚至把检查语句都拿出来..
  • 如果尝试了各种不同的检查方式,我什至拿出了检查语句,我的问题是让这个脚本插入一个等于 0 的 $var 作为 int。
  • 如果你运行上面的代码,它会回显 Yay 语句还是 Max Fail 语句?您是否尝试将 bindParam() 的 data_type 参数设置为 PDO::PARAM_INT?另请注意,您有 12 个字段的 10 个 ?。
  • 抱歉,打字太匆忙了,可能有错误,但原版的长度是原来的 3 倍,所以我很快就弄了一个简短的版本,但它几乎是一样的。哦,谢谢你,ironcito 你能解释一下吗我如何使用“bindParam() 到 PDO::PARAM_INT”。请
  • 如果$user_id是一个int,那么$st-&gt;bindParam(1, $user_id, PDO::PARAM_INT);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-12
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
相关资源
最近更新 更多