Creare un sistema di commenti agli articoli in php/mysql

Posted on 01. May, 2006 by Administrator in mysql, php, tutorials


Obiettivo
In questo articolo tratteremo come creare un sistema di commenti agli articoli:

Il database
La tabella sql sara' composta da 5 campi, il campo id e' solo per l'incremento:

"data" indica la data del commento
"id_art" indica l'id dell'articolo
"commento" e' il commento vero e proprio
"utente" e' la email di chi ha lasciato il commento

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

Il form
La pagina che mostra i commenti e contiene in form per inserirne 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"< href=mailto:$array[utente]>$array[utente]</><br>";
echo"$array[commento]<br>";
}
?>
<br>
<input type="button" vlue="link" onclick="document.theform.commento.innerHTML+='


'">
<input type="button" vlue="codice" onclick="document.theform.commento.innerHTML+='


'">
<input type="button" vlue="grassetto" onclick="document.theform.commento.innerHTML+='[b][/b]'">
<form method="post" action="savecomment.php" name="theform">
<input type="hidden" value="<?echo $id;?>" name="idart" >
Emil :<input type="text" value="" name="email"> <br>
Commento:<br><textarea rows=13 cols=60 name="commento"></textarea><br>
<input type="submit" vlue="Inserisci">
</form>

Salvare i commenti

La pagina svecomment.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("

","&lt; href=",$new);                                                                 <br />
$new=str_replace("

",">link</>",$new);
$new=str_replace("

","&lt;p clss=codice>",$new);                                                      <br />
$new=str_replace("

","</p>",$new);
$new=str_replace("[b]","<b>",$new);
$new=str_replace("[/b]","</b>",$new);
$new = ereg_replace("\n","<br>",$new);
$dti=mysql_query("insert into commenti(data,id_art,commento,utente) vlues ('$data','$idart','$new','$email')");
?>


Correlati

2 Comments

Weallor

04. May, 2009

eh. amazing

melvo

09. Jun, 2009

bello ma non mettete il codice del vostro sistema di commenti?

Leave a reply