How to insert several data at the same time in mysql and php?

0

I want to insert several values at the same time in a mysq table, I have two INSERT'S but only the first works (formatted), the other is constituted by a for cycle because it is inserted three times but it does not work (orders) this is the code php:

<?php
//conectamos con el servidor
    $conectar= mysql_connect('localhost','root','');
    //verificamos la conexion
    if(!$conectar){
        echo"No se pudo conectar con el servidor";

    }else{

$base=mysql_select_db('direccion');
    if(!$base){
        echo"No se encontro la base de datos";

    }

    }
//recuperar las variables

    $area=$_POST['area'];
    $numerointerno=$_POST['numerointerno'];
    $fecha=$_POST['fecha'];
    $folio=$_POST['folio'];
    $partida=$_POST['partida'];
    $pedidos=$_POST['pedidos'];
    $concargo=$_POST['concargo'];
    $lote1=$_POST['lote'];
    $cantidad=$_POST['cantidad'];
    $unidad=$_POST['unidad'];
    $conceptos=$_POST['conceptos'];
    $costo=$_POST['costo'];


//hacemos la sentencia de sql
    $sql="INSERT INTO formatod   VALUES('$area','$numerointerno','$fecha','$folio','$partida','$pedidos','$concargo')";

    $sql1="INSERT INTO pedidos (lote,cantidad,unidad,conceptos,costo) VALUES";


for ($i = 0; $i < count($lote1); $i++){
    $sql1.="('".$lote1[$i]."','".$cantidad[$i]."','".$unidad[$i]."','".$conceptos[$i]."','".$costo[$i]."'),";

    echo "<center><strong><h4>¡INSERCIÓN EXITOSA!<br><a href='table.php'></a></strong></h4></center>";

}

?>

I hope you can help me

    
asked by Luis Miguel Vidal Garcia 16.06.2017 в 20:48
source

2 answers

1

There are several ways to enter data

PDO

To enter data in a database, first you have to bear in mind that you have a process:

$sentencia = $mbd->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$sentencia->bindParam(':name', $nombre);
$sentencia->bindParam(':value', $valor);

// insertar una fila
$nombre = 'uno';
$valor = 1;
$sentencia->execute();

// insertar otra fila con diferentes valores
$nombre = 'dos';
$valor = 2;
$sentencia->execute();

Source

Things to keep in mind

  • If you notice, in the code above, first insert the query mysql, with :nombreVariable , in the prepare, where you prepare the PHP statement, to attach to each :valor its corresponding value with bindParam .
  • Assigns the values.
  • Perform the execute.
  • Comment that bindParam, has the parameters of bindParam(':nombre , $ name); ' being the first one you define in the mysql statement and the second one you define in the variable.

    Classical

    What you do, you do it correctly:

    $consulta = "insert into tabla_d value (a,b,c,d)";
    
    for($x = 0 ; $x <= 3 ; $x++){
      $sql.= "(a,b,c,d),"
      if($x == 3){
         $sql.= "(a,b,c,d);"
      }
    }
    

    Solution:

    $sql1="INSERT INTO pedidos (lote,cantidad,unidad,conceptos,costo) VALUES";
    
    
    for ($i = 0; $i < count($lote1); $i++){
       if(($i+1) == count($lote1)){
        $sql1.="('".$lote1[$i]."','".$cantidad[$i]."','".$unidad[$i]."','".$conceptos[$i]."','".$costo[$i]."');";
    
       }else{
        $sql1.="('".$lote1[$i]."','".$cantidad[$i]."','".$unidad[$i]."','".$conceptos[$i]."','".$costo[$i]."'),";
    
       }
    }
        echo "<center><strong><h4>¡INSERCIÓN EXITOSA!<br><a href='table.php'></a></strong></h4></center>";
    

    The question that I look at, the ; that you do not put in the end does not end the query, logically this I did without putting myself to check it, but I think it serves to guide you.

    By the way: Curiosity

    Insert several data at the same time in the same table with mysql and php, it is not possible, because Mysql already controls that not exactly edit the data at the same time, we abstract from problems in the database in consistency issues of the data.

        
    answered by 17.06.2017 в 02:04
    0

    You could try doing something like this:

    for ($i = 0; $i < count($lote1); $i++){
    $sql1="INSERT INTO pedidos (lote,cantidad,unidad,conceptos,costo) VALUES '".$lote1[$i]."','".$cantidad[$i]."','".$unidad[$i]."','".$conceptos[$i]."','".$costo[$i]."')";
    }
    

    And remember the execution of your query should go within for to be applied in each iteration.

    As you mentioned, it is not a good practice to use the API mysql when PHP in your documentation marks it as obsolete and insecure, you should use what you mark PHP as active and recommended.

        
    answered by 16.06.2017 в 20:59