PHP PDO:如何插入多行?

时间:2012-09-01 22:56:29

标签: php sql pdo

我有很多sql语句但我想要,但我想知道是否有任何方法或功能可以缩短并使用PHP PDO在尽可能少的行中进行。

我的一个想法是创建一个具有以下名称的数组:Perfiles,Usuarios,Customer,Mps,Poliza和Servicios,像foreach一样,但我没有具体的想法,你能帮助我吗?

SQL语句

$sqlpermsPer = "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar)
                                            VALUES ($idPerfil,'Perfiles',0,0,0,0)";
$resultpermsPer = $this->dbConnect->query($sqlpermsPer) or die ($sqlpermsPer);

sqlpermsC = "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar)
                                            VALUES ($idPerfil,'Clientes',0,0,0,0)";
$resultpermsC = $this->dbConnect->query($sqlpermsC) or die ($sqlpermsC);

$sqlpermsU = "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar)
                                            VALUES ($idPerfil,'Usuarios',0,0,0,0)";
$resultpermsU = $this->dbConnect->query($sqlpermsU) or die ($sqlpermsU);

$sqlpermsM = "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar)
                                            VALUES ($idPerfil,'Mps',0,0,0,0)";
$resultpermsM = $this->dbConnect->query($sqlpermsM) or die ($sqlpermsM);

$sqlpermsP = "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar)
                                            VALUES ($idPerfil,'Poliza',0,0,0,0)";
$resultpermsP = $this->dbConnect->query($sqlpermsP) or die ($sqlpermsP);

$sqlpermsS = "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar)
                                            VALUES ($idPerfil,'Servicio',0,0,0,0)";
$resultpermsS = $this->dbConnect->query($sqlpermsS) or die ($sqlpermsS);

提前致谢!

4 个答案:

答案 0 :(得分:3)

这将创建一个包含字符串的数组,并循环遍历它们,每次都使用绑定参数执行相同的查询。

$stmt = $this->dbConnect->prepare("INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar) VALUES (:idPerfil,:str,0,0,0,0)");
$stmt->bindParam(':idPerfil', $idPerfil);
$stmt->bindParam(':str',$val);
$in_arr = array("Perfiles","Clientes","Usuarios","Mps","Poliza","Servicio");

foreach ($in_arr as $key => $val) {
    $stmt->execute();
}  

答案 1 :(得分:0)

INSERT INTO
  table 
    (column_one, column_two) 
  VALUES
    ('value_one_one', 'value_one_two'), 
    ('value_two_one', 'value_two_two'), 
    ('value_three_one', 'value_three_two'); 

答案 2 :(得分:0)

只要您正确终止每个INSERT,就可以将它们连接起来。以下代码示例还使用prepare来阻止SQL注入。

$sql  = "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar) VALUES (:idPerfil,'Perfiles',0,0,0,0);";
ssql .= "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar) VALUES (:idPerfil,'Clientes',0,0,0,0);";
$sql .= "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar) VALUES (:idPerfil,'Usuarios',0,0,0,0);";
$sql .= "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar) VALUES (:idPerfil,'Mps',0,0,0,0);";
$sql .= "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar) VALUES (:idPerfil,'Poliza',0,0,0,0);";
$sql .= "INSERT INTO t_perfiles_permisos (Id_Perfil, Area_Permiso, Buscar, Crear, Eliminar, Modificar) VALUES (:idPerfil,'Servicio',0,0,0,0);";

try
{
  if ( $stmt = $db->prepare( $sql ) )
  {
    if ( ! $stmt->execute( array( ':idPerfil' => $idPerfil ) )
      error_log( $stmt->errorInfo() );
  }
}
catch ( PDOException $e )
{
  error_log( $e->getMessage() );
}

答案 3 :(得分:-1)

请检查此answer

您可能希望执行多插入语句,如下所示:

$qry = 'INSERT INTO table (FirstName, LastName) VALUES ';
$qry .= "(?,?), "
      . "(?,?), "
      . "(?,?)  "
      ;