Steinböck Andreas: timestamp korrekt anzeigen

Hallo zusammen,
Ich bin gerade dabei ein Gästebuch zu gestalten und möchte dabei Datum und Zeit des Eintrages mit anzeigen. Das ganze Gästebuch besteht aus folgenden Scripts:

index.php

<html>
<head>
<title></title>
</head>
<body>
<div align="center">
<table width="450" cellspacing="1" cellpadding="3" bgcolor="#000000">
<tr>
<td bgcolor="#e2e2e2" align="center"><h4>G&aumlstebuch Mondscheinkino</h4></td>
</tr>
<tr>
<td bgcolor="#336699" align="center"><A HREF=gbook_new.php>Neuen Eintrag hinzufügen</A></td>
</tr>
</table>
</div>
<br><br>
<?
function datum($_dat)
{
// Wandelt timestamp String in Datum und Uhrzeit um.
$datum=intval(substr($_dat,6,2)).".".intval(substr($_dat,4,2)).".".substr($_dat,0,4)." um  ".substr($_dat,8,2).":".substr($_dat,10,2).":".substr($_dat,12,2);
return $datum;
}
$db = mysql_connect("localhost","root","design");
mysql_select_db("kino",$db);
$result=mysql_query("select * from gbook_kino");
while($row=mysql_fetch_array($result))
{
$id=$row["id"];
$titel=$row["titel"];
$eintrag=$row["eintrag"];
$autor=$row["autor"];
$datum=$row["datum"];
?>
<div align="center">
<table width="450" cellspacing="1" cellpadding="3" bgcolor="#000000">
<TR>
<TD bgcolor="#e2e2e2" width="240">
<b>Titel:</b><br> <? echo $titel ?><br>
<b>Eintrag:</b><br> <? echo $eintrag ?><br>
<b>Autor:</b><br> <? echo $autor ?><br>
<b>Letzte &Auml;nderung:</b><br><? echo ($datum) ? datum($datum) : "" ?>
</TD>
</TR>
</table>
</div>
<br><br>
<? } ?>
</body>
</html>

gbook_new.php

<?
include("config.php");
mysql_connect($db_host,$db_user,$db_pass) || die("Could not contact mySQL!");
mysql_select_db($db_db) || die("Connected mySQL, but database is unavailable!");
print("
<html>
<head>
<title>Film Eintragen</title>
<SCRIPT LANGUAGE=JavaScript>
function verify_page()
{
{
document.frmRegister.submit();
}
}
</SCRIPT>
</head>
<BODY bgcolor=#336699>
<BR>
<FORM name=frmRegister action=gbook_new_r.php method=post>
<TABLE align="center" cellspacing="1" cellpadding="3" border="0" bgcolor="#ffffff">
<tr>
<td bgcolor="#336699"><b>Eintrag hinzufügen</b></td>
<td bgcolor="#336699"><A HREF=index.php>Zurück zur Übersicht</A></td>
</TR>
<tr>
<TD bgcolor="#336699"> </TD>
<TD bgcolor="#336699"> </TD>
</TR>
<tr>
<TD bgcolor="#336699">Titel:</TD>
<TD bgcolor="#336699"><INPUT name=titel size="10" maxlength="15"></TD>
</TR>
<tr>
<TD bgcolor="#336699">Eintrag:</TD>
<TD bgcolor="#336699"><textarea name=eintrag cols="30" rows="5"></textarea></TD>
</TR>
<tr>
<TD bgcolor="#336699">Autor:</TD>
<TD bgcolor="#336699"><INPUT name=autor ize="20" maxlength="30"></TD>
</TR>
<tr>
<td bgcolor="#336699"> </TD>
<td bgcolor="#336699"><INPUT type=button value="einfügen" ONCLICK=verify_page()> </TD>
</TR>
</TABLE>
</FORM>
</BODY>
</html>
");
?>

gbook_new_r.php

<?
include("config.php");
mysql_connect($db_host,$db_user,$db_pass) || die("Could not contact mySQL!");
mysql_select_db($db_db) || die("Connected mySQL, but database is unavailable!");

// Werte des Formulars
$titel  = $HTTP_POST_VARS["titel"];   // [1]
$eintrag        = $HTTP_POST_VARS["eintrag"];   // [2]
$autor        = $HTTP_POST_VARS["autor"];     // [3]
$datum        = $HTTP_POST_VARS["datum"];     // [4]

// in DATENBANK einfügen
$sqlInsert  = "INSERT INTO gbook_kino VALUES ('', '$titel', '$eintrag', '$autor', '$datum');";
$result = mysql_query($sqlInsert);
if (mysql_error())
{
echo mysql_error();
exit;
}

// Bestätigung über Eingabe

print("
<html>
<head>
<title>Update erfolgreich</title>
<style>
a:link {color:#ffcc00; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
a:active {color:#ffcc00; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
a:visited {color:#ffcc00; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
a:hover {color:#ffffff; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
td {font-family:arial,helvetica,sans-serif; color:#ffffff; font-size:9pt;}
</style>
</head>
<BODY bgcolor=#336699>
<TABLE align="center" cellspacing="1" cellpadding="3" border="0" bgcolor="#ffffff">
  <tr>
    <td bgcolor="#336699"><b>Insert erfolgreich!</b></td>
  </tr>
  <tr>
  <td bgcolor="#336699"><A HREF=index.php>Zurück zur Übersicht</A></td>
  </tr>
</table>
</BODY>
</html>
");
?>

Wenn ich nun einen neuen Eintrag hinzufüge, wird mir die Datums- und Uhrzeitanzeige zwar im richtigen Format angezeigt, allerdings immer mit null:

0.0.0000 00.00.00

Wenn ich einen Eintrag direkt mit phpMyAdmin in der Mysql Datenbank mache, wird mir der Timestamp richtig angezeigt, über das Eingabeformular aber nicht. Habe schon einiges ausprobiert aber bis jetzt habe ich das Problem noch nicht lösen können. Vielleicht hat jemand eine Idee was ich hier falsch mache.
Danke im voraus für Eure Hilfe.

lg
Andi

  1. Hallo zusammen,
    Ich bin gerade dabei ein Gästebuch zu gestalten und möchte dabei Datum und Zeit des Eintrages mit anzeigen. Das ganze Gästebuch besteht aus folgenden Scripts:

    index.php

    $db = mysql_connect("localhost","root","design");

    ------------------------------------^^^^

    Nanana, sowas macht man aber nicht.

    gbook_new.php

    <FORM name=frmRegister action=gbook_new_r.php method=post>

    <TD bgcolor="#336699"><INPUT name=titel size="10" maxlength="15"></TD>

    <TD bgcolor="#336699"><textarea name=eintrag cols="30" rows="5"></textarea></TD>

    <TD bgcolor="#336699"><INPUT name=autor ize="20" maxlength="30"></TD>

    <td bgcolor="#336699"><INPUT type=button value="einfügen" ONCLICK=verify_page()> </TD>

    So, Du hast hier in Deinem Formular drei Felder (von denen ich mich frage, was sie mit einem Gästebuch zu tun haben, aber das nur nebenbei :) namens titel, eintrag und autor.

    gbook_new_r.php

    // Werte des Formulars
    $titel  = $HTTP_POST_VARS["titel"];   // [1]
    $eintrag        = $HTTP_POST_VARS["eintrag"];   // [2]
    $autor        = $HTTP_POST_VARS["autor"];     // [3]
    $datum        = $HTTP_POST_VARS["datum"];     // [4]

    Und hier zauberst Du noch ein Formularfeld datum hervor, daß aber garnicht da ist.

    // in DATENBANK einfügen
    $sqlInsert  = "INSERT INTO gbook_kino VALUES ('', '$titel', '$eintrag', '$autor', '$datum');";

    Demenstprechend kommt hier bei $datum natürlich auch..

    Wenn ich nun einen neuen Eintrag hinzufüge, wird mir die Datums- und Uhrzeitanzeige zwar im richtigen Format angezeigt, allerdings immer mit null:

    0.0.0000 00.00.00

    ..raus, genau.

    Gruß,
      soenk.e

    1. Hallo Sönke,
      Danke für Deinen Hinweis. Habe die Fehler in den Scripts gefunden. Bezüglich Username und Passwort kann ich Dir sagen das hier keine Sicherheitslücken bestehen da es sich hier nur um eine Testplattform handelt, welch nicht einmal im Intranet ans Netz gekoppelt ist. Hier sind übrigens die überarbeiteten Scripts.

      lg
      Andi

      gbook_new.php

      <?
      include("config.php");
      mysql_connect($db_host,$db_user,$db_pass) || die("Could not contact mySQL!");
      mysql_select_db($db_db) || die("Connected mySQL, but database is unavailable!");
      print("
      <html>
      <head>
      <title>Film Eintragen</title>
      <SCRIPT LANGUAGE=JavaScript>
      function verify_page()
      {
      {
      document.frmRegister.submit();
      }
      }
      </SCRIPT>
      </head>
      <BODY bgcolor=#336699>
      <BR>
      <FORM name=frmRegister action=gbook_new_r.php method=post>
      <TABLE align="center" cellspacing="1" cellpadding="3" border="0" bgcolor="#ffffff">
      <tr>
      <td bgcolor="#336699"><b>Eintrag hinzufügen</b></td>
      <td bgcolor="#336699"><A HREF=index.php>Zurück zur Übersicht</A></td>
      </TR>
      <tr>
      <TD bgcolor="#336699"> </TD>
      <TD bgcolor="#336699"> </TD>
      </TR>
      <tr>
      <TD bgcolor="#336699">Vorname:</TD>
      <TD bgcolor="#336699"><INPUT name=vorname size="10" maxlength="15"></TD>
      </TR>
      <tr>
      <TD bgcolor="#336699">Nachname:</TD>
      <TD bgcolor="#336699"><INPUT name=nachname size="10" maxlength="15"></TD>
      </TR>
      <tr>
      <TD bgcolor="#336699">Email:</TD>
      <TD bgcolor="#336699"><INPUT name=email ize="20" maxlength="30"></TD>
      </TR>
      <tr>
      <TD bgcolor="#336699">Nachricht:</TD>
      <TD bgcolor="#336699"><textarea name=nachricht rows=4 cols=30></textarea></TD>
      </TR>
      <tr>
      <td bgcolor="#336699"> </TD>
      <td bgcolor="#336699"><INPUT type=button value="einfügen" ONCLICK=verify_page()> </TD>
      </TR>
      </TABLE>
      </FORM>
      </BODY>
      </html>
      ");
      ?>

      gbook_new_r.php

      <?
      include("config.php");
      mysql_connect($db_host,$db_user,$db_pass) || die("Could not contact mySQL!");
      mysql_select_db($db_db) || die("Connected mySQL, but database is unavailable!");

      // Werte des Formulars

      $vorname = $HTTP_POST_VARS["vorname"];   // [1]
      $nachname       = $HTTP_POST_VARS["nachname"];  // [2]
      $nachricht      = $HTTP_POST_VARS["nachricht"]; // [3]
      $email        = $HTTP_POST_VARS["email"];     // [4]

      // in DATENBANK einfügen
      $sqlInsert  = "INSERT INTO andi_table (vorname, nachname, nachricht, email) VALUES ('$vorname', '$nachname', '$nachricht', '$email')";
      $result = mysql_query($sqlInsert);
      if (mysql_error())
      {
      echo mysql_error();
      exit;
      }

      // Bestätigung über Eingabe

      print("
      <html>
      <head>
      <title>Update erfolgreich</title>
      <style>
      a:link {color:#ffcc00; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
      a:active {color:#ffcc00; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
      a:visited {color:#ffcc00; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
      a:hover {color:#ffffff; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
      td {font-family:arial,helvetica,sans-serif; color:#ffffff; font-size:9pt;}
      </style>
      </head>
      <BODY bgcolor=#336699>
      <TABLE align="center" cellspacing="1" cellpadding="3" border="0" bgcolor="#ffffff">
        <tr>
          <td bgcolor="#336699"><b>Insert erfolgreich!</b></td>
        </tr>
        <tr>
        <td bgcolor="#336699"><A HREF=index.php>Zurück zur Übersicht</A></td>
        </tr>
      </table>
      </BODY>
      </html>
      ");
      ?>

      index.php

      <html>
      <head>
      <title></title>
      <style>
      a:link {color:#ffcc00; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
      a:active {color:#ffcc00; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
      a:visited {color:#ffcc00; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
      a:hover {color:#ffffff; text-decoration:none; font-family:arial,helvetica,sans-serif; font-size:9pt;}
      td {font-family:arial,helvetica,sans-serif; color:#ffffff; font-size:9pt;}
      </style>
      </head>
      <body bgcolor="#336699">
      <div align="center">
      <table width="450" cellspacing="1" cellpadding="3" bgcolor="#ffffff">
      <tr>
      <td bgcolor="#336699" align="center"><h4>G&aumlstebuch Mondscheinkino</h4></td>
      </tr>
      <tr>
      <td bgcolor="#336699" align="center"><A HREF=gbook_new.php>Neuen Eintrag hinzufügen</A></td>
      </tr>
      </table>
      </div>
      <br><br>
      <?
      function datum($_dat)
      {
      // Wandelt timestamp String in Datum und Uhrzeit um.
      $datum=intval(substr($_dat,6,2)).".".intval(substr($_dat,4,2)).".".substr($_dat,0,4)." um  ".substr($_dat,8,2).":".substr($_dat,10,2).":".substr($_dat,12,2);
      return $datum;
      }
      include("config.php");
      mysql_connect($db_host,$db_user,$db_pass) || die("Could not contact mySQL!");
      mysql_select_db($db_db) || die("Connected mySQL, but database is unavailable!");
      $result=mysql_query("select * from andi_table");
      while($row=mysql_fetch_array($result))
      {
      $id=$row["id"];
      $vorname=$row["vorname"];
      $nachname=$row["nachname"];
      $nachricht=$row["nachricht"];
      $email=$row["email"];
      $datum=$row["datum"];
      ?>
      <div align="center">
      <table width="450" cellspacing="1" cellpadding="3" bgcolor="#ffffff">
      <TR>
      <TD bgcolor="#336699" width="240">
      <b>Name:</b>  <? echo $vorname ?> <? echo $nachname ?> <? echo $email ?><br>
      <b>Nachricht:</b> <? echo $nachricht ?><br>
      <b>Eingetragen am:</b> <? echo ($datum) ? datum($datum) : "" ?>
      </TD>
      </TR>
      </table>
      </div>
      <br><br>
      <? } ?>
      </body>
      </html>