ho questo codice che funziona ma mi da uno strano problema nel richiamare i record del db nella tabella i dati contenuti nell'array "enti" me li visualizza, mentre quelli richimati dall'array "città" no. Potete aiutarmi a capire perchè? grazie
Codice PHP:
<?php
if($_POST && isset($_GET['id']))
{
aggiorna_record();
}
elseif(isset($_GET['id']))
{
mostra_record();
}
else
mostra_lista();
function mostra_lista()
{
// mostro un eventuale messaggio
if(isset($_GET['msg']))
echo '<b>'.htmlentities($_GET['msg']).'</b><br /><br />';
// preparo la query
$query = "SELECT id,citta,ente FROM enti";
// invio la query
$result = mysql_query($query);
// controllo l'esito
if (!$result) {
die("Errore nella query $query: " . mysql_error());
}
$citta_array = array(' ','citta1','citta2','citta3',);
$ente_array = array(' ','ente1','ente2','ente3','ente4','ente5');
echo '
<table border="1">
<tr>
<th> Città </th>
<th> Ente </th>
<th> </th>
</tr>';
while ($row = mysql_fetch_assoc($result))
{
if(!$citta) $citta = ' ';
if(!$ente) $ente = ' ';
$citta = $citta_array[$row['citta']];
$ente = $ente_array[$row['ente']];
// preparo il link per la modifica dei dati del record
$link = $_SERVER['PHP_SELF'] . '?id=' . $row['id'];
echo "<tr>
<td> $citta </td>
<td> $ente </td>
<td><a href=\"$link\"> modifica </a></td>
</tr>";
}
echo '</table>';
// libero la memoria di PHP occupata dai record estratti con la SELECT
mysql_free_result($result);
// chiudo la connessione a MySQL
mysql_close();
}
function aggiorna_record()
{
// recupero i campi di tipo "stringa"
$nome_dirigente = trim($_POST['nome_dirigente']);
// verifico se devo eliminare gli slash inseriti automaticamente da PHP
if(get_magic_quotes_gpc())
{
$nome_dirigente = stripslashes($nome_dirigente);
}
// effettuo l'escape dei caratteri speciali per inserirli all'interno della query
$nome_dirigente = mysql_real_escape_string($nome_dirigente);
// recupero gli altri campi del form
$citta = strval($_POST['citta']);
$ente = strval($_POST['ente']);
$id = intval($_GET['id']);
// preparo la query
$query = "UPDATE enti SET
citta = '$citta',
ente = '$ente',
nome_dirigente = '$nome_dirigente'
WHERE id = $id";
// invio la query
$result = mysql_query($query);
// controllo l'esito
if (!$result) {
die("Errore nella query $query: " . mysql_error());
}
// chiudo la connessione a MySQL
mysql_close();
$messaggio = urlencode('Dati aggiornati');
header("location: $_SERVER[PHP_SELF]?msg=$messaggio");
}
function mostra_record()
{
// mostro un eventuale messaggio
if(isset($_GET['msg']))
echo '<b>'.htmlentities($_GET['msg']).'</b><br /><br />';
$id = intval($_GET['id']);
// preparo la query
$query = "SELECT citta,ente,nome_dirigente FROM enti WHERE id = $id";
// invio la query
$result = mysql_query($query);
// controllo l'esito
if (!$result) {
die("Errore nella query $query: " . mysql_error());
}
// controllo che la SELECT abbia restituito un record
// l'id passato via GET potrebbe essere stato manipolato
if(mysql_num_rows($result) != 1) {
die("l'ID passato via GET è errato");
}
list($citta,$ente,$nome_dirigente) = mysql_fetch_row($result);
$nome_dirigente = htmlspecialchars($nome_dirigente);
?>
<form name="form_registrazione" method="post" action="">
<p>
<label>Nome dirigente:
<input name="nome_dirigente" type="text" value="<?echo $nome_dirigente?>" />
</label>
<p>
<label>Città:
<select name="citta">
<option value="00"> città </option>
<option value="01" <?if($citta=='01') echo 'selected="selected"'?>>citta1</option>
<option value="02" <?if($citta=='02') echo 'selected="selected"'?>>citta2</option>
<option value="03" <?if($citta=='03') echo 'selected="selected"'?>>citta3</option>
</select>
</label>
</p>
<p>
<label>Enti:<br />
<input name="ente" type="checkbox" value="1" <?if($ente==1) echo 'checked="checked"'?> />ente1<br />
<input name="ente" type="checkbox" value="2" <?if($ente==2) echo 'checked="checked"'?> />ente2<br />
<input name="ente" type="checkbox" value="3" <?if($ente==3) echo 'checked="checked"'?> />ente3<br />
<input name="ente" type="checkbox" value="4" <?if($ente==4) echo 'checked="checked"'?> />ente4<br />
<input name="ente" type="checkbox" value="5" <?if($ente==5) echo 'checked="checked"'?> />ente5<br />
</label>
</p>
<p>
<input name="invia" type="submit" value="Invia" />
</p>
</form>
<?
}
?>