Non picchiatemi!
Codice PHP:
<?php
// richiamo lo script responsabile della connessione a MySQL
include "../../connessione.php";
?>
<?php
if (isset($_POST['submit'])):
$nome = $_POST['nome'];
$descrizione = $_POST['descrizione'];
$prezzo = $_POST['prezzo'];
mysql_query( "INSERT INTO prodotti(`nome`,`descrizione`,`prezzo`) VALUES('".addslashes( $nome )."','".addslashes( $descrizione )."','".addslashes( $prezzo )."')" );
if (@mysql_query($sql)) {
echo "<script>alert('Inserimento eseguito con successo.');</script>";
echo "<script>document.location='../menu_amministrazione.php'</script>";
} else {
echo "<script>alert('Impossibile eseguire l'inserimento.');</script>";
echo "<script>document.location='../menu_amministrazione.php'</script>";
}
?>
<?php
else: // permetti all'utente di creare una categoria
?>
<?php
ini_set("memory_limit","256M");
$category = trim($_POST['category']);
?>
<?php
if(isset($_POST['inserisci_immagine'])){
if(isset($_FILES['immagine'])){
$imagename = $_FILES['immagine']['name'];
$source = $_FILES['immagine']['tmp_name'];
$target = "../../img/prodotti/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "../../img/prodotti/" . $imagepath; // questo è il nuovo file che salvi
$file = "../../img/prodotti/" . $imagepath; // questo è il file originale
list($width, $height) = getimagesize($file);
$modwidth = 1024; // dimensione immagine grande
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 90);
$save = "../../img/prodotti/miniatura-" . $imagepath; // questo è il nuovo file che salvi
$file = "../../img/prodotti/" . $imagepath; // questo è il file originale
list($width, $height) = getimagesize($file);
$max_w = 170;
$max_h = 120;
// area utile e offset
if ($width>$height*$max_w/$max_h) {
$a_h = $height;
$a_w = $a_h*$max_w/$max_h;
$delta_x = floor(($width-$a_w)/2);
$delta_y = 0;
}
else {
$a_w = $width;
$a_h = $a_w*$max_h/$max_w;
$delta_x = 0;
$delta_y = floor(($height-$a_h)/2);
}
$tn = imagecreatetruecolor($max_w, $max_h);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, $delta_x, $delta_y, $max_w, $max_h, $a_w, $a_h);
imagejpeg($tn, $save, 90);
$strSQL = "INSERT INTO immagini ";
$strSQL .="(nome_immagine,id_prodotto) VALUES ('$imagepath','$category')";
$objQuery = mysql_query($strSQL);
// inserimento eseguito con successo
echo "<script>alert('Inserimento eseguito con successo.');</script>";
echo "<script>document.location='../menu_amministrazione.php'</script>";
}
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Prodotti - Inserisci</title>
</head>
<body>
<table width="650" border="0" align="center" cellpadding="1" cellspacing="2">
<tr>
<td height="20" colspan="2" align="right" id="voce"><a href="../menu_amministrazione.php">Home</a> - <a href="../logout.php">Logout</a></td>
</tr>
<tr>
<td height="68" colspan="2"><img src="../immagini/header.jpg" width="650" height="100" alt="logo" /></td>
</tr>
<form action="" method="post" enctype="multipart/form-data" name="modulo_inserisci" id="modulo_inserisci" onsubmit="return validateForm()">
<tr>
<td colspan="2" valign="middle"><table width="100%" border="0">
<tr>
<td><table width="100%" border="0" cellspacing="0">
<tr>
<td colspan="2" id="intestazione"><div align="center">Prodotti - Inserisci</div></td>
</tr>
<tr>
<td colspan="2"><br /></td>
</tr>
<tr>
<td width="40%" colspan="1" id="voce" style="font-weight:bold"><div align="right">Nome: </div></td>
<td width="60%" id="voce"><div align="left"><input name="nome" type="text" id="nome" /></div></td>
</tr>
<tr>
<td width="40%" colspan="1" id="voce" style="font-weight:bold"><div align="right">Descrizione: </div></td>
<td width="60%" id="voce"><div align="left"><textarea name="descrizione" id="descrizione" cols="25" rows="10"></textarea></div></td>
</tr>
<tr>
<td width="40%" colspan="1" id="voce" style="font-weight:bold"><div align="right">Prezzo: </div></td>
<td width="60%" id="voce"><div align="left"><input name="prezzo" type="text" id="prezzo" /></div></td>
</tr>
<tr>
<td width="40%" colspan="1" id="voce" style="font-weight:bold"><div align="right">Immagine: </div></td>
<td width="60%" id="voce"><input name="immagine" id="immagine" size="30" type="file" /></div></td>
</tr>
<tr>
<td width="40%" colspan="1" id="voce" style="font-weight:bold"><div align="right"></div></td>
<td width="60%" id="voce"><div align="left"><br /> = A capo</div></td>
</tr>
<tr>
<td colspan="2"><br /></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input name="submit" type="submit" id="button_ok" value="Invia" /></div></td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
</form>
<tr>
<td height="6" colspan="2" bgcolor="#000000"></td>
</tr>
</table>
</body>
</html>
<?php endif; ?>
Questo è il codice ripulito da dove voglio ripartire, eventualmente prendendo qualche riga da quanto pubblicato prima.
Riesco a pubblicare nella tabella prodotti, ma non nella tabella immagini e non va l'upload singolo che ho messo al momento, una volta che riesco a fare l'upload singolo, vorrei aggiungere gli altri.
segnalo che $category = trim($_POST['category']); in un altro esempio mi serviva per caricare solo un'immagine in una determinata categoria scelta tramite un menu a tendina, in questo caso io nella tabella immagini devo poter associare al posto della categoria id_prodotto per "legare" le immagini alla scheda del prodotto.
Attendo vostri suggerimenti, grazie!