[PHP] salvataggio di un dato più volte

simo94n6

Nuovo Utente
5 Ott 2018
10
0
1
ciao ragazzi vi scrivo perchè ci sto sbattendo la testa ma non riesco ad arrivare alla soluzione.Ho molti vincoli da controllare ma sembrerebbe siano tutti rispettati,il problema mi si presenta quando devo andare a salvare ciò che viene scritto in una textarea vi mostro il codice per farvi comprendere..
PHP:
<?php                                                   $xmlString = "";
                                                                        foreach ( file("xml/prodotto.xml") as $node ) {
                                                                            $xmlString .= trim($node);
                                                                        }

                                                                        $doc = new DOMDocument();
                                                                        if (!$doc->loadXML($xmlString)) {
                                                                          die ("Errore nel parsing del documento\n");
                                                                        }
                                                                        
                                                                        $root = $doc->documentElement;
                                                                        $records = $doc->documentElement->childNodes;           
                                                                        
                                                                        // Scorre i record:
                                                       for ($a=0; $a<$records->length; $a++) {
                                                                            $record = $records->item($a);
                                                                            $id= $record->firstChild;
                                                                            $idValue= $id->nodeValue;
                                                                            $titolo= $id->nextSibling;
                                                                            $titoloValue= $titolo->textContent;
                                                                            $genere= $titolo->nextSibling;
                                                                            $genereValue= $genere->textContent;
                                                                            $fot= $genere->nextSibling;
                                                                            $fotValue= $fot->textContent;
                                                                            $descrizion= $fot->nextSibling;
                                                                            $descrizionValue= $descrizion->textContent;
                                                                            $prezz= $descrizion->nextSibling;
                                                                            $prezzValue= $prezz->nodeValue;
                                                                            $totvalutaz= $prezz-> nextSibling;
                                                                            $totvalutazValue= $totvalutaz->nodeValue;
                                                                            $numvalutaz= $totvalutaz-> nextSibling;
                                                                            $numvalutazValue= $numvalutaz->nodeValue;
                                                                            $valutaz= $numvalutaz-> nextSibling;
                                                                            $valutazValue= $valutaz->nodeValue;
      
                                        //if che mi evidenzia il prodotto che è stato prenotato e quindi sul catalogo mi darà la possibilità di commentarlo
                                                                            if(isset($_SESSION['prodott']) && $titoloValue==$_SESSION['prodott'])
                                                                        {
                                                                            $confronto=$titoloValue;
                                                                            $_SESSION['titoloValue']=$confronto;
                                                                            echo"$confronto";
                                                                            
                                                                        }
     
                                                                             //visualizzazione elenco
                                                       tabellaprodotto("$titoloValue","$genereValue","$fotValue","$descrizionValue","$prezzValue","$valutazValue");
quando appunto vado a richiamare la funzione tabellaprodotto dovrò avere una tabella differente a seconda del tipo di utente che la visiona,il problema avviene quando devo salvare il commento che un utente effettua,in quanto me lo salva svariate volte e li succede il casino
PHP:
//UTENTE CHE HA PRENOTATO
      else if(isset($_SESSION['ora']) && $_SESSION['titoloValue']==$_SESSION['prodott']) {
          
          echo"<td class=\"pb\"><form method='post' action='prodotti1.php'>
        <textarea name ='comment' placeholder='Digita commento'></textarea><br />
        <input class='button' type='submit' name='commento' value='Commenta'>";
        //leggo i commenti
                    $xmlString = "";
                foreach ( file("xml/commenti.xml") as $node ) {
                    $xmlString .= trim($node);
                }

                $doc = new DOMDocument();
                if (!$doc->loadXML($xmlString)) {
                  die ("Errore nel parsing del documento\n");
                }
                
                $root = $doc->documentElement;
                $records = $doc->documentElement->childNodes;           
                
                // Scorre i record:
                        

                for ($a=0; $a<$records->length; $a++)
                {
                    $record = $records->item($a);
                    $id= $record->firstChild;
                    $idValue= $id->nodeValue;
                    $nomeut= $id->nextSibling;
                    $nomeutValue= $nomeut->textContent;
                    $titoloprod= $nomeut->nextSibling;
                    $titoloprodValue= $titoloprod->textContent;
                    if($titoloprodValue==$_SESSION['titoloValue'])
                    {
                    $testo= $titoloprod->nextSibling;
                    $testoValue= $testo->textContent;

                echo"<tr border='2'>
                    <td >$nomeutValue</td>
                    <td class=\"pb\">".$testoValue."</td>
                    <td>bottone </td>
                  </tr>";
                    $_SESSION['titoloValue']="";
                    }
                }$_SESSION['titoloValue']="";
          
      }
      //UTENTE CHE HA PRENOTATO E COMMENTATO
          if(isset($_POST['commento']))
          {
                  //scrivo su file xml
                $xmlString = "";
                foreach ( file("./xml/commenti.xml") as $node ) {
                    $xmlString .= trim($node);
                }

                $doc = new DOMDocument();
                if (!$doc->loadXML($xmlString)) {
                  die ("Errore mentre nel parsing documento\n");
                }
                
                $root = $doc->documentElement;
                $records = $doc->documentElement->childNodes;   

                //  del nuovo <record> commento
                $nRecord = $doc->createElement("commento");
                $Idan = $doc->createElement("id", 0);
                $nnomeut = $doc->createElement("nomeutente", $_SESSION['username']);
                $ntitoloprod = $doc->createElement("titolo", $_SESSION['prodott']);
                $ntesto = $doc->createElement("testo", $_POST['comment']);
  
                $nRecord->appendChild($Idan);
                $nRecord->appendChild($nnomeut);
                $nRecord->appendChild($ntitoloprod);
                $nRecord->appendChild($ntesto);

                
                // append del nuovo utente:
                $root->appendChild($nRecord);


                // salvo le modifiche
                $percorso=dirname(__FILE__) . "/xml/commenti.xml";
                $doc->formatOutput = true;
                $doc->save($percorso);
                
          }
 

Discussioni simili