爆炸然后插入表格

时间:2014-12-04 03:29:07

标签: php mysql sql string explode

我只是对如何爆炸字符串以单独插入数据库感到困惑。

我的代码:

$mymotives = "Entrega de Documentos, Orientacion, Username y Password, Convalidacion";          
$ExplodeMotives = explode(", ", $mymotives);
foreach ($ExplodeMotives as  $value) {
    $queryAdd = "INSERT INTO StuSelectCateg (attendedBy, personName, personLast, depName, categoryName, timeOut) 
                 VALUES ('".$_SESSION["nomEmpleado"]. " " . $_SESSION["appEmpleado"]."', '".$_SESSION['estNombre']."', '".$_SESSION['estApellido']."', '".$_SESSION['concentracion']."', '$value', '$stuTOut')";
    mysqli_query($con, $queryAdd);
}

我希望我的输出像这样

在我的数据库中
Table | Category
-----------------------------
1     | Entrega de Documentos
2     | Username y Password
3     | Convalidacion

4 个答案:

答案 0 :(得分:0)

<?php
      $mymotives = "Entrega de Documentos, Orientacion, Username y Password, Convalidacion";
      $ExplodeMotives = explode(", ", $mymotives);
      foreach ($ExplodeMotives as  $value)
      {
           $queryAdd[] = "INSERT INTO StuSelectCateg (attendedBy, personName, personLast, depName, categoryName, timeOut) VALUES ('".$_SESSION["nomEmpleado"]. " " . $_SESSION["appEmpleado"]."', '".$_SESSION['estNombre']."', '".$_SESSION['estApellido']."', '".$_SESSION['concentracion']."', '$value', '$stuTOut')";
      }
     mysqli_query($con, $queryAdd);
?>

你可以改变变量查询添加到像爱一样的数组。

答案 1 :(得分:0)

哇感谢上帝我自己做了这件事,无论哪种方式都是错误的

  

$mymotives=$stuMotives; $ExplodeMotives = explode(", ", $mymotives); foreach ($ExplodeMotives as $value) { $queryAdd = "INSERT INTO StuSelectCateg (attendedBy, personName, personLast, depName, categoryName, timeOut) VALUES ('".$_SESSION["nomEmpleado"]. " " . $_SESSION["appEmpleado"]."', '$stuName', '$stuApell', '$stuDepart', '$value', '$stuTOut')"; mysqli_query($con, $queryAdd); }

答案 2 :(得分:0)

回复相当晚,但是想指出,使用其他解决方案,当您可以在一个SQL查询中执行所有插入时,您将创建与mysql的多个连接。使用我的解决方案。

<?php
      $string = "Entrega de Documentos, Orientacion, Username y Password, Convalidacion";
      $categories = explode(", ", $string);

      $query = "INSERT INTO StuSelectCateg (attendedBy, personName, personLast, depName, categoryName, timeOut) VALUES ";

      foreach ($ExplodeMotives as  $value)
      {
           $query_vals[]  =   "('" 
                          .$_SESSION["nomEmpleado"]. "', '" 
                          .$_SESSION["appEmpleado"]."',  '" 
                          .$_SESSION['estNombre']."', '"
                          .$_SESSION['estApellido']."', '"
                          .$_SESSION['concentracion']."', '"
                          .$value."', '"
                          .$stuTOut. "')";
      }
     $query = $query . implode(", ", $query_vals );
     mysqli_query($con, $query);
?>

这将执行一个查询,例如在一个mysql插入调用中插入所有行:

INSERT INTO tableName
   (column1,column2,column3,column4)
VALUES
  ('value1' , 'value2', 'value3','value4'),
  ('value1' , 'value2', 'value3','value4'),
  ('value1' , 'value2', 'value3','value4');

答案 3 :(得分:0)

获取字符串数据并爆炸并存储到数据库中

$str = "abcde@gmail.com,dfkjp@gmail.com,veer@gmail.com,kalll@gmail.com";
    $emails=explode(",",$str);
    $key=1;
    foreach($emails as $email){
    //echo $email.'<br>';
    $sql="INSERT INTO `block_simplenotify`(`id`,`email`) VALUES ('$key++','$email')";
    }