Problema Salva e Stampa contemporaneamente

  • Creatore Discussione Creatore Discussione elpirata
  • Data di inizio Data di inizio

elpirata

Utente Attivo
18 Mar 2009
264
7
18
Ciao raga,
ho un file salva.php che preleva tramite POST i dati inseriti in delle form e li inserisce in un database,

questo il codice :

Codice:
<?php

// INCLUDO IL FILE PER LA CONNESSIONE AL DATABASE
include ("conn.php");

//Effettuo l'Escape dei campi _POST 

foreach ($_POST as $key => $value) {
$_POST[$key] = mysql_real_escape_string($value);
}

// EFFETTUO L'UPPER CASE DEI CAMPI INSERITI

foreach ($_POST as $key => $value) { 
    $_POST[$key] = strtoupper($value); 
} 

$veicolo = $_POST['veicolo'];
$targa = $_POST['targa']; 
$km = $_POST['km'];
$telaio = $_POST['telaio']; 
$cliente = $_POST['cliente']; 
$indirizzo = $_POST['indirizzo']; 
$piva = $_POST['piva']; 
$tel = $_POST['tel']; 
$itr1 = $_POST['itr1'];
$itr2 = $_POST['itr2'];
$itr3 = $_POST['itr3'];
$itr4 = $_POST['itr4']; 
$itr5 = $_POST['itr5']; 
$itr6 = $_POST['itr6'];
$itr7 = $_POST['itr7']; 
$itr8 = $_POST['itr8'];
$itr9 = $_POST['itr9']; 
$itr10 = $_POST['itr10'];
$itr11 = $_POST['itr11'];
$itr12 = $_POST['itr12']; 
$itr13 = $_POST['itr13'];
$itr14 = $_POST['itr14'];
$itr15 = $_POST['itr15'];
$for1 = $_POST['for1']; 
$for2 = $_POST['for2'];
$for3 = $_POST['for3'];
$for4 = $_POST['for4'];
$for5 = $_POST['for5'];
$for6 = $_POST['for6'];
$for7 = $_POST['for7']; 
$for8 = $_POST['for8']; 
$for9 = $_POST['for9']; 
$for10 = $_POST['for10']; 
$for11 = $_POST['for11']; 
$for12 = $_POST['for12']; 
$for13 = $_POST['for13']; 
$for14 = $_POST['for14']; 
$for15 = $_POST['for15']; 
$imp_1 = $_POST['imp_1']; 
$imp_2 = $_POST['imp_2']; 
$imp_3 = $_POST['imp_3']; 
$imp_4 = $_POST['imp_4']; 
$imp_5 = $_POST['imp_5']; 
$imp_6 = $_POST['imp_6']; 
$imp_7 = $_POST['imp_7']; 
$imp_8 = $_POST['imp_8']; 
$imp_9 = $_POST['imp_9']; 
$imp_10 = $_POST['imp_10']; 
$imp_11 = $_POST['imp_11']; 
$imp_12 = $_POST['imp_12']; 
$imp_13 = $_POST['imp_13']; 
$imp_14 = $_POST['imp_14']; 
$imp_15 = $_POST['imp_15']; 
$totale = $_POST['totale']; 
$n_tecnico = $_POST['n_tecnico']; 
$data = $_POST['data'];   

// QUERY DI INSERIMENTO SENZA UPPER, L'UPPER E' DEMANDATO AL COMANDO FOREACH

$sql = "INSERT INTO `CommesseO` (`veicolo`,`targa`,`km`,`telaio`,`cliente`,`indirizzo`,`piva`,`tel`,`itr1`,`itr2`,`itr3`,`itr4`,`itr5`,`itr6`,`itr7`,`itr8`,`itr9`,`itr10`,`itr11`,`itr12`,`itr13`,`itr14`,`itr15`,`for1`,`for2`,`for3`,`for4`,`for5`,`for6`,`for7`,`for8`,`for9`,`for10`,`for11`,`for12`,`for13`,`for14`,`for15`,`imp_1`,`imp_2`,`imp_3`,`imp_4`,`imp_5`,`imp_6`,`imp_7`,`imp_8`,`imp_9`,`imp_10`,`imp_11`,`imp_12`,`imp_13`,`imp_14`,`imp_15`,`totale`,`n_tecnico`,`data`)
VALUES 
(('$veicolo'),('$targa'),'$km',('$telaio'),('$cliente'),('$indirizzo'),'$piva','$tel',('$itr1'),('$itr2'),('$itr3'),('$itr4'),('$itr5'),('$itr6'),('$itr7'),('$itr8'),('$itr9'),('$itr10'),('$itr11'),('$itr12'),('$itr13'),('$itr14'),('$itr15'),('$for1'),('$for2'),('$for3'),('$for4'),('$for5'),('$for6'),('$for7'),('$for8'),('$for9'),('$for10'),('$for11'),('$for12'),('$for13'),('$for14'),('$for15'),$imp_1, $imp_2, $imp_3, $imp_4, $imp_5, $imp_6, $imp_7, $imp_8, $imp_9, $imp_10, $imp_11, $imp_12, $imp_13, $imp_14, $imp_15, $totale,('$n_tecnico'),STR_TO_DATE('$data', '%d/%m/%Y'))";


// Lanciamo la query:
$result = mysql_query($sql);



// controllo l'esito
if (!$result) {
	die("Errore nella query $sql: " . mysql_error());
}

else
echo '</br>';
echo '</br>';
echo '</br>';
echo "<center><h3><font color=\"red\">I dati da te inseriti sono stati correttamente registrati</center>\n"; 
echo "<center><h3><font color=\"red\">Prosegui con il menù a destra</h3></center>\n";
echo '</br>';

// E chiudiamo la connessione a MySQL
mysql_close();

?>

Vorrei fare in modo che dopo l'inserimento il programma recuperasse l'id appena registrato e lo passasse alla pagina stampa.php per generare la stampa del documento

questo il codice del file stampa.php

Codice:
// INCLUDO IL FILE PER LA CONNESSIONE AL DATABASE
include ("conn.php");

//Preparo la query di modifica
$sql = "SELECT * , DATE_FORMAT(Commesse.data,'%d/%m/%Y') as data FROM Commesse WHERE id='$_SESSION[id]'";

// Lanciamo la query:
$result = mysql_query($sql);
 
if (mysql_num_rows($result) == 0)
 {
 echo "Non ho trovato niente con questo id: ".$_SESSION['id'];
 echo "<br />";
 echo "La query eseguita è: ".$sql;
 exit;
 }
 

$dati = mysql_fetch_assoc ($result);

$id = $dati ['id'];
$veicolo = $dati['veicolo'];
$targa = $dati['targa']; 
$km = $dati['km'];
$telaio = $dati['telaio']; 
$cliente = $dati['cliente']; 
$indirizzo = $dati['indirizzo']; 
$piva = $dati['piva']; 
$tel = $dati['tel']; 
$itr1 = $dati['itr1'];
$itr2 = $dati['itr2'];
$itr3 = $dati['itr3'];
$itr4 = $dati['itr4']; 
$itr5 = $dati['itr5']; 
$itr6 = $dati['itr6'];
$itr7 = $dati['itr7']; 
$itr8 = $dati['itr8'];
$itr9 = $dati['itr9']; 
$itr10 = $dati['itr10'];
$itr11 = $dati['itr11'];
$itr12 = $dati['itr12']; 
$itr13 = $dati['itr13'];
$itr14 = $dati['itr14'];
$itr15 = $dati['itr15'];
$for1 = $dati['for1']; 
$for2 = $dati['for2'];
$for3 = $dati['for3'];
$for4 = $dati['for4'];
$for5 = $dati['for5'];
$for6 = $dati['for6'];
$for7 = $dati['for7']; 
$for8 = $dati['for8']; 
$for9 = $dati['for9']; 
$for10 = $dati['for10']; 
$for11 = $dati['for11']; 
$for12 = $dati['for12']; 
$for13 = $dati['for13']; 
$for14 = $dati['for14']; 
$for15 = $dati['for15']; 
$imp_1 = $dati['imp_1']; 
$imp_2 = $dati['imp_2']; 
$imp_3 = $dati['imp_3']; 
$imp_4 = $dati['imp_4']; 
$imp_5 = $dati['imp_5']; 
$imp_6 = $dati['imp_6']; 
$imp_7 = $dati['imp_7']; 
$imp_8 = $dati['imp_8']; 
$imp_9 = $dati['imp_9']; 
$imp_10 = $dati['imp_10']; 
$imp_11 = $dati['imp_11']; 
$imp_12 = $dati['imp_12']; 
$imp_13 = $dati['imp_13']; 
$imp_14 = $dati['imp_14']; 
$imp_15 = $dati['imp_15']; 
$totale = $dati['totale']; 
$n_tecnico = $dati['n_tecnico']; 
$data = $dati['data'];   

// controllo l'esito
if (!$result) {
	die("Errore nella query $sql: " . mysql_error());
}

// E chiudiamo la connessione a MySQL
mysql_close();

?>

<FORM name Commessa method="POST" action="../stampa.php">
<fieldset>
  <legend>Anagrafica Cliente</legend>
  <div id="anagrafica">
    <table>
      <tr>
        <td width="99">Id</td>
        <td width="495"><input name="id" disabled="disabled" id="id" value="<?php echo $id ?>" size="10" readonly="readonly"></td>
        <td width="99">Data Intervento</td>
        <td><input type="text"  name="data" class="datepicker" required="required" id="data" value="<?php echo $data ?>"></td>
        </tr>
      <tr>
        <td>Cliente</td>
        <td><input name="cliente" class="uppercase" id="cliente" value="<?php echo $cliente ?>"></td>
		<td>Indirizzo</td>
        <td><input name="indirizzo" class="uppercase" id="indirizzo" value="<?php echo $indirizzo ?>"></td>
        </tr>
      <tr>
        <td>Partita Iva</td>
        <td><input name="piva" class="uppercase" id="piva" maxlength="12" value="<?php echo $piva ?>"></td>
        <td>Telefono</td>
        <td><input name="tel" class="uppercase" id="tel" value="<?php echo $tel ?>"></td>
		</tr>
    </table>
</fieldset>

<fieldset>
  <legend>Anagrafica Vettura</legend>
    <table>
      <td width="99">Veicolo</td>
        <td width="145"><input name="veicolo" class="uppercase" id="veicolo" value="<?php echo $veicolo ?>"></td>
		<td>Targa</td>
        <td><input name="targa" required="required" class="uppercase" id="targa" value="<?php echo $targa ?>"></td>
        </tr>
      <tr>
        <td>Telaio</td>
        <td><input name="telaio" class="uppercase" id="telaio" value="<?php echo $telaio ?>"></td>
		<td>Km</td>
        <td width="495"><input name="km" class="uppercase" id="km" value="<?php echo $km ?>"></td>
        </tr>
      <tr>
        <td width="99">Nome tecnico</td>
        <td width="278"><input name="n_tecnico" required="required" class="uppercase" id="n_tecnico" value="<?php echo $n_tecnico ?>"></td>
        </tr>
    </table>
</fieldset>
<fieldset>
<legend>Interventi di manutenzione veicolo</legend>
<table>
        <td>Intervento</td>
        <td>Fornitore</td>
      	<td>Importo</td>
        </tr>
      <tr>
        </tr>
      <tr>
        <td><input name="itr1" class="uppercase" id="itr1" size="40" value="<?php echo $itr1 ?>"></td>
        <td><input name="for1" class="uppercase" id="for1" size="25" value="<?php echo $for1 ?>"></td>
        <td><input name="imp_1" id="imp_1" size="10" value="<?php echo $imp_1 ?>"></td>
        </tr>
      <tr>
        <td><input name="itr2" class="uppercase" id="itr2" size="40" value="<?php echo $itr2 ?>"></td>
        <td><input name="for2" class="uppercase" id="for2" size="25" value="<?php echo $for2 ?>"></td>
        <td><input name="imp_2" id="imp_2" size="10" value="<?php echo $imp_2 ?>"></td>
        </tr>
        
        <tr>
        <td><input name="itr3" class="uppercase" id="itr3" size="40" value="<?php echo $itr3 ?>"></td>
        <td><input name="for3" class="uppercase" id="for3" size="25" value="<?php echo $for3 ?>"></td>
        <td><input name="imp_3" id="imp_3" size="10" value="<?php echo $imp_3 ?>"></td>
        </tr>
        
        <tr>
        <td><input name="itr4" class="uppercase" id="itr4" size="40" value="<?php echo $itr4 ?>"></td>
        <td><input name="for4" class="uppercase" id="for4" size="25" value="<?php echo $for4 ?>"></td>
        <td><input name="imp_4" id="imp_4" size="10" value="<?php echo $imp_4 ?>"></td>
        </tr>
        
        <tr>
        <td><input name="itr5" class="uppercase" id="itr5" size="40" value="<?php echo $itr5 ?>"></td>
        <td><input name="for5" class="uppercase" id="for5" size="25" value="<?php echo $for5 ?>"></td>
        <td><input name="imp_5" id="imp_5" size="10" value="<?php echo $imp_5 ?>"></td>
        </tr> 
        
        <tr>
        <td><input name="itr6" class="uppercase" id="itr6" size="40" value="<?php echo $itr6 ?>"></td>
        <td><input name="for6" class="uppercase" id="for6" size="25" value="<?php echo $for6 ?>"></td>
        <td><input name="imp_6" id="imp_6" size="10" value="<?php echo $imp_6 ?>"></td>
        </tr>
        
        <tr>
        <td><input name="itr7" class="uppercase" id="itr7" size="40" value="<?php echo $itr7 ?>"></td>
        <td><input name="for7" class="uppercase" id="for7" size="25" value="<?php echo $for7 ?>"></td>
        <td><input name="imp_7" id="imp_7" size="10" value="<?php echo $imp_7 ?>"></td>
        </tr>    
        
        <tr>
        <td><input name="itr8" class="uppercase" id="itr8" size="40" value="<?php echo $itr8 ?>"></td>
        <td><input name="for8" class="uppercase" id="for8" size="25" value="<?php echo $for8 ?>"></td>
        <td><input name="imp_8" id="imp_8" size="10" value="<?php echo $imp_8 ?>"></td>
        </tr>
        
        <tr>
        <td><input name="itr9" class="uppercase" id="itr9" size="40" value="<?php echo $itr9 ?>"></td>
        <td><input name="for9" class="uppercase" id="for9" size="25" value="<?php echo $for9 ?>"></td>
        <td><input name="imp_9" id="imp_9" size="10" value="<?php echo $imp_9 ?>"></td>
        </tr> 
        
        <tr>
        <td><input name="itr10" class="uppercase" id="itr10" size="40" value="<?php echo $itr10 ?>"></td>
        <td><input name="for10" class="uppercase" id="for10" size="25" value="<?php echo $for10 ?>"></td>
        <td><input name="imp_10" id="imp_10" size="10" value="<?php echo $imp_10 ?>"></td>
        </tr>
         <tr>
        <td><input name="itr11" class="uppercase"  id="itr11" size="40" value="<?php echo $itr11 ?>"></td>
        <td><input name="for11" class="uppercase" id="for11" size="25" value="<?php echo $for11 ?>"></td>
        <td><input name="imp_11" id="imp_11" size="10" value="<?php echo $imp_11 ?>"></td>
        </tr>
        <tr>
        <td><input name="itr12" class="uppercase"  id="itr12" size="40" value="<?php echo $itr12 ?>"></td>
        <td><input name="for12" class="uppercase" id="for12" size="25" value="<?php echo $for12 ?>"></td>
        <td><input name="imp_12" id="imp_12" size="10" value="<?php echo $imp_12 ?>"></td>
        </tr>
        <tr>
        <td><input name="itr13" class="uppercase"  id="itr13" size="40" value="<?php echo $itr13 ?>"></td>
        <td><input name="for13" class="uppercase" id="for13" size="25" value="<?php echo $for13 ?>"></td>
        <td><input name="imp_13" id="imp_13" size="10" value="<?php echo $imp_13 ?>"></td>
        </tr>
        <tr>
        <td><input name="itr14" class="uppercase"  id="itr14" size="40" value="<?php echo $itr14 ?>"></td>
        <td><input name="for14" class="uppercase" id="for14" size="25" value="<?php echo $for14 ?>"></td>
        <td><input name="imp_14" id="imp_14" size="10" value="<?php echo $imp_14 ?>"></td>
        </tr>
         <tr>
        <td><input name="itr15" class="uppercase"  id="itr15" size="40" value="<?php echo $itr15 ?>"></td>
        <td><input name="for15" class="uppercase" id="for15" size="25" value="<?php echo $for15 ?>"></td>
        <td><input name="imp_15" id="imp_15" size="10" value="<?php echo $imp_15 ?>"></td>
        </tr> 
          <td></td> 
          <td align="center"><input type="button" value="Calcola Totale" onClick="javascript:sum();"></td> 	
          <td><input name="totale" type="text" required="required" size="10" value="<?php echo $totale ?>"> </td>         
        </table>
</fieldset>
<div id="salva">
<input type="image" src="css/img/pdf.png" height="46" width="46" />
</div>
</FORM>
</div> <!-- chiudo il div container -->
</body>
</html>

:(
 
Ciao, prima due consigli:
1 - puoi evitare di scrivere tutto questo codice
PHP:
//Effettuo l'Escape dei campi _POST 

foreach ($_POST as $key => $value) {
$_POST[$key] = mysql_real_escape_string($value);
}

// EFFETTUO L'UPPER CASE DEI CAMPI INSERITI

foreach ($_POST as $key => $value) { 
    $_POST[$key] = strtoupper($value); 
} 

$veicolo = $_POST['veicolo'];
$targa = $_POST['targa']; 
$km = $_POST['km'];
$telaio = $_POST['telaio']; 
$cliente = $_POST['cliente']; 
$indirizzo = $_POST['indirizzo']; 
$piva = $_POST['piva']; 
$tel = $_POST['tel']; 
$itr1 = $_POST['itr1'];
$itr2 = $_POST['itr2'];
$itr3 = $_POST['itr3'];
$itr4 = $_POST['itr4']; 
$itr5 = $_POST['itr5']; 
$itr6 = $_POST['itr6'];
$itr7 = $_POST['itr7']; 
$itr8 = $_POST['itr8'];
$itr9 = $_POST['itr9']; 
$itr10 = $_POST['itr10'];
$itr11 = $_POST['itr11'];
$itr12 = $_POST['itr12']; 
$itr13 = $_POST['itr13'];
$itr14 = $_POST['itr14'];
$itr15 = $_POST['itr15'];
$for1 = $_POST['for1']; 
$for2 = $_POST['for2'];
$for3 = $_POST['for3'];
$for4 = $_POST['for4'];
$for5 = $_POST['for5'];
$for6 = $_POST['for6'];
$for7 = $_POST['for7']; 
$for8 = $_POST['for8']; 
$for9 = $_POST['for9']; 
$for10 = $_POST['for10']; 
$for11 = $_POST['for11']; 
$for12 = $_POST['for12']; 
$for13 = $_POST['for13']; 
$for14 = $_POST['for14']; 
$for15 = $_POST['for15']; 
$imp_1 = $_POST['imp_1']; 
$imp_2 = $_POST['imp_2']; 
$imp_3 = $_POST['imp_3']; 
$imp_4 = $_POST['imp_4']; 
$imp_5 = $_POST['imp_5']; 
$imp_6 = $_POST['imp_6']; 
$imp_7 = $_POST['imp_7']; 
$imp_8 = $_POST['imp_8']; 
$imp_9 = $_POST['imp_9']; 
$imp_10 = $_POST['imp_10']; 
$imp_11 = $_POST['imp_11']; 
$imp_12 = $_POST['imp_12']; 
$imp_13 = $_POST['imp_13']; 
$imp_14 = $_POST['imp_14']; 
$imp_15 = $_POST['imp_15']; 
$totale = $_POST['totale']; 
$n_tecnico = $_POST['n_tecnico']; 
$data = $_POST['data'];
sostituendolo con questo
PHP:
foreach ($_POST as $key => $value) {
    ${$key} = mysql_real_escape_string(strtoupper($value));
}
2 - l'estensione mysql è deprecata, meglio se inizi ad usare mysqli

Per la tua domanda puoi recuperare l'ultimo id inserito con mysql_insert_id() dopo la INSERT e rimandare alla pagina stampa con header()
PHP:
header("location:stampa.php?id=" . mysql_insert_id());
 
Ciao criric,
nel ringraziarti per la risposta ti dico che il tuo suggerimento per il recupero dei campi post funziona egregiamente, il problema continuo ad averlo con il "Salva e Stampa", ho modificato il codice in questo modo :

Codice:
<?php

// INCLUDO IL FILE PER LA CONNESSIONE AL DATABASE
include ("conn.php");


// RECUPERO TUTTI I CAMPI POST DEL FORM E FACCIO L'ESCAPE E L'UPPER DEI CARATTERI
foreach ($_POST as $key => $value) {
    ${$key} = mysql_real_escape_string(strtoupper($value));
}


// QUERY DI INSERIMENTO SENZA UPPER, L'UPPER E' DEMANDATO AL COMANDO FOREACH
$sql = "INSERT INTO `Commesse` (`veicolo`,`targa`,`km`,`telaio`,`cliente`,`indirizzo`,`piva`,`tel`,`itr1`,`itr2`,`itr3`,`itr4`,`itr5`,`itr6`,`itr7`,`itr8`,`itr9`,`itr10`,`itr11`,`itr12`,`itr13`,`itr14`,`itr15`,`for1`,`for2`,`for3`,`for4`,`for5`,`for6`,`for7`,`for8`,`for9`,`for10`,`for11`,`for12`,`for13`,`for14`,`for15`,`q1`,`q2`,`q3`,`q4`,`q5`,`q6`,`q7`,`q8`,`q9`,`q10`,`q11`,`q12`,`q13`,`q14`,`q15`,`iu1`,`iu2`,`iu3`,`iu4`,`iu5`,`iu6`,`iu7`,`iu8`,`iu9`,`iu10`,`iu11`,`iu12`,`iu13`,`iu14`,`iu15`,`imp_1`,`imp_2`,`imp_3`,`imp_4`,`imp_5`,`imp_6`,`imp_7`,`imp_8`,`imp_9`,`imp_10`,`imp_11`,`imp_12`,`imp_13`,`imp_14`,`imp_15`,`totale`,`n_tecnico`,`data`)
VALUES 
(('$veicolo'),('$targa'),'$km',('$telaio'),('$cliente'),('$indirizzo'),'$piva','$tel',('$itr1'),('$itr2'),('$itr3'),('$itr4'),('$itr5'),('$itr6'),('$itr7'),('$itr8'),('$itr9'),('$itr10'),('$itr11'),('$itr12'),('$itr13'),('$itr14'),('$itr15'),('$for1'),('$for2'),('$for3'),('$for4'),('$for5'),('$for6'),('$for7'),('$for8'),('$for9'),('$for10'),('$for11'),('$for12'),('$for13'),('$for14'),('$for15'),$q1,$q2,$q3,$q4,$q5,$q6,$q7,$q8,$q9,$q10,$q11,$q12,$q13,$q14,$q15,$iu1,$iu2,$iu3,$iu4,$iu5,$iu6,$iu7,$iu8,$iu9,$iu10,$iu11,$iu12,$iu13,$iu14,$iu15,$imp_1, $imp_2, $imp_3, $imp_4, $imp_5, $imp_6, $imp_7, $imp_8, $imp_9, $imp_10, $imp_11, $imp_12, $imp_13, $imp_14, $imp_15, $totale,('$n_tecnico'),STR_TO_DATE('$data', '%d/%m/%Y'))";


// Lanciamo la query:
$result = mysql_query($sql);

// RIMANDO ALLA PAGINA DI STAMPA RECUPERANDO L'ID COMMESSA
header("location:stampa.php?id=" . mysql_insert_id()); 

// Stampo la query
// echo $sql;  

// controllo l'esito
if (!$result) {
	die("Errore nella query $sql: " . mysql_error());
}

else

echo '</br>';
echo '</br>';
echo '</br>';
echo "<center><h3><font color=\"red\">I dati da te inseriti sono stati correttamente registrati</center>\n"; 
echo "<center><h3><font color=\"red\">Prosegui con il menu' a destra</h3></center>"; 
echo '</br>';

// E chiudiamo la connessione a MySQL
mysql_close();

?>

ma effettua l'inserimento e non va alla pagina stampa.php
 
Ho provato così

Codice:
// Lanciamo la query:
$result = mysql_query($sql);
var_dump(mysql_insert_id());
printf ("L'ultimo recod inserito ha l'identificativo %d\n", mysql_insert_id());

// RIMANDO ALLA PAGINA DI STAMPA RECUPERANDO L'ID COMMESSA
header("Location:pstampa.php?id=".mysql_insert_id()); 
exit;

Recupera correttamente l'id ma non effettua il redirect :(
 
La funzione header() non accetta output prima del suo utilizzo e probabilmente stampi qualcosa.
utilizza javascript
PHP:
echo "<script type='text/javascript'>";
echo "location.href='pstampa.php?id=" . mysql_insert_id() . "';";
echo "</script>";
 
La funzione header() non accetta output prima del suo utilizzo e probabilmente stampi qualcosa.
utilizza javascript
PHP:
echo "<script type='text/javascript'>";
echo "location.href='pstampa.php?id=" . mysql_insert_id() . "';";
echo "</script>";
.

Ciao criric,
nel ringraziarti per la risposta ti confermo che grazie al tuo suggerimento ora il redirect funziona egregiamente, il problema molto probabilmente adesso è nella pagina "pstampa.php".

In sostanza la pagina dovrebbe mostrare all'utente la form appena inserita con il tasto "Stampa", sulla barra dell'indirizzo l'id è corretto (ossia l'ultimo) http://www.miosito.com/pstampa.php?id=154 ma non viene recuperata la form per il preview

questa la pagina pstampa.php

Codice:
<?php
session_start();
$_SESSION['id'] = mysql_insert_id();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Commessa</title>
</head>

<body>
<script type="text/javascript">
$(function(){
     $.datepicker.setDefaults( $.datepicker.regional[ "it" ] );
     $('.datepicker').datepicker();
});
</script>

<div id="container"> <!-- apro il div container -->
<?php

/// INCLUDO IL FILE PER LA CONNESSIONE AL DATABASE
include ("conn.php");

//Preparo la query di modifica
$sql = "SELECT * , DATE_FORMAT(Commesse.data,'%d/%m/%Y') as data FROM Commesse WHERE id='$_SESSION[id]'";

// Lanciamo la query:
$result = mysql_query($sql);
 
if (mysql_num_rows($result) == 0)
 {
 echo "Non ho trovato niente con questo id: ".$_SESSION['id'];
 echo "<br />";
 echo "La query eseguita è: ".$sql;
 exit;
 }
 

$dati = mysql_fetch_assoc ($result);

$id = $dati ['id'];
$veicolo = $dati['veicolo'];
$targa = $dati['targa']; 
$km = $dati['km'];
$telaio = $dati['telaio']; 
$cliente = $dati['cliente']; 
$indirizzo = $dati['indirizzo']; 
$piva = $dati['piva']; 
$tel = $dati['tel']; 
$itr1 = $dati['itr1'];
$itr2 = $dati['itr2'];
$itr3 = $dati['itr3'];
$itr4 = $dati['itr4']; 
$itr5 = $dati['itr5']; 
$itr6 = $dati['itr6'];
$itr7 = $dati['itr7']; 
$itr8 = $dati['itr8'];
$itr9 = $dati['itr9']; 
$itr10 = $dati['itr10'];
$itr11 = $dati['itr11'];
$itr12 = $dati['itr12']; 
$itr13 = $dati['itr13'];
$itr14 = $dati['itr14'];
$itr15 = $dati['itr15'];
$for1 = $dati['for1']; 
$for2 = $dati['for2'];
$for3 = $dati['for3'];
$for4 = $dati['for4'];
$for5 = $dati['for5'];
$for6 = $dati['for6'];
$for7 = $dati['for7']; 
$for8 = $dati['for8']; 
$for9 = $dati['for9']; 
$for10 = $dati['for10']; 
$for11 = $dati['for11']; 
$for12 = $dati['for12']; 
$for13 = $dati['for13']; 
$for14 = $dati['for14']; 
$for15 = $dati['for15'];
$q1 = $dati['q1']; 
$q2 = $dati['q2'];
$q3 = $dati['q3'];
$q4 = $dati['q4'];
$q5 = $dati['q5'];
$q6 = $dati['q6'];
$q7 = $dati['q7'];
$q8 = $dati['q8'];
$q9 = $dati['q9'];
$q10 = $dati['q10'];
$q11 = $dati['q11'];
$q12 = $dati['q12'];
$q13 = $dati['q13'];
$q14 = $dati['q14'];
$q15 = $dati['q15'];
$iu1 = $dati['iu1'];
$iu2 = $dati['iu2'];
$iu3 = $dati['iu3'];
$iu4 = $dati['iu4'];
$iu5 = $dati['iu5'];
$iu6 = $dati['iu6'];
$iu7 = $dati['iu7'];
$iu8 = $dati['iu8'];
$iu9 = $dati['iu9'];
$iu10 = $dati['iu10'];
$iu11 = $dati['iu11'];
$iu12 = $dati['iu12'];
$iu13 = $dati['iu13'];
$iu14 = $dati['iu14'];
$iu15 = $dati['iu15'];
$imp_1 = $dati['imp_1']; 
$imp_2 = $dati['imp_2']; 
$imp_3 = $dati['imp_3']; 
$imp_4 = $dati['imp_4']; 
$imp_5 = $dati['imp_5']; 
$imp_6 = $dati['imp_6']; 
$imp_7 = $dati['imp_7']; 
$imp_8 = $dati['imp_8']; 
$imp_9 = $dati['imp_9']; 
$imp_10 = $dati['imp_10']; 
$imp_11 = $dati['imp_11']; 
$imp_12 = $dati['imp_12']; 
$imp_13 = $dati['imp_13']; 
$imp_14 = $dati['imp_14']; 
$imp_15 = $dati['imp_15']; 
$totale = $dati['totale']; 
$n_tecnico = $dati['n_tecnico']; 
$data = $dati['data']; 

// controllo l'esito
if (!$result) {
	die("Errore nella query $sql: " . mysql_error());
}

// E chiudiamo la connessione a MySQL
mysql_close();

?>

<FORM name InserisciCommessaOfficina method="POST" action="http://www.fiatsalzano.com/web/sv/stampa.php">
<fieldset>

<!-- ANAGRAFICA CLIENTE -->

  <legend>Anagrafica Cliente</legend>
  <div id="anagrafica">
    <table>
      <tr>
        <td width="99">Id</td>
        <td width="495"><input name="id" disabled="disabled" id="id" value="<?php echo $id ?>" size="10" readonly="readonly"></td>
        <td width="99">Data  gg/mm/aaaa</td>
        <td><input type="text"  name="data" class="datepicker" required="required" id="data" value="<?php echo $data ?>"></td>
        </tr>
      <tr>
        <td>Cliente</td>
        <td><input name="cliente" class="uppercase" id="cliente" value="<?php echo $cliente ?>"></td>
        </tr>
      <tr>
        <td>Indirizzo</td>
        <td><input name="indirizzo" class="uppercase" id="indirizzo" value="<?php echo $indirizzo ?>"></td>
        </tr>
      <tr>
        <td>Partita Iva</td>
        <td><input name="piva" class="uppercase" id="piva" maxlength="12" value="<?php echo $piva ?>"></td>
        </tr>
      <tr>
        <td>Telefono</td>
        <td><input name="tel" class="uppercase" id="tel" value="<?php echo $tel ?>"></td>
        </tr>
    </table>
</fieldset>

<fieldset>

<!-- ANAGRAFICA VETTURA -->

  <legend>Anagrafica Vettura</legend>
    <table>
      <td width="99">Veicolo</td>
        <td width="145"><input name="veicolo" class="uppercase" id="veicolo" value="<?php echo $veicolo ?>"></td>
        </tr>
      <tr>
        <td>Targa</td>
        <td><input name="targa" required="required" class="uppercase" id="targa" value="<?php echo $targa ?>"></td>
        </tr>
      <tr>
        <td>Telaio</td>
        <td><input name="telaio" class="uppercase" id="telaio" value="<?php echo $telaio ?>"></td>
        </tr>
      <tr>
        <td>Km</td>
        <td width="495"><input name="km" class="uppercase" id="km" value="<?php echo $km ?>"></td>
        <td width="99">Nome tecnico</td>
        <td width="278"><input name="n_tecnico" required="required" class="uppercase" id="n_tecnico" value="<?php echo $n_tecnico ?>"></td>
        </tr>
    </table>
</fieldset>
<fieldset>

<!-- INIZIO IL CODICE PER GLI INTERVENTI DI MANUTENZIONE -->

<legend>Interventi di manutenzione veicolo</legend>
<table>
        <td>Intervento</td>
        <td>Fornitore</td>
		<td>Q.ta</td>
		<td>Unitario</td>
      	<td>Totale</td>
        </tr>
      <tr>
        </tr>
      <tr>
        <td><input name="itr1" class="uppercase" id="itr1" size="40" value="<?php echo $itr1 ?>"></td>
        <td><input name="for1" class="uppercase" id="for1" size="15" value="<?php echo $for1 ?>"></td>
		<td><input name="q1" class="uppercase factor" id="q1" size="2" value="<?php if ($q1 <> "0"){ echo $q1;} else { echo ""; } ?>"</td>
		<td><input name="iu1" class="uppercase factor" id="iu1" size="2" value="<?php if ($iu1 <> "0.00"){ echo $iu1;} else { echo ""; } ?>"</td>
        <td><input name="imp_1" id="imp_1" size="4" value="<?php if ($imp_1 <> "0.00"){ echo $imp_1;} else { echo ""; } ?>"</td>
        </tr>
      <tr>
        <td><input name="itr2" class="uppercase" id="itr2" size="40" value="<?php echo $itr2 ?>"></td>
        <td><input name="for2" class="uppercase" id="for2" size="15" value="<?php echo $for2 ?>"></td>
		<td><input name="q2" class="uppercase factor" id="q2" size="2" value="<?php if ($q2 <> "0"){ echo $q2;} else { echo ""; } ?>"</td>
		<td><input name="iu2" class="uppercase factor" id="iu2" size="2" value="<?php if ($iu2 <> "0.00"){ echo $iu2;} else { echo ""; } ?>"</td>
        <td><input name="imp_2" id="imp_2" size="4" value="<?php if ($imp_2 <> "0.00"){ echo $imp_2;} else { echo ""; } ?>"</td>
        </tr>
        
        <tr>
        <td><input name="itr3" class="uppercase" id="itr3" size="40" value="<?php echo $itr3 ?>"></td>
        <td><input name="for3" class="uppercase" id="for3" size="15" value="<?php echo $for3 ?>"></td>
		<td><input name="q3" class="uppercase factor" id="q3" size="2" value="<?php if ($q3 <> "0"){ echo $q3;} else { echo ""; } ?>"</td>
		<td><input name="iu3" class="uppercase factor" id="iu3" size="2" value="<?php if ($iu3 <> "0.00"){ echo $iu3;} else { echo ""; } ?>"</td>
        <td><input name="imp_3" id="imp_3" size="4" value="<?php if ($imp_3 <> "0.00"){ echo $imp_3;} else { echo ""; } ?>"</td>
        </tr>
        
        <tr>
        <td><input name="itr4" class="uppercase" id="itr4" size="40" value="<?php echo $itr4 ?>"></td>
        <td><input name="for4" class="uppercase" id="for4" size="15" value="<?php echo $for4 ?>"></td>
		<td><input name="q4" class="uppercase factor" id="q4" size="2" value="<?php if ($q4 <> "0"){ echo $q4;} else { echo ""; } ?>"</td>
		<td><input name="iu4" class="uppercase factor" id="iu4" size="2" value="<?php if ($iu4 <> "0.00"){ echo $iu4;} else { echo ""; } ?>"</td>
        <td><input name="imp_4" id="imp_4" size="4" value="<?php if ($imp_4 <> "0.00"){ echo $imp_4;} else { echo ""; } ?>"</td>
        </tr>
        
        <tr>
        <td><input name="itr5" class="uppercase" id="itr5" size="40" value="<?php echo $itr5 ?>"></td>
        <td><input name="for5" class="uppercase" id="for5" size="15" value="<?php echo $for5 ?>"></td>
		<td><input name="q5" class="uppercase factor" id="q5" size="2" value="<?php if ($q5 <> "0"){ echo $q5;} else { echo ""; } ?>"</td>
		<td><input name="iu5" class="uppercase factor" id="iu5" size="2" value="<?php if ($iu5 <> "0.00"){ echo $iu5;} else { echo ""; } ?>"</td>
        <td><input name="imp_5" id="imp_5" size="4" value="<?php if ($imp_5 <> "0.00"){ echo $imp_5;} else { echo ""; } ?>"</td>
        </tr> 
        
        <tr>
        <td><input name="itr6" class="uppercase" id="itr6" size="40" value="<?php echo $itr6 ?>"></td>
        <td><input name="for6" class="uppercase" id="for6" size="15" value="<?php echo $for6 ?>"></td>
		<td><input name="q6" class="uppercase factor" id="q6" size="2" value="<?php if ($q6 <> "0"){ echo $q6;} else { echo ""; } ?>"</td>
		<td><input name="iu6" class="uppercase factor" id="iu6" size="2" value="<?php if ($iu6 <> "0.00"){ echo $iu6;} else { echo ""; } ?>"</td>
        <td><input name="imp_6" id="imp_6" size="4" value="<?php if ($imp_6 <> "0.00"){ echo $imp_6;} else { echo ""; } ?>"</td>
        </tr>
        
        <tr>
        <td><input name="itr7" class="uppercase" id="itr7" size="40" value="<?php echo $itr7 ?>"></td>
        <td><input name="for7" class="uppercase" id="for7" size="15" value="<?php echo $for7 ?>"></td>
		<td><input name="q7" class="uppercase factor" id="q7" size="2" value="<?php if ($q7 <> "0"){ echo $q7;} else { echo ""; } ?>"</td>
		<td><input name="iu7" class="uppercase factor" id="iu7" size="2" value="<?php if ($iu7 <> "0.00"){ echo $iu7;} else { echo ""; } ?>"</td>
        <td><input name="imp_7" id="imp_7" size="4" value="<?php if ($imp_7 <> "0.00"){ echo $imp_7;} else { echo ""; } ?>"</td>
        </tr>    
        
        <tr>
        <td><input name="itr8" class="uppercase" id="itr8" size="40" value="<?php echo $itr8 ?>"></td>
        <td><input name="for8" class="uppercase" id="for8" size="15" value="<?php echo $for8 ?>"></td>
		<td><input name="q8" class="uppercase factor" id="q8" size="2" value="<?php if ($q8 <> "0"){ echo $q8;} else { echo ""; } ?>"</td>
		<td><input name="iu8" class="uppercase factor" id="iu8" size="2" value="<?php if ($iu8 <> "0.00"){ echo $iu8;} else { echo ""; } ?>"</td>
        <td><input name="imp_8" id="imp_8" size="4" value="<?php if ($imp_8 <> "0.00"){ echo $imp_8;} else { echo ""; } ?>"</td>
        </tr>
        
        <tr>
        <td><input name="itr9" class="uppercase" id="itr9" size="40" value="<?php echo $itr9 ?>"></td>
        <td><input name="for9" class="uppercase" id="for9" size="15" value="<?php echo $for9 ?>"></td>
		<td><input name="q9" class="uppercase factor" id="q9" size="2" value="<?php if ($q9 <> "0"){ echo $q9;} else { echo ""; } ?>"</td>
		<td><input name="iu9" class="uppercase factor" id="iu9" size="2" value="<?php if ($iu9 <> "0.00"){ echo $iu9;} else { echo ""; } ?>"</td>
        <td><input name="imp_9" id="imp_9" size="4" value="<?php if ($imp_9 <> "0.00"){ echo $imp_9;} else { echo ""; } ?>"</td>
        </tr> 
        
        <tr>
        <td><input name="itr10" class="uppercase" id="itr10" size="40" value="<?php echo $itr10 ?>"></td>
        <td><input name="for10" class="uppercase" id="for10" size="15" value="<?php echo $for10 ?>"></td>
		<td><input name="q10" class="uppercase factor" id="q10" size="2" value="<?php if ($q10 <> "0"){ echo $q10;} else { echo ""; } ?>"</td>
		<td><input name="iu10" class="uppercase factor" id="iu10" size="2" value="<?php if ($iu10 <> "0.00"){ echo $iu10;} else { echo ""; } ?>"</td>
        <td><input name="imp_10" id="imp_10" size="4" value="<?php if ($imp_10 <> "0.00"){ echo $imp_10;} else { echo ""; } ?>"</td>
        </tr>
         <tr>
        <td><input name="itr11" class="uppercase"  id="itr11" size="40" value="<?php echo $itr11 ?>"></td>
        <td><input name="for11" class="uppercase" id="for11" size="15" value="<?php echo $for11 ?>"></td>
		<td><input name="q11" class="uppercase factor" id="q11" size="2" value="<?php if ($q11 <> "0"){ echo $q11;} else { echo ""; } ?>"</td>
		<td><input name="iu11" class="uppercase factor" id="iu11" size="2" value="<?php if ($iu11 <> "0.00"){ echo $iu11;} else { echo ""; } ?>"</td>
        <td><input name="imp_11" id="imp_11" size="4" value="<?php if ($imp_11 <> "0.00"){ echo $imp_11;} else { echo ""; } ?>"</td>
        </tr>
        <tr>
        <td><input name="itr12" class="uppercase"  id="itr12" size="40" value="<?php echo $itr12 ?>"></td>
        <td><input name="for12" class="uppercase" id="for12" size="15" value="<?php echo $for12 ?>"></td>
		<td><input name="q12" class="uppercase factor" id="q12" size="2" value="<?php if ($q12 <> "0"){ echo $q12;} else { echo ""; } ?>"</td>
		<td><input name="iu12" class="uppercase factor" id="iu12" size="2" value="<?php if ($iu12 <> "0.00"){ echo $iu12;} else { echo ""; } ?>"</td>
        <td><input name="imp_12" id="imp_12" size="4" value="<?php if ($imp_12 <> "0.00"){ echo $imp_12;} else { echo ""; } ?>"</td>
        </tr>
        <tr>
        <td><input name="itr13" class="uppercase"  id="itr13" size="40" value="<?php echo $itr13 ?>"></td>
        <td><input name="for13" class="uppercase" id="for13" size="15" value="<?php echo $for13 ?>"></td>
		<td><input name="q13" class="uppercase factor" id="q13" size="2" value="<?php if ($q13 <> "0"){ echo $q13;} else { echo ""; } ?>"</td>
		<td><input name="iu13" class="uppercase factor" id="iu13" size="2" value="<?php if ($iu13 <> "0.00"){ echo $iu13;} else { echo ""; } ?>"</td>
        <td><input name="imp_13" id="imp_13" size="4" value="<?php if ($imp_13 <> "0.00"){ echo $imp_13;} else { echo ""; } ?>"</td>
        </tr>
        <tr>
        <td><input name="itr14" class="uppercase"  id="itr14" size="40" value="<?php echo $itr14 ?>"></td>
        <td><input name="for14" class="uppercase" id="for14" size="15" value="<?php echo $for14 ?>"></td>
		<td><input name="q14" class="uppercase factor" id="q14" size="2" value="<?php if ($q14 <> "0"){ echo $q14;} else { echo ""; } ?>"</td>
		<td><input name="iu14" class="uppercase factor" id="iu14" size="2" value="<?php if ($iu14 <> "0.00"){ echo $iu14;} else { echo ""; } ?>"</td>
        <td><input name="imp_14" id="imp_14" size="4" value="<?php if ($imp_14 <> "0.00"){ echo $imp_14;} else { echo ""; } ?>"</td>
        </tr>
         <tr>
        <td><input name="itr15" class="uppercase"  id="itr15" size="40" value="<?php echo $itr15 ?>"></td>
        <td><input name="for15" class="uppercase" id="for15" size="15" value="<?php echo $for15 ?>"></td>
		<td><input name="q15" class="uppercase factor" id="q15" size="2" value="<?php if ($q15 <> "0"){ echo $q15;} else { echo ""; } ?>"</td>
		<td><input name="iu15" class="uppercase factor" id="iu15" size="2" value="<?php if ($iu15 <> "0.00"){ echo $iu15;} else { echo ""; } ?>"</td>
        <td><input name="imp_15" id="imp_15" size="4" value="<?php if ($imp_15 <> "0.00"){ echo $imp_15;} else { echo ""; } ?>"</td>
        </tr> 
		
		<!-- CALCOLO IL TOTALE CON JAVASCRIPT-->
		
          <td></td> 
          <!--td align="center"><input type="button" value="Calcola Totale" onClick="javascript:sum();"></td--> 	
		  <td></td>
		  <td></td>
		  <td></td>
          <td><input name="totale" type="text" required="required" size="4" value="<?php echo $totale ?>"> </td>         
        </table>
		
</fieldset>

<div id="salva">
<input type="image" src="css/img/pdf.png" height="46" width="46" />
</div>
</FORM>
</div> <!-- chiudo il div container -->
</body>
</html>

appena arriva al link http://www.miosito.com/pstampa.php?id=154 viene fuori il messaggio

Non ho trovato niente con questo id:
La query eseguita è: SELECT * , DATE_FORMAT(Commesse.data,'%d/%m/%Y') as data FROM CommesseOfficina WHERE id=''

:(
 
l'id non è in SESSION
Codice:
WHERE id='$_SESSION[id]'";
ma in GET
PHP:
WHERE id='" . $_GET['id'];
meglio cosi
PHP:
WHERE id='" . (int) $_GET['id'];
cosi sei sicuro che ti arriva un intero
 
Perfetto,
seguendo i vostri consigli ho risolto, anche se nel codice che mi hai passato risultava esserci un apice in più

Codice:
WHERE id='" . (int) $_GET['id'];

modificato in:

Codice:
WHERE id= " . (int) $_GET['id'];


:byebye:
 

Discussioni simili