3

我有这个函数可以将数据插入到表中:

public static function insertSistema($name,$description,$created_at, $updated_at, $img_file_name, $img_content_type, $img_file_size, $img_updated_at, $visible, $description, $access_floors, $access_procedures, $access_datas, $access_histories, $access_incidences, $access_operations, $access_reports, $access_messagings)
{
    $conector = new Conexion("localhost","xrem_prueba");
    try
    {
        $con = $conector->Conectar();
        $con->exec('SET CHARACTER SET utf8');
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $consulta = $con->prepare("INSERT INTO systems (name, created_at, updated_at, img_file_name, img_content_type, img_file_size, img_updated_at, visible, description, access_floors, access_procedures, access_datas, access_histories, access_incidences, access_operations, access_reports, access_messagings ) VALUES (:name, :created_at, :updated_at, :img_file_name, :img_content_type, :img_file_size, :img_updated_at, :visible, :description, :access_floors, :access_procedures, :access_datas, :access_histories, :access_incidences, :access_operations, :access_reports, :access_messagings);");
        $consulta->bindParam(':name', $name, PDO::PARAM_STR, 57);
        $consulta->bindParam(':description', $description, PDO::PARAM_STR);
        $consulta->bindParam(':created_at', $created_at, PDO::PARAM_STR);
        $consulta->bindParam(':updated_at', $updated_at, PDO::PARAM_STR);
        $consulta->bindParam(':img_file_name', $img_file_name, PDO::PARAM_STR,255);
        $consulta->bindParam(':img_content_type', $img_content_type, PDO::PARAM_STR,255);
        $consulta->bindParam(':img_file_size', $img_file_size, PDO::PARAM_INT);
        $consulta->bindParam(':img_updated_at', $img_updated_at, PDO::PARAM_INT);
        $consulta->bindParam(':visible', $visible, PDO::PARAM_INT,1);
        $consulta->bindParam(':access_floors', $access_floors, PDO::PARAM_INT,1);
        $consulta->bindParam(':access_procedures', $access_procedures, PDO::PARAM_INT,1);
        $consulta->bindParam(':access_datas', $access_procedures, PDO::PARAM_INT,1);
        $consulta->bindParam(':access_histories', $access_procedures, PDO::PARAM_INT,1);
        $consulta->bindParam(':access_incidences', $access_procedures, PDO::PARAM_INT,1);
        $consulta->bindParam(':access_operations', $access_procedures, PDO::PARAM_INT,1);
        $consulta->bindParam(':access_reports', $access_reports, PDO::PARAM_INT,1);
        $consulta->bindParam(':access_messagings', $access_messagings, PDO::PARAM_INT,1);
        $datos = array('name'=>$name,
                       'description'=>$description,
                       'created_at'=>$created_at,
                       'updated_at'=>$updated_at,
                       'img_file_name'=>$img_file_name,
                       'img_content_type'=>$img_content_type,
                       'img_file_size'=>$img_file_size,
                       'img_updated_at'=>$img_updated_at,
                       'visible'=>$visible,
                       'access_floors'=>$access_floors,
                       'access_procedures'=>$access_procedures,
                       'access_datas'=>$access_datas,
                       'access_histories'=>$access_histories,
                       'access_incidences'=>$access_incidences,
                       'access_operations'=>$access_operations,
                       'access_reports'=>$access_reports,
                       'access_messagings'=>$access_messagings
        );
        $consulta->execute($datos);
        $conector = null;
        $con = null;
        return $consulta;
    }
    catch (Exception $e)
    {
        $conector = null;
        $con = null;
        throw $e;
    }
}

如您所见,我有$consulta->bindParam(':name', $name, PDO::PARAM_STR, 57);第 10 行:bindParam选项,它应该截断 57 个字符的长度,但它会插入任何类型的字符,因此不会截断。

4

2 回答 2

6

长度参数不用于截断。仅当参数来自存储过程时才应使用它,然后,它用于告知长度 - 而不是设置它。您必须$name在绑定之前截断变量。

来自PHP 手册:“数据类型的长度。要指示参数是存储过程中的 OUT 参数,您必须显式设置长度。”

于 2013-06-17T11:43:13.190 回答
1

我是怎么做到的

public static function insertSistema($name,$description,$created_at, $updated_at, $img_file_name, $img_content_type, $img_file_size, $img_updated_at, $visible, $description, $access_floors, $access_procedures, $access_datas, $access_histories, $access_incidences, $access_operations, $access_reports, $access_messagings)
{
    global $con;
    $data   = func_get_args();
    $qmarks = str_repeat('?,', count($data) - 1) . '?';
    $stmt   = $con->prepare("INSERT INTO systems VALUES (NULL,$qmarks");
    $stmt->execute($data);
}

是的 - 我也会摆脱那个冗长而多风的参数列表,将它换成数组

于 2013-06-17T11:57:33.713 回答