La pagina è divisa in questa maniera:
1) messa a schermo dell'articolo con la foto
2) form NASCOSTO con copiati al suo interno le variabili della tabella, le stesse messe a schermo sopra. Bottone INVIA
3) form VISIBILE per la modifica dei campi. Bottone ANTEPRIMA
I form vanno alla pagina prodottiupload.php e li se viene rilevato un GET "anteprima" (bottone anteprima) il file viene salvato in tabella anteprima altrimenti viene inviato nella tabella definitiva.
Ecco i codici:
codice HTML:
<?php
$id=$_GET["id"];
mysql_connect("localhost", "root", "root");
mysql_select_db("DB");
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
$sql="SELECT * FROM `anteprima` WHERE id=1";
$dati=mysql_query($sql);
$row=mysql_fetch_array($dati);
?>
<article>
<div class="content3">
<div class="foto2">
<?php
echo '<img src="getfotoant.php?id=' . $row['id'] . '" alt="' . $row['alt'] . '" title="' . $row['alt'] .'" width="300px" /> ' . "\n";
?>
<br/>
<?php echo $row["descfoto"]; ?>
</div>
<div class="article3">
<h2><?php echo $row["titolo"]; ?></h2>
<h4><?php echo $row["sottotitolo"]; ?></h4>
<pre><p><?php echo $row["descrizione"]; ?></p></pre>
<p>
PRODOTTI: <?php echo $row["prodotti"]; ?>
</p>
</div>
</div>
</article>
<form class="upload" action="../db/prodottiupload.php" method="post" enctype="multipart/form-data">
<input class="input" type="text" name="userFile" value="<?php echo $row['immagine']; ?>"/>
<input class="input" type="text" name="alt" required value="<?php echo $row['alt']; ?>"/>
<input class="input" type="text" name="descFoto" value="<?php echo $row['descfoto']; ?>"/>
<input class="input" type="text" name="title" required value="<?php echo $row['titolo']; ?>"/>
<input class="input" type="text" name="subtitle" required value="<?php echo $row['sottotitolo']; ?>"/>
<textarea class="input" name="descrizione" required /><?php echo $row['descrizione']; ?></textarea>
<input class="input" type="text" name="prodText" required value="<?php echo $row['prodotti']; ?>"/>
<button class="invio" type="submit">Invia</button>
</form>
<div class="content3" style="background:transparent; box-shadow:none">
<form class="upload" action="../db/prodottiupload.php" method="post" enctype="multipart/form-data">
<p>Seleziona la foto</p>
<input class="input" type="file" name="userFile" id="userFile" />
<p>Testo alternativo</p>
<input class="input" type="text" name="alt" required value="<?php echo $row['alt']; ?>"/>
<p>Descrizione della foto</p>
<input class="input" type="text" name="descFoto" value="<?php echo $row['descfoto']; ?>"/>
<p>Titolo</p>
<input class="input" type="text" name="title" required value="<?php echo $row['titolo']; ?>"/>
<p>Sottotitolo</p>
<input class="input" type="text" name="subtitle" required value="<?php echo $row['sottotitolo']; ?>"/>
<p>Descrizione prodotto</p>
<textarea class="input scorre" rows="8" name="descrizione" required /><?php echo $row['descrizione']; ?></textarea>
<p>Prodotti</p>
<input class="input" type="text" size="30" name="prodText" required value="<?php echo $row['prodotti']; ?>"/>
<button class="invio" type="submit" formaction="prodottiupload.php?anteprima=01">Anteprima</button>
</form>
</div>
codice HTML:
<!DOCTYPE html>
<html lang="it">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1>Caricamento file</h1>
<p>
Hai caricato questo files:<br/><br/>
Nome: <?php echo $_FILES['userFile']['name'] ?><br/>
Grandezza: <?php echo $_FILES['userFile']['size'] ?> bytes<br/>
Tipo: <?php echo $_FILES['userFile']['type'] ?>
</p>
<?php
$peso=80000;
if ( empty($_FILES['userFile']['type']) ) {
die('<p><b>ERRORE!</b> Nessuna immagine selezionata</p></body></html>');
} else if ( empty($_POST['title']) ) {
die('<p><b>ERRORE!</b> Nessun titolo inserito</p></body></html>');
} else if ( empty($_POST['descrizione']) ) {
die('<p><b>ERRORE!</b> Manca la descrizione</p></body></html>');
} else if ( !preg_match( '/gif|png|x-png|jpeg/', $_FILES['userFile']['type']) ) {
die('<p><b>ERRORE!</b> Estensione del file non supportata</p></body></html>');
} else if ( strlen($_POST['alt']) < 5 ) {
die('<p><b>ERRORE!</b> Testo alternativo troppo breve</p></body></html>');
} else if ( $_FILES['userFile']['size'] > $peso ) {
?><p><b>ERRORE!</b> File più grande di <?php echo $peso ?> bytes</p><?php
die('</body></html>');
} else if ( !($link=mysql_connect("localhost", "root", "root")) ) {
die('<p>Error connecting to database</p></body></html>');
} else if ( !(mysql_select_db("DB")) ) {
die('<p>Error selecting database</p></body></html>');
} else if ( !($handle = fopen ($_FILES['userFile']['tmp_name'], "r")) ) {
die('<p>Error opening temp file</p></body></html>');
} else if ( !($image = fread ($handle, filesize($_FILES['userFile']['tmp_name']))) ) {
die('<p>Error reading temp file</p></body></html>');
} else {
fclose ($handle);
$image = mysql_real_escape_string($image);
$alt = $_POST['alt'];
$descFoto = $_POST['descFoto'];
$title = $_POST['title'];
$subtitle = $_POST['subtitle'];
$descrizione = $_POST['descrizione'];
$prodText = $_POST['prodText'];
if(get_magic_quotes_gpc())
{
$alt = stripslashes($alt);
$descFoto = stripslashes($descFoto);
$title = stripslashes($title);
$subtitle = stripslashes($subtitle);
$descrizione = stripslashes($descrizione);
$prodText = stripslashes($prodText);
}
$alt = mysql_real_escape_string($alt);
$descFoto = mysql_real_escape_string($descFoto);
$title = mysql_real_escape_string($title);
$subtitle = mysql_real_escape_string($subtitle);
$descrizione = mysql_real_escape_string($descrizione);
$prodText = mysql_real_escape_string($prodText);
if(isset($_GET["anteprima"])){
$query = "UPDATE anteprima SET
type='{$_FILES['userFile']['type']}',
alt='" . $alt . "',
immagine='" . $image . "',
descfoto='" . $descFoto . "',
titolo='" . $title . "',
sottotitolo='" . $subtitle . "',
descrizione='" . $descrizione . "',
prodotti='" . $prodText . "'
WHERE id=1";
$result = mysql_query($query);
if (!$result) {
die("Errore nella query $query: " . mysql_error());
} else {
header("location: ../db/prodottimodifica.php?anteprima=1");
}
}
else {
$query = 'INSERT INTO prodotti (immagine,alt,type,descfoto,titolo,sottotitolo,descrizione,prodotti) VALUES ("' . $image . '","' . $alt . '","' . $_FILES['userFile']['type'] . '","' . $descFoto . '","' . $title . '","' . $subtitle . '","' . $descrizione . '","' . $prodText . '")';
$result = mysql_query($query);
if (!$result) {
die("Errore nella query $query: " . mysql_error());
} else {
header("location: ../prodotti.php?conferma=1");
}
}
};
?>
</body>
</html>
Nel prima form ora ho messo il campo testo collegato alla variabile dell'immagine. Il risultato è che la variabile userFile risulta vuota