ciao ragazzi
ho un problema con questo codice, ovvero se io non carico nessun file la variabile $immagine non viene settata con valore "default" e quindi poi mi stampa un'immagine inesistente (img error)
qui il codice
Codice PHP:
if(isset($_POST['load-service']) and $_SERVER['REQUEST_METHOD'] == "POST") { if(isset($_FILES['fileImg'])) { $key_anno=date('Y'); $key_mese=date('m'); $key_giorno=date('d'); $key_ora=date('h'); $key_minuto=date('i'); $key_secondo=date('s'); $key_number_random=rand(0,999999); $immagine=$key_anno.$key_mese.$key_giorno.$key_ora.$key_minuto.$key_secondo.$key_number_random; $estensione=pathinfo($_FILES["fileImg"]["name"], PATHINFO_EXTENSION); $immagine=$immagine.".".$estensione; move_uploaded_file($_FILES["fileImg"]["tmp_name"], "img/services/".$immagine); unset($_FILES['fileImg']); } else { $immagine = "default"; } $it_testo = addslashes($_POST['textServIT']); $it_test = stripslashes($it_testo); $en_testo = addslashes($_POST['textServEN']); $en_testo = stripslashes($en_testo); $load_service = $db->prepare("INSERT INTO cityhub_services (it_titolo,en_titolo,it_testo,en_testo,immagine) VALUES (:it_titolo,:en_titolo,:it_testo,:en_testo,:immagine)"); $load_service->execute(array(':it_titolo'=>$_POST['inputServTitleIT'],':en_titolo'=>$_POST['inputServTitleEN'],':it_testo'=>$it_testo,':en_testo'=>$en_testo,':immagine'=>$immagine));
l'html invece è così
codice HTML:
<form class="form-horizontal" method="POST" role="form" enctype="multipart/form-data" onsubmit="return control_form()" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
<!-- CARICA IMMAGINE --> <div class="form-group"> <label class="control-label">Allega immagine</label> <input type="file" name="fileImg"> </div> <div class="form-group"> <a href="#" class="btn btn-danger btn-block cancel-load-service" role="button">Annulla</a> </div> <div class="form-group"> <button type="submit" class="btn btn-success btn-block" name="load-service">Carica</button> </div>
<!-- LISTA SERVIZI --> <div class="col-md-12 text-center"> <?php $show_services = $db->prepare("SELECT * FROM cityhub_services ORDER BY id DESC"); $show_services->execute(); $result_services = $show_services->fetchAll(); ?> <div class="table-responsive"> <table class="table table-striped"> <thead> <tr> <th>Titolo IT</th> <th>Titolo EN</th> <th>Testo IT</th> <th>Testo EN</th> <th>Immagine</th> </tr> </thead> <tbody> <?php foreach($result_services as $row) { if($row['immagine']=="default"){ $img_service = "img/services_blank.png"; } else { $img_service = "img/services/".$row['immagine']; } echo "<tr>"; echo "<td>{$row['it_titolo']}</td>"; echo "<td>{$row['en_titolo']}</td>"; echo "<td>{$row['it_testo']}</td>"; echo "<td>{$row['en_testo']}</td>"; echo "<td><img src=".$img_service." style=\"height:250px;width:250px;\" class=\"center-block img-responsive\"></td>"; echo "</tr>"; } ?> </tbody>
il punto è che sul database il campo $immagine non me lo salva con il valore "default" ma invece utilizza il valore $immagine generato da data mese giorno ora ecc ecc ecc...