Inserimento di dati

<html><head>
<title>Lezioncina sul PHP - 28</title>
<meta name="author" content="Ing. Stefano Salvi">
</head>
<body bgcolor=#ffffcc text=#000080 vlink=#0040cc link=#0080ff>
<table width=100% height=100%><tr>
<td width=90% valign=middle>
<h1 align=center><font size=+5>Inserimento di dati</font></h1>
</td><td width=90 valign=middle><img src=php.jpg align=right border=0
usemap=#mymap></td></tr>
<map name=mymap>
<area shape=rect coords=0,0,30,90 href=27.html>
<area shape=rect coords=30,0,60,90 href=index.html>
<area shape=rect coords=60,0,90,90 href=29.html>
</map>
<tr>
<td height=80% valign=middle align=left colspan=2>
<font size=+3>
<?php
  require "include/dbase_mysql.inc";
  // require "include/dbase_pgsql.inc";

function messaggio ($msg, $error) {
  $color = ($error) ? "ff4040" : "40ff40";
  echo "<div style=\"background-color: #$color; border: 2px solid black; font-weight: bold; text-align: center;\">$msg</div>\n";
}

  $nome = (array_key_exists("nome", $_REQUEST)) ? $_REQUEST["nome"] : "";
  $cognome = (array_key_exists("cognome", $_REQUEST)) ? $_REQUEST["cognome"] : "";
  $tel = (array_key_exists("tel", $_REQUEST)) ? $_REQUEST["tel"] : "";
  if (array_key_exists("azione", $_REQUEST) && $_REQUEST["azione"] == "inserisci") { // Ha i dati, controlla
    if (!$nome || !$cognome || !$tel) {
       messaggio ("Devono essere presenti Nome Cognome e Numero","errore");
    } else if (!preg_match ("/^[0-9+][-0-9]*$/", $tel)) {
       messaggio ("Il numero di telefono deve iniziare per 0 o + e contenere solo cifre o '-'","errore");
    } else {
      $conn=connetti ();
      $res = db_exec ($conn, "select * from telefono where tel='$tel';");
      if (db_numrows ($res) > 0) {
        messaggio ("Il telefono $tel esiste gi&agrave","errore");
      } else {
        $res = db_exec ($conn, "insert into telefono values ('$nome',
          '$cognome','$tel');");
        if ($res) {
          messaggio ("Inserito '$nome $cognome' telefono $tel", "");
          $nome = "";
          $cognome = "";
          $tel = "";
        } else {
          messaggio ("Errore nell'inserimento di '$nome $cognome' telefono $tel","errore");
        }
      }
    }
  }

  echo "<form action=28.php method=post>\n";
  echo "<input type=hidden name=azione value=inserisci>\n";
  echo "Nome <input type=text name=nome value=\"$nome\"><br>\n";
  echo "Cognome <input type=text name=cognome value=\"$cognome\"><br>\n";
  echo "Numero di telefono <input type=text name=tel value=\"$tel\"><br>\n";
  echo "<input type=submit value=\"Aggiungi numero di telefono\">\n";
  echo "</form>\n";
 ?>
<h1 align=center>[<a href=28.s.html>Sorgente</a>]</h1>
</font></td></tr></table>
<p align=right><i>&copy; Ing. Stefano Salvi - </i>released under <a
 href="http://www.gnu.org/licenses/fdl.txt"
 title="Testo inglese della FDL presso www.gnu.org">FDL licence</a></p>
</body>
</html>

© Ing. Stefano Salvi - released under FDL licence