Creare un sistema di commenti agli articoli in php/mysql

In questo articolo tratteremo come creare un sistema dicommenti agli articoli.

La tabella sql creata da:

CREATE TABLE `commenti` (`id` int(11) NOT NULL auto_increment,`data` varchar(15) NOT NULL default '',`id_art` varchar(12) NOT NULL default '',`tit_art` varchar(100) NOT NULL default '',`commento` longtext NOT NULL,`utente` varchar(40) NOT NULL default '',PRIMARY KEY (`id`)) TYPE=MyISAM;

La pagina che mostra i commenti e contiene in form perinserirne uno nuovo

<?$obj->connessione();$dati=mysql_query("select * from commenti where id_art='$id' order by data desc");while($array=mysql_fetch_array($dati)){echo"$array[data] da ";echo"<a href=mailto:$array[utente]>$array[utente]</a><br>";echo"$array[commento]<br>";}?><br><input type="button" value="link" onclick="document.theform.commento.innerHTML+='[a][/a]'"><input type="button" value="codice" onclick="document.theform.commento.innerHTML+='[code][/code]'"><input type="button" value="grassetto" onclick="document.theform.commento.innerHTML+='[b][/b]'"><form method="post" action="savecomment.php" name="theform"><input type="hidden" value="<?echo $id;?>" name="idart" ><input type="hidden" value="<?echo $titolo;?>" name="titart">Email :<input type="text" value="" name="email"> <br>Commento:<br><textarea rows=13 cols=60 name="commento"></textarea><br><input type="submit" value="Inserisci"></form>


 

La pagina savecomment.php che salva il commento nella tabella

<?$data= date("d/m/y" ,time());$new=$commento;$new=str_replace("<?" ,"&lt;?" ,$new);$new=str_replace("<?php" ,"&lt;?php" ,$new);$new=str_replace("<" ,"&lt;" ,$new);$new=str_replace("[a]" ,"<a href=" ,$new);$new=str_replace("[/a]" ,">link</a>" ,$new);$new=str_replace("[code]" ,"<p class=codice>" ,$new);$new=str_replace("[/code]" ,"</p>" ,$new);$new=str_replace("[b]" ,"<b>" ,$new);$new=str_replace("[/b]" ,"</b>" ,$new);$new = ereg_replace("" ,"<br>" ,$new);$dati=mysql_query("insert into commenti(data,id_art,tit_art,commento,utente) values ('$data','$idart','$titart','$new','$email')");?>