2016-07-04 7 views
0

Ich habe 2 mysql Tabellen: factura und detallefactura.PHP lastInsertId() als ein Array, und fügen Sie Index für die gleiche wie ein anderes Array

factura hat 2 Felder:

id 
cliente 

detallefactura hat drei Felder:

id 
fac_id 
tratamiento 

Ich möchte, dass fac_idlastInsertId() von factura(id) sein.

Mein Problem ist, dass tratamiento wird mehr als eine Zeile (was Benutzer in der Form hinzugefügt), so muss ein Array sein.

Die Frage ist, wie kann ich mehr als einen Index in fac_id Array erstellen, um dasselbe wie tratamiento Array zu sein? Jetzt erhalte ich:

Notice: Undefined offset: 1 in C:\xampp\htdocs\noepruebalast\prueba2.php on line 76 


DetalleFacturas Object 
(
    [conn:DetalleFacturas:private] => PDO Object 
     (
     ) 

    [table_name:DetalleFacturas:private] => detallefactura 
    [id] => 
    [fac_id] => Array 
     (
      [0] => 75 
     ) 

    [tratamiento] => Array 
     (
      [0] => Limpieza 
      [1] => Extraer 
     )) 

ich brauche "einen anderen Index" [1] => 75

Hier ist der Beispielcode:

<?php 

// get database connection 
include_once 'config/database.php'; 

$database = new Database(); 
$db = $database->getConnection(); 

class Facturas{ 

    // database connection and table name 
    private $conn; 
    private $table_name = "factura"; 

    // object properties 
    public $id; 
    public $cliente; 

    public function __construct($db){ 
     $this->conn = $db; 
    } 

function create(){ 

     $query = "INSERT INTO 
        " . $this->table_name . " 
       SET 
        cliente = :cliente"; 

     $stmt = $this->conn->prepare($query); 

     $stmt->bindParam(':cliente', $this->cliente); 

     if($stmt->execute()){ 
      return true; 
     }else{ 
      return false; 
     } 
    } 
} 
$factura = new facturas($db); 


class DetalleFacturas{ 

    // database connection and table name 
    private $conn; 
    private $table_name = "detallefactura"; 

    // object properties 
    public $id; 
    public $fac_id; 
    public $tratamiento; 

    public function __construct($db){ 
     $this->conn = $db; 
    } 


    function create(){ 

     $query = "INSERT INTO 
        " . $this->table_name . " 
       SET 
        fac_id = :fac_id, tratamiento = :tratamiento"; 

     $stmt = $this->conn->prepare($query); 

     $stmt->bindParam(':fac_id', $fac_ids); 
     $stmt->bindParam(':tratamiento', $tratamientos); 


     if (is_array($this->tratamiento)) { 
      foreach ($this->tratamiento as $clave=> $tratamientos) { 
       $fac_ids = $this->fac_id[$clave]; 
       $stmt->execute(); 
      } 
     } 
    } 
} 

$detallefactura = new detallefacturas($db); 



if ($_POST){ 
      $factura->cliente = $_POST['cliente']; 
      $factura->create(); 
      $last[] = $db->lastInsertId('factura'); 

      $detallefactura->fac_id = $last; 
      $detallefactura->tratamiento = $_POST['tratamiento']; 
      $detallefactura->create(); 

     } else { 

      echo "no va"; 
     } 
?> 

<form action='prueba2.php' method='post'> 

    <table class='table table-hover table-responsive table-bordered'> 

     <tr> 
      <td>Cliente</td> 
      <td><input type='text' name='cliente' class='form-control'></td> 
     </tr> 

     <tr> 
      <td>Tratamiento</td> 
      <td><input type='text' name='tratamiento[]' class='form-control'></td> 
     </tr> 

       <tr> 
      <td>Tratamiento2</td> 
      <td><input type='text' name='tratamiento[]' class='form-control'></td> 
     </tr> 

     <tr> 
      <td> 
       <button type='submit' class='btn btn-primary'>Crear</button> 
      </td> 
     </tr> 

    </table> 
</form> 

<pre><?php print_r($detallefactura) ?></pre> 
+1

LAST_INSERT_ID() kann immer nur einen Wert zurückgeben. Es wird keine Reihe von IDs zurückgeben, wenn Sie eine Reihe von Einfügungen durchführen. Sie erhalten nur die ** LAST ** ID, die für eine Verbindung generiert wurde. Sie müssten/id/einfügen/get ID/etc ... –

+0

Ich brauche nur einen Wert (in diesem Fall 75), aber ich muss es so oft wie Tratamiento Array kopieren. Wenn tratamiento ein Array von 2 ist, brauche ich [fac_id] => Array ( [0] => 75 [1] => 75 ) – rafpre

+0

Ich will eine Million Dollar, nicht bedeuten, ich werde es bekommen, lastinsertid ist nur, dass die letzte Einfüge-ID, können Sie die IDs wählen Reihenfolge ihnen desc ... Oder warum nicht die letzte Einfüge-ID von create() zurück und dann speichern Sie, wie Sie Elemente einfügen. – ArtisticPhoenix

Antwort

0

für dieses

ich nur einen Wert benötigen (in dieser Fall 75), aber ich muss es so oft als tratamiento Array kopieren. Wenn tratamiento ein Array von 2 ist, ich brauche [fac_id] => Array ([0] => 75 [1] => 75)

Wenn Sie es wirklich brauchen, dass viele Male dann tun array_keys und array_fill_keys

$keys = array_keys($tratamiento); 
$fac_id = array_fill_keys($keys, $insertId); 

So ähnlich.

http://php.net/manual/en/function.array-keys.php

http://php.net/manual/en/function.array-fill-keys.php

+0

Ich werde das versuchen. – rafpre

+0

Danke, es funktioniert: $ detallefactura-> fac_id = $ last; $ Schlüssel = array_keys ($ _ POST ['tratamiento']); $ detallefactura-> fac_id = array_fill_keys ($ keys, $ last); – rafpre

+0

Sicher, fühlen Sie sich frei, die Antwort zu akzeptieren .... – ArtisticPhoenix

Verwandte Themen