mysqli准备的问题变量数量不匹配

时间:2013-11-17 16:14:14

标签: php mysql mysqli prepared-statement

我收到错误'变量数与预备语句中的参数数量不匹配'

 $stmt = $this->conn->prepare( "INSERT INTO  user 
            ( 
                st1, u1, e1, sa1, 
                h1, roles_id, name_titles_id, first_name, 
                last_name, phone,  mobile, address_road,
                address_area, address_region, post_code, city, 

                country_id, creation_date, activated_at, modified_date_time, 
                created_by, referred_by, gender, ad1, status 
            ) 
            VALUES 
            ( 
                ?, ?, ?, ?, 
                ?, ?, ?, ?, 
                ?, ?, ?, ?, 
                ?, ?, ?, ?, 

                ?, ?, ?, ?,
                ?, ?, ?, ?, ?
                 )" 
            ) 


        $stmt->bind_param('i',$st1);
        $stmt->bind_param('s',$u1);
        $stmt->bind_param('s',$e1);
        $stmt->bind_param('s',$sa1);

        $stmt->bind_param('s',$h1);
        $stmt->bind_param('i',$roles_id);
        $stmt->bind_param('i',$name_titles_id);
        $stmt->bind_param('s',$first_name);

        $stmt->bind_param('s',$last_name);
        $stmt->bind_param('s',$phone);
        $stmt->bind_param('s',$mobile);
        $stmt->bind_param('s',$address_road);

        $stmt->bind_param('s',$address_area);
        $stmt->bind_param('s',$address_region);
        $stmt->bind_param('s',$post_code);
        $stmt->bind_param('s',$city);

        $stmt->bind_param('i',$country_id);
        $stmt->bind_param('s',$creation_date);
        $stmt->bind_param('s',$activated_at);
        $stmt->bind_param('s',$modified_date_time);

        $stmt->bind_param('i',$created_by);
        $stmt->bind_param('i',$referred_by);
        $stmt->bind_param('s',$gender);
        $stmt->bind_param('s',$ad1);
        $stmt->bind_param('i',$status);

修改

只需做一个小测试即可确认,我们不能将多个bind_param与mysqli一起使用。

不行:

$stmt->bind_param('s',$a);
$stmt->bind_param('s',$b); 

工作:

$stmt->bind_param('ss',$a, $b);

希望它对将来的搜索有用。

1 个答案:

答案 0 :(得分:0)

你的问题很简单。您正在尝试手动手动,而数据的数量则要求自动化流程。你必须让一个程序为你创建一个查询。

假设您已有一个包含数据的数组。您所需要的只是定义要插入的字段列表

$fields  = "st1,u1,e1,sa1,h1,roles_id,name_titles_id,first_name,last_name,phone,";
$fields .= "mobile,address_road,address_area,address_region,post_code,city,";
$fields .= "country_id,creation_date,activated_at,modified_date_time,"; 
$fields .= "created_by,referred_by,gender,ad1,status" ;
$fields  = explode(",",$fields);

然后使用一些编程。幸运的是,it's already done

include 'safemysql.class.php';
$db = new safeMysql();
$insert = $db->filterArray($_POST,$fields);
$db->query("INSERT INTO user SET ?u", $insert);

是的,你正在以错误的方式使用bind_param。正确的用法可以在手册页中看到。