Ciao a tutti, sto iniziando a smanettare con i database, ma mi sono già inceppato.
In pratica, creo una tabella inviando questa query:
Codice PHP:
CREATE TABLE $section (id INT (5) UNSIGNED not null AUTO_INCREMENT, tit VARCHAR (255) not null , txt TEXT not null , dat INT (11) , PRIMARY KEY (id))
Poi, aggiungo delle righe con questa:
Codice PHP:
INSERT INTO $section (tit, txt, dat) VALUES ('$title', '$text', '$date')
Adesso arriva il problema: mettiamo che io abbia nella mia tabella 7 righe. Voglio cancellare la riga nr. 3. Quindi:
Codice PHP:
DELETE FROM $section WHERE id=$id //In questo caso $id varrà 3
Adesso però voglio che gli id delle righe 4, 5, 6 e 7 diventino rispettivamente 3, 4, 5 e 6, in modo da non avere dei "buchi" negli id (cioè, ad esempio, da non avere 1, 2, 4, 5, 6 e 7). Quindi:
Codice PHP:
SELECT count(*) AS tot FROM $section
Sapendo il totale di id, eseguo questo codice restante:
Codice PHP:
$db_max_id = "SELECT count(*) AS tot FROM $section";
$db_tot = mysql_query($db_max_id);
$tot = mysql_fetch_array($db_tot);
$max_id = $tot['tot'] - 1;
$actual_id = $id + 1;
while ($max_id >= $actual_id)
{
$new_id = $actual_id + 1;
$db_update_id = "UPDATE $section SET id=$new_id WHERE id=$actual_id";
if (mysql_query($db_update_id) == FALSE)
{
die ("Errore durante l'aggiornamento degli id");
}
$actual_id + 1;
}
echo 'Fatto!';
Ma purtroppo non funziona! La riga viene eliminata ma gli id non vengono aggiornati... Qualcuno sa dirmi perchè??