<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Scrive due volte lo stesso utente]]></title><description><![CDATA[<p dir="auto">Ciao a tutti, sono arrivato alla frutta, non riesco ad uscire da questo problema, aiutatemi vi prego:</p>
<p dir="auto">ho un file php che deve aggiungere uno studente nel db se non è presente, mentre se è gia presente deve inserire il voto relativo alla sua interrogazione.</p>
<p dir="auto">Il problema è che se lo studente non è presente lo inserisce due volte, mentre se è già presente inserisce due volte il voto relativo alla sua interrogazione.</p>
<p dir="auto">posto il file....</p>
<p dir="auto">&lt;?php<br />
include("config.inc.php");</p>
<h1></h1>
<h1>Aquisisce i dati dal file config.inc '$db_host, $db_user, $db_password'</h1>
<p dir="auto">$varNome="Brambilla";<br />
#$varNome=$_POST["nome"];<br />
$varVoto=$_POST["voto"];<br />
$varDescrizione=$_POST["descrizione"];</p>
<h1></h1>
<h1>acquisisce i dati dal form html e li inserisce nelle</h1>
<h1>variabili $varNome, £varVoto e $varDescrizione</h1>
<h1></h1>
<p dir="auto">$conn=mysql_connect("$db_host", "$db_user", "$db_password");</p>
<h1></h1>
<h1>Collegamento al server mySQL che ritorna true o false nella</h1>
<h1>variabile $conn se c'è stato il collegamento oppure no.</h1>
<h1></h1>
<p dir="auto">if(!$conn){<br />
echo("errore durante la connessione a mysql");<br />
exit();}<br />
else{<br />
echo("connessione avvenuta correttamente<br />
");<br />
}</p>
<h1></h1>
<p dir="auto">mysql_select_db($db_name, $conn) OR die ("Errore nella selezione del db: "<br />
. mysql_error());</p>
<h1></h1>
<h1>Seleziona la connessione e il database sul quale si deve lavorare e</h1>
<h1>riporta l'eventuale errore del db</h1>
<h1></h1>
<p dir="auto">$strSQL="SELECT id_alunno FROM alunni WHERE nome='$varNome'";</p>
<h1></h1>
<h1>Costruzione della query da passare al db tramite la variabile $strSQL</h1>
<h1></h1>
<p dir="auto">$risultato=mysql_query($strSQL, $conn)or die("Errore nella query: "<br />
. mysql_error());</p>
<h1></h1>
<h1>Restituisce un identificativo del risultato (cioè un'altra variabile di tipo</h1>
<h1>resource), che servirà successivamente, se la query è andata a buon fine;</h1>
<h1>se invece MySql ha rilevato degli errori, la funzione restituisce FALSE;</h1>
<h1></h1>
<p dir="auto">$riga=mysql_fetch_array($risultato);</p>
<h1></h1>
<h1>Legge le righe dal database restituendo l'id alunno</h1>
<h1></h1>
<p dir="auto">$varInsVoto=0;<br />
if($riga==false and $varInsVoto==0){<br />
$verifica="riga = false<br />
";<br />
echo $verifica;<br />
echo "VarInsVoto: ".$varInsVoto."<br />
";<br />
echo "Risultato: " .mysql_num_rows($risultato). "<br />
";<br />
$strSQL="INSERT INTO alunni (nome)VALUES('$varNome')";<br />
$varInsVoto=1;<br />
$risultato=mysql_query($strSQL,$conn);<br />
if(! mysql_query($strSQL)){<br />
echo("Errore nel comando INSERT");<br />
exit();}<br />
else{<br />
echo("Studente <strong>$varNome</strong> aggiunto corretamente<br />
");<br />
}<br />
}</p>
<h1></h1>
<h1>se l'alunno è presente nel database</h1>
<h1></h1>
<p dir="auto">else{<br />
$verifica="if riga = true<br />
";<br />
echo $verifica;<br />
$strSQL="SELECT id_alunno FROM alunni WHERE nome='$varNome'";<br />
$risultato=mysql_query($strSQL);<br />
$riga=mysql_fetch_array($risultato);}</p>
<h1></h1>
<h1>Legge le righe dal db restituendo l'id_alunno relativo al nome immesso</h1>
<h1></h1>
<p dir="auto">while($riga){	<br />
echo("id alunno: ".$riga["id_alunno"]."<br />
");<br />
$id_ricavato=$riga["id_alunno"];<br />
$riga=mysql_fetch_array($risultato);}</p>
<h1></h1>
<h1>ed inserisce l'interrogazione</h1>
<h1></h1>
<p dir="auto">$strSQL="INSERT INTO interrogazioni(id_alunno, voto, descrizione)<br />
VALUES('$id_ricavato','$varVoto','$varDescrizione')";<br />
$risultato=mysql_query($strSQL);<br />
if(! mysql_query($strSQL)){<br />
echo("Errore nel comando INSERT");<br />
exit();}<br />
else{<br />
echo "VarInsVoto: ".$varInsVoto."<br />
";<br />
echo("Studente <strong>$varNome</strong> già presente, aggiunto voto e descrizione<br />
");	<br />
$strSQL="SELECT id_alunno FROM alunni WHERE nome='$varNome'";<br />
$risultato=mysql_query($strSQL);<br />
$riga=mysql_fetch_array($risultato);<br />
$id_ricavato=$riga["id_alunno"];<br />
}<br />
<a href="/hashtags/if">#if</a>(! mysql_query($strSQL)){</p>
<h1>echo("Errore nel comando SELECT");</h1>
<h1>exit();}</h1>
<p dir="auto"><a href="/hashtags/else">#else</a></p>
<h1>echo("query effettuata corretamente");</h1>
<h1>$strSQL="INSERT INTO interrogazioni(id_alunno,voto,descrizione)</h1>
<h1>VALUES('$id_ricavato','$varVoto','$varDescrizione')";</h1>
<h1>$risultato=mysql_query($strSQL);</h1>
<p dir="auto">?&gt;</p>
<p dir="auto">grazie e ciao a tutti</p>
]]></description><link>https://dev.connect.gt/topic/9370/scrive-due-volte-lo-stesso-utente</link><generator>RSS for Node</generator><lastBuildDate>Fri, 27 Nov 2026 08:15:54 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/9370.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 15 Apr 2006 17:17:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Scrive due volte lo stesso utente on Mon, 17 Apr 2006 17:30:29 GMT]]></title><description><![CDATA[<p dir="auto">risolto, grazie comunque.</p>
]]></description><link>https://dev.connect.gt/post/314688</link><guid isPermaLink="true">https://dev.connect.gt/post/314688</guid><dc:creator><![CDATA[francodace]]></dc:creator><pubDate>Mon, 17 Apr 2006 17:30:29 GMT</pubDate></item></channel></rss>