versione con l'oggetto $pdo:
stessi problemi...codice:<!doctype html> <html> <head> <meta charset="utf-8"> <style type="text/css"> label, input, textarea { margin: 1em; } </style> <title>Bacheca elettronica</title> </head> <body> <form method="post" action=""> <label for="name">Nome</label> <input type="text" name="name" id="name"/><br> <label for="mail">Email</label> <input type="text" name="mail" id="mail"/><br> <label for="message">Messaggio</label><br> <textarea rows="5" cols="40" name="message" id="message"></textarea><br> <input type="submit" value="Invia"/> </form> <?php //Parametri del database $db_host = "localhost"; //posizione in cui si trova il database $db_user = "admin"; //utente del database $db_password = "password"; //password del database $database = "bacheca"; //nome del database $db_bacheca = "bacheca"; //nome della tabella del database //Controllo dati $error= ""; if(isset($_POST['name']) and $_POST['name'] != ""){//<- controllo nome $nome = strip_tags($_POST['name']); } elseif(isset($_POST['name'])){ $error .= "Nome mancante.<br />"; } if(isset($_POST['mail']) and preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $_POST['mail'])){//<- controlla se la mail è presente e se è in un formato valido $email = $_POST['mail']; } elseif(isset($_POST['mail'])){ $error .= "Email mancante o non valida.<br />"; } if(isset($_POST['message']) and $_POST['message'] != ""){//<- controllo messaggio $messaggio = strip_tags($_POST['message']); } elseif(isset($_POST['message'])){ $error .= "Inserire un messaggio.<br/>"; } //Inizializzo le variabili $pdo=null; $result=null; //Mi connetto al database try { $pdo = new PDO("mysql:host=".$db_host.";dbname=".$database, $db_user, $db_password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec('SET NAMES "utf8"'); } catch (PDOException $e) { $error .= "Impossibile connettersi al database.<br/>"; echo "<p>".$error."</p>"; exit(); } //Salvo il messaggio try { if(isset($nome,$email,$messaggio)){ $sql = "INSERT INTO ".$db_bacheca." (nome,email,messaggio) VALUES ('".$nome."','".$email."','".$messaggio."')"; //Invio i dati al database $pdo->exec($sql); } else{ echo "<p>".$error."</p>"; } } catch (PDOException $e) { $error .= "Impossibile scrivere nel database.<br/>"; echo "<p>".$error."</p>"; exit(); } //Leggo il contenuto del database try { $sql = "SELECT * FROM ".$db_bacheca." ORDER BY data_messaggio DESC"; //Invio i dati al database $result = $pdo->query($sql); } catch (PDOException $e) { $error .= "Impossibile leggere dal database.<br/>"; echo "<p>".$error."</p>"; exit(); } //Scrivo gli ultimi 5 record dal database try { $n = 0; $max = 5; while($row=$result->fetch() and $n < $max){ echo " <div> <p>".$row["messaggio"]."<br/> Autore: ".$row["nome"]."<br/> Data: ".$row["data_messaggio"]."<br/> Email: ".$row["email"]."</p> </div> "; $n++; } } catch (PDOException $e) { $error .= "Impossibile scrivere i contenuti prelevati dal database.<br/>"; echo "<p>".$error."</p>"; exit(); } ?> </body> </html>
P.S.:questo codice qui sotto non funge e non riesco a capire perché!
Codice PHP:<input type="text" name="name" id="name" value="<?php (isset($name) and $name!=null) ? print_r($name) : print_r(""); ?>"/><br>
...
$sql = "INSERT INTO ".$db_bacheca." (nome,email,messaggio) VALUES ('".$nome."','".$email."','".$messaggio."')";
//Invio i dati al database
$pdo->exec($sql);
$nome=null;

Rispondi quotando