[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
?>
 
Ciao se mi passi la struttura (senza dati riservati) posso dare un occhiata.
 
Ciao sono semplicemente:

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

Grazie
 
Creato la tabella ma manca comunque qualcosa e sarebbe da andare a controllare anche un po la sintassi.
 
Si quella magari non è proprio il massimo , in fondo sono comunque un novellino.

Riguardo la tabella però è quella
 
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.
 
Si , all'inizio ero partito con un'idea più "ambiziosa"
.. poi mi sono ridimenzionato
 
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
 
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