[PHP] [RISOLTO] Modifica record db da form

Alex@1983

Utente Attivo
9 Mag 2015
126
2
18
Ciao a tutti,

volevo chiedervi aiuto per una cosa sulla quale mi sono bloccato e non riesco ad andare avanti............

In pratica ho fatto un registro, composto da un form dove inserire i dati (nella parte superiore della schermata) per inserirli all'interno del db...
e da una tabella (nella parte sottostante il form) che mostra i record gia inseriti nel db

Ora vorrei rendere possibile la modifica di record già inseriti......MA NON CAPISCO PROPRIO COME FARE !!!
MI DATE UNA MANO?
Vi posto il codice della mia pagina....

Grazie mille in anticipo

PHP:
<?php
ob_start();//questa serve in quanto si usa la funzione header dopo un outpt html
session_start();
//.....
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<link type="text/css" rel="stylesheet" media="all" href="ri.css"/>
<head>
    <title>Registro Interventi</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="HAPedit 3.0">
</head>

<body>
<div style="
     padding-left: 20px;
     padding-right: 0px;
     height: 0px;
     widht: 0px;
     ">
   <form method="post" runat="server" action="<?php echo $_SERVER['PHP_SELF']; ?>" autocomplete="on">
    <table class="table" align="center"
           style="
                  padding-left: 1px;
                  width: 120%;0px;
                  padding-left: 10px
                  ">
      <tr>
        <td align="center" colspan="8" class="registro"><h1>REGISTRO</h1></td>
    </tr>
      <tr class="lbl"> 
         <td class="lbl"; align="center">DATA</td>
       </td>
     <td class="lbl"; align="center">CATEGORIA
       </td>
     <td class="lbl"; align="center">SOTTO<br>CATEGORIA
       </td>
         <td class="lbl"; align="center">STATO
       </td>
     <td class="lbl"; align="center">TEMPO<br>IMPIEGATO
       </td>
     </tr>
      <tr>   
         <td> <input type="text" id="data"  name="data" value="" size="10" autocomplete="on">
       </td>
               <input type="text" id="cat"  name="categoria" value="" size="20" autocomplete="on">
       </td>
     <td> <input type="text" id="sotcat"  name="sotcat" value="" size="15" autocomplete="on">
       </td>
         <td> <select id="stato"  name="stato" value="">
        <option></option>
        <option>Aperto</option>
        <option>Chiuso</option>
        <option>In sospeso</option>
        <option>Annullato</option>
          </select>
       </td>
     <td> <input type="text" id="tmpimp"  name="tmpimp" value="" size="10" autocomplete="on">
       </td>
     </tr>
       <tr>
         <td class="lbl"; align="center"; colspan="8">DESCRIZIONE INTERVENTO
       </td>
     </tr>
     <td colspan="8"> <input type="text" class="desc" id="desc"  name="desc" value="" size="137" autocomplete="on">
       </td>
     </tr>
     <td align="center"; colspan="8"><input type="submit" name="inserisci" value="Inserisci"
                 style="
                      width: 161px;
                      height: 55px;
                      font-size: 25px;
                      ">
       </td>
     </tr>
   </table>
  </form>
 
<br/>
<br/> 
 
<?php

if(isset($_POST['inserisci'])){
    header("Cache-Control: no-cache"); //dovrebbero vuotare la cache
    header("Expires: -1"); 

    
 $HOST = 'localhost';
$USER = 'utente';
$PASSWORD = 'password';
$connessione=mysql_connect("$HOST","$USER","$PASSWORD");//database connection
// Qui sotto al posto di NOME_DATABASE, inserite il nome del vostro DB
mysql_select_db("my_db");

$id= 'NULL';
$data = $_POST['data'];

$dataUs = explode("/", $data);
$datadb = $dataUs[2]. "-". $dataUs[1]. "-" .$dataUs[0]; 
$categoria = htmlentities($_POST['cat'], ENT_QUOTES, "UTF-8");
$sottocategoria = $_POST['sotcat'];
$descrizione = htmlentities($_POST['desc'],ENT_QUOTES);
$stato = $_POST['stato'];
$tempoimp = $_POST['tmpimp'];


if(preg_match("#^\d{2}/\d{2}/\d{4}$#",$data))

    {
    echo ".";
}
else
    {   
    echo "Inserisci la data nel formato corretto \n gg/mm/aaaa";
        
    exit;
}   

if (($data=="")

    {
        echo "Non hai inserito la data";

        exit;
    }


$toinsert = "INSERT INTO `registro`
        (id, data, richiedente, reparto, categoria, sottocategoria, operatore, descrizione, stato, tempoimpiegato)
            VALUES
            ('$id',
              '$datadb',
             '$categoria',
             '$sottocategoria',
             '$descrizione',
             '$stato',
             '$tempoimp')";


$res = mysql_query($toinsert);
if($res){
    

        echo("INSERITO");

header("location: form.php");
    
    }

else
    {
    echo("ERRORE '$id',
              '$data',
             '$categoria',
             '$sottocategoria',
             '$descrizione',
             '$stato',
             '$tempoimp' ");

mysql_close($connessione);
}
}

?>


</div>
</div>


<div class=�wide�> </div>
<br/>
<br/>
<div id="provaprova"
     style="     
     padding-top: 0px;
     width: 123%;
     padding-right: 0px;
     padding-left: 5px;
     ">
<div id="provaprova" class="tableregistro"
     style="     
     padding-top: 0px;
     width: 100%;
     padding-right: 0px;
     padding-left: 5px;
     height: 0px;
     height: 400px;
    
     border:0px solid black;
   ">

<br>
<br>

<table class="tableregistro" border=1 cellspacing=0 cellpadding=3>
    <tr class="testata">
            <th style="width: 35px">ID</th>
            <th style="width: 82px">DATA</th>
            <th style="width: 140px">CATEGORIA</th>
            <th style="width: 144px">SOTTO CATEGORIA</th>
            <th style="width: 600px">DESCRIZIONE</th>
            <th style="width: 70px">STATO</th>
            <th style="width: 55px">TEMPO</th>
        </tr>
<?php

$HOST = 'localhost';
 $USER = 'utente';
 $PASSWORD = 'password';
$connessione = mysql_connect("$HOST","$USER","$PASSWORD");//database connection
// Qui sotto al posto di NOME_DATABASE, inserite il nome del vostro DB
mysql_select_db("my_db");


$sql = "SELECT * FROM `registro`\n"
    . "ORDER BY `registro`.`id` DESC";
$result = mysql_query($sql);


while($r = mysql_fetch_array($result)) {
$dataoraRelativi = ($result['data']);
$d = explode("-", $r['data']);
$dataRelIta = $d[2]. "/" .$d[1] . "/" .$d[0];


if ($r['stato'] == "Chiuso")

echo "<tr bgcolor='#98FF98'><td style='width: 35px;'>".$r['id']."</td>".
     "<td style='width: 82px;'>".$dataRelIta."</td>".
     "<td style='width: 140px;'>".$r['categoria']."</td>".
     "<td style='width: 144px;'>".$r['sottocategoria']."</td>".
     "<td style='width: 600px;'>".$r['descrizione']."</td>".
     "<td style='width: 70px;'>".$r['stato']."</td>".
     "<td style='width: 55px;'>".$r['tempoimpiegato']."</td>
    </tr>";

else

if ($r['stato'] == "Aperto")


echo "<tr bgcolor='#FF8C69'><td>".$r['id']."</td>".
     "<td style='width: 80px;'>".$dataRelIta."</td>".
     "<td>".$r['categoria']."</td>".
     "<td style='width: 80px;'>".$r['sottocategoria']."</td>".
     "<td style='width: 400px;'>".$r['descrizione']."</td>".
     "<td>".$r['stato']."</td>".
     "<td>".$r['tempoimpiegato']."</td>
    </tr>";

else

if ($r['stato'] == "Annullato")


echo "<tr bgcolor='#5F5F5F' style='color:#FFFF00'><td>".$r['id']."</td>".
    "<td style='width: 80px;'>".$dataRelIta."</td>".
     "<td>".$r['categoria']."</td>".
     "<td style='width: 80px;'>".$r['sottocategoria']."</td>".
     "<td style='width: 400px;'>".$r['descrizione']."</td>".
     "<td>".$r['stato']."</td>".
     "<td>".$r['tempoimpiegato']."</td>
    </tr>";

else

if ($r['stato'] == "In sospeso")


echo "<tr bgcolor='#FFFF66'><td>".$r['id']."</td>".
    "<td style='width: 80px;'>".$dataRelIta."</td>".
     "<td>".$r['categoria']."</td>".
     "<td style='width: 80px;'>".$r['sottocategoria']."</td>".
     "<td style='width: 400px;'>".$r['descrizione']."</td>".
     "<td>".$r['stato']."</td>".
     "<td>".$r['tempoimpiegato']."</td>
      </tr>";

else

echo "<tr><td>".$r['id']."</td>".
     "<td style='width: 80px;'>".$dataRelIta."</td>".
     "<td>".$r['categoria']."</td>".
     "<td style='width: 80px;'>".$r['sottocategoria']."</td>".
     "<td style='width: 400px;'>".$r['descrizione']."</td>".
     "<td>".$r['stato']."</td>".
     "<td>".$r['tempoimpiegato']."</td>

    </tr>";
}

mysql_close($connessione);
?>
</table>
</div>
 </body>
</html>

<?php
ob_end_flush();//chiude ob_start
?>
 

bubino8

Utente Attivo
28 Apr 2017
360
20
28
31
BZ
Ciao se mi passi la struttura (senza dati riservati) posso dare un occhiata.
 

Alex@1983

Utente Attivo
9 Mag 2015
126
2
18
Ciao sono semplicemente:

id - autoincrement
data - date AAAA-MM-GG
categoria - varchar
sottocategoria - varchar
stato - varchar
tempo - varchar

Grazie
 

bubino8

Utente Attivo
28 Apr 2017
360
20
28
31
BZ
Creato la tabella ma manca comunque qualcosa e sarebbe da andare a controllare anche un po la sintassi.
 

Alex@1983

Utente Attivo
9 Mag 2015
126
2
18
Si quella magari non è proprio il massimo , in fondo sono comunque un novellino.

Riguardo la tabella però è quella
 

bubino8

Utente Attivo
28 Apr 2017
360
20
28
31
BZ
cerco di dare un occhiata ma a riga 135
PHP:
(id, data, richiedente, reparto, categoria, sottocategoria, operatore, descrizione, stato, tempoimpiegato)
sembrano molti piu campi rispetto alla base db che mi hai scritto tu.
 

Alex@1983

Utente Attivo
9 Mag 2015
126
2
18
Si , all'inizio ero partito con un'idea più "ambiziosa"
.. poi mi sono ridimenzionato
 

bubino8

Utente Attivo
28 Apr 2017
360
20
28
31
BZ
Premetto che c'era un casino allucinante con style css su + righe con ; messi a caso. Ti consiglio di ripassare un pò html prima di metterti a fare PHP (scusa la schiettezza)
Comunque.
Pagina principale:
PHP:
<?php
ob_start();//questa serve in quanto si usa la funzione header dopo un outpt html
session_start();
//.....
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    
    <head>
        <title>Registro Interventi</title>
        <link type="text/css" rel="stylesheet" media="all" href="ri.css"/>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <meta name="generator" content="HAPedit 3.0">
    </head>
    <body>
        <?php
        if(isset($_POST['inserisci'])){
            $HOST = 'localhost';
            $USER = 'root';
            $PASSWORD = '';
            $connessione=mysql_connect("$HOST","$USER","$PASSWORD");//database connection
            mysql_select_db("check");
            $data = $_POST['data'];
            $dataUs = explode("/", $data);
            $datadb = $dataUs[2]. "-". $dataUs[1]. "-" .$dataUs[0];
            $categoria = $_POST['categoria'];
            $sottocategoria = $_POST['sotcat'];
            $stato = $_POST['stato'];
            $tempoimp = $_POST['tmpimp'];
            $descrizione = htmlentities($_POST['desc'],ENT_QUOTES);
          
            if(preg_match("#^\d{2}/\d{2}/\d{4}$#",$data)){
                echo ".";
            }
            else{   
                echo "Inserisci la data nel formato corretto \n gg/mm/aaaa";
                exit;
            }   
            if ($data==""){
                echo "Non hai inserito la data";
                exit;
            }

            $toinsert = "INSERT INTO `registro` (id, data, categoria, sottocategoria, stato, tempo, descrizione)VALUES( NULL,'$datadb','$categoria','$sottocategoria','$stato','$tempoimp','$descrizione')";
            $res = mysql_query($toinsert) or die(mysql_error());
            if($res){
                echo("INSERITO");
                header("location: index.php");
            }
            else{
                echo("ERRORE '$data','$categoria','$sottocategoria','$descrizione','$stato','$tempoimp' ");
                mysql_error();
                mysql_close($connessione);
            }
        }

        ?>
        <div style="text-align:center;">
            <form method="post" runat="server" action="<?php echo $_SERVER['PHP_SELF']; ?>" autocomplete="on">
            <table class="table" align="center">
                <tr>
                    <td colspan="5" class="registro"><h1>REGISTRO</h1></td>
                </tr>
                <tr class="lbl">
                    <td class="lbl">DATA</td>
                    <td class="lbl" >CATEGORIA</td>
                    <td class="lbl">SOTTO<br>CATEGORIA</td>
                    <td class="lbl">STATO</td>
                    <td class="lbl">TEMPO<br>IMPIEGATO</td>
                </tr>
                <tr>   
                    <td><input type="text" id="data"  name="data" value="" size="10" autocomplete="on"></td>
                    <td><input type="text" id="cat"  name="categoria" value="" size="20" autocomplete="on"></td>
                    <td><input type="text" id="sotcat"  name="sotcat" value="" size="15" autocomplete="on"></td>
                    <td>
                        <select id="stato" name="stato">
                            <option></option>
                            <option value="Aperto">Aperto</option>
                            <option value="Chiuso">Chiuso</option>
                            <option value="In sospeso">In sospeso</option>
                            <option value="Annullato">Annullato</option>
                        </select>
                    </td>
                    <td><input type="text" id="tmpimp"  name="tmpimp" value="" size="10" autocomplete="on"></td>
                </tr>
                <tr>
                    <td colspan="5">DESCRIZIONE INTERVENTO</td>
                </tr>
                <tr>
                    <td colspan="5"><input type="text" class="desc" id="desc"  name="desc" value="" size="137" autocomplete="on"></td>
                </tr>
                <tr>
                    <td colspan="5"><input type="submit" name="inserisci" value="Inserisci" style="width: 161px;height: 55px;font-size: 25px;"></td>
                </tr>
            </table>
            </form>
        </div>
    
<br/>
        <div class="wide"> </div>
        <div id="provaprova" class="tableregistro" style="padding-top: 0px;width: 100%;padding-right: 0px;padding-left: 5px;height: 0px;height: 400px;border:0px solid black;">
            <table class="tableregistro" border=1 cellspacing=0 cellpadding=3>
                <tr class="testata">
                    <th style="width: 35px">ID</th>
                    <th style="width: 82px">DATA</th>
                    <th style="width: 140px">CATEGORIA</th>
                    <th style="width: 144px">SOTTO CATEGORIA</th>
                    <th style="width: 600px">DESCRIZIONE</th>
                    <th style="width: 70px">STATO</th>
                    <th style="width: 55px">TEMPO</th>
                </tr>
                <?php
                $HOST = 'localhost';
                $USER = 'root';
                $PASSWORD = '';
                $connessione = mysql_connect("$HOST","$USER","$PASSWORD");//database connection
                // Qui sotto al posto di NOME_DATABASE, inserite il nome del vostro DB
                mysql_select_db("check");
                $sql = "SELECT * FROM `registro` ORDER BY id DESC";
                $result = mysql_query($sql);
                while($r = mysql_fetch_array($result)) {
                    $dataoraRelativi = ($result['data']);
                    $d = explode("-", $r['data']);
                    $dataRelIta = $d[2]. "/" .$d[1] . "/" .$d[0];
                    if ($r['stato'] == "Chiuso"){
                        echo "<tr bgcolor='#98FF98'><td style='width: 35px;'>".$r['id']."</td>".
                             "<td style='width: 82px;'>".$dataRelIta."</td>".
                             "<td style='width: 140px;'>".$r['categoria']."</td>".
                             "<td style='width: 144px;'>".$r['sottocategoria']."</td>".
                             "<td style='width: 600px;'>".$r['descrizione']."</td>".
                             "<td style='width: 70px;'>".$r['stato']."</td>".
                             "<td style='width: 55px;'>".$r['tempo']."</td>
                            </tr>";
                    }elseif($r['stato'] == "Aperto"){
                        echo "<tr bgcolor='#FF8C69'><td>".$r['id']."</td>".
                             "<td style='width: 80px;'>".$dataRelIta."</td>".
                             "<td>".$r['categoria']."</td>".
                             "<td style='width: 80px;'>".$r['sottocategoria']."</td>".
                             "<td style='width: 400px;'>".$r['descrizione']."</td>".
                             "<td>".$r['stato']."</td>".
                             "<td>".$r['tempo']."</td>
                            </tr>";

                    }elseif ($r['stato'] == "Annullato"){
                        echo "<tr bgcolor='#5F5F5F' style='color:#FFFF00'><td>".$r['id']."</td>".
                            "<td style='width: 80px;'>".$dataRelIta."</td>".
                             "<td>".$r['categoria']."</td>".
                             "<td style='width: 80px;'>".$r['sottocategoria']."</td>".
                             "<td style='width: 400px;'>".$r['descrizione']."</td>".
                             "<td>".$r['stato']."</td>".
                             "<td>".$r['tempo']."</td>
                            </tr>";

                    }elseif ($r['stato'] == "In sospeso"){
                        echo "<tr bgcolor='#FFFF66'><td>".$r['id']."</td>".
                            "<td style='width: 80px;'>".$dataRelIta."</td>".
                             "<td>".$r['categoria']."</td>".
                             "<td style='width: 80px;'>".$r['sottocategoria']."</td>".
                             "<td style='width: 400px;'>".$r['descrizione']."</td>".
                             "<td>".$r['stato']."</td>".
                             "<td>".$r['tempo']."</td>
                              </tr>";
                    }else{
                        echo "<tr><td>".$r['id']."</td>".
                             "<td style='width: 80px;'>".$dataRelIta."</td>".
                             "<td>".$r['categoria']."</td>".
                             "<td style='width: 80px;'>".$r['sottocategoria']."</td>".
                             "<td style='width: 400px;'>".$r['descrizione']."</td>".
                             "<td>".$r['stato']."</td>".
                             "<td>".$r['tempo']."</td>

                            </tr>";
                    }
                }

                mysql_close($connessione);
                ?>
            </table>
        </div>
    </body>
</html>

<?php
ob_end_flush();//chiude ob_start
?>

Vai a modificare Righe: 21,22,23,25 e 116,117,118,121 sono i dati di connessione al DB.

Struttura DB:
PHP:
DROP TABLE IF EXISTS `registro`;
CREATE TABLE `registro` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `data` date DEFAULT NULL,
  `categoria` varchar(255) DEFAULT NULL,
  `sottocategoria` varchar(255) DEFAULT NULL,
  `stato` varchar(255) DEFAULT NULL,
  `tempo` varchar(255) DEFAULT NULL,
  `descrizione` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

Fammi sapere
 

Alex@1983

Utente Attivo
9 Mag 2015
126
2
18
Perfetto!!!....Bubino8

funziona tutto alla grande direi !!!!

per quanto riguarda il "casino allucinante" ti dò pienamente ragione....ho ancora molto da imparare!!!!

Grazie mille per il supporto!

Ciao
 
Discussioni simili
Autore Titolo Forum Risposte Data
N [RISOLTO]Utente Loggato, Modifica immagine personale in PHP PHP 9
N [RISOLTO]Modifica Profilo Utente PHP PHP 32
C [RISOLTO]Inserimento variabile php in input html PHP 20
moustache [RISOLTO] SQL PHP IIS PHP 8
L update tabelle in php mysql [risolto] PHP 6
A [PHP] Problema query insert [RISOLTO] PHP 14
B [PHP] recuperare IP dei server in load balancing [RISOLTO] PHP 3
K [RISOLTO] Problema Griglia Php+Mysql PHP 13
C [RISOLTO][PHP] Errore di sintassi PHP 8
Kolop [RISOLTO][PHP] Problema Pagination PHP 2
C [RISOLTO][PHP] Funzione ONclick PHP 14
C [RISOLTO][PHP] Conteggio righe di una tabella PHP 4
N [PHP] Utilizzo variabili di sessione [Risolto] PHP 13
Tommy03 [RISOLTO][PHP] Webserver o devserver? PHP 2
spider81man [PHP] Problemi cancellazione dato su DB [RISOLTO] PHP 1
A [PHP] RISOLTO Invio Mail con Tabella PHP 2
elpirata [PHP][RISOLTO] Sommare gli importi estratti da un ciclo while PHP 3
elpirata [PHP][RISOLTO] Effettuare la somma dei tempi di lavorazione PHP 3
elpirata [PHP] [RISOLTO]Sovrascrivere testo in una tabella PHP 2
A [RISOLTO]Recuperare dati inviati con json tramite php PHP 4
C [RISOLTO][PHP] Passaggio variabili senza refresh di pagina PHP 7
elpirata [PHP][RISOLTO] Errore di tipo Notice: Undefined index - Come risolvere quando si hanno tante var PHP 10
S Problema in PHP per invio file XML - RISOLTO- PHP 8
A [RISOLTO] HighChart e PHP PHP 4
A [RISOLTO] PHP Selezionare tutti i file con stessa estensione PHP 2
A [RISOLTO] Table elaborata da codice PHP con dati da DB non visualizzata in IFRAME PHP 15
Zea [PHP] RISOLTO - creare link con il risultato dell'interrogazione db PHP 1
A [PHP] [RISOLTO]Compilazione campi form da record db PHP 3
G [PHP][RISOLTO] Include/Require non funzionante PHP 9
C [PHP] RISOLTO - Eliminare elementi duplicati da array multidimensionale PHP 1
G [RISOLTO][PHP] Call to a member function num_rows() on a non-object PHP 9
U [PHP - RISOLTO] Tabella dinamica: colonne selezionate da checkbox PHP 40
CristianB72 [RISOLTO] [PHP] Istruzione "if" non funziona PHP 6
fabryx [PHP] RISOLTO: Download file da web e trasferimento su server PHP 2
fabryx [PHP]RISOLTO: Parsing xml nodi con caratteri speciali PHP 1
CristianB72 [RISOLTO][PHP] preg_match di un array PHP 7
G [RISOLTO][PHP] Estrapolare dati da Tabella PHP 11
A [RISOLTO]Errore caricamento file php PHP 21
coccobil [PHP] RISOLTO -Recupare valore data che soddisfa certi requisiti da array PHP 3
fabryx [PHP]RISOLTO: Trasformare righe in colonne assegnando singole variabili PHP 10
fabryx [PHP]RISOLTO: eliminazione riga con singolo valore duplicato in array PHP 19
elpirata [RISOLTO]Stampare giorno della settimana in tabella php PHP 6
elpirata [PHP] [RISOLTO] Stampare classe css in base al risultato della query PHP 6
elpirata [Risolto] Gestione Prenotazioni script php PHP 25
K [risolto] passaggio variabile a pagina php con ajax Ajax 6
J [risolto] Trasformare la seguente classe PHP PHP 2
E [risolto] win 8.1 + IE11 le sessioni PHP non fuonzionano correttamente PHP 7
N [RISOLTO] Mostrare a video le checkbox selezionate con PHP e MySQL PHP 4
otto9due [risolto] Problema sessioni php e login PHP 5
G [risolto] Creazione file excel con PHP PHP 3

Discussioni simili