Location: PHPKode > scripts > CD-Stock Database Script > cd-stock-database-script/cdstock.php
<html>
<style type="text/css">
<!--
td {  font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
tr {  font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
p {  font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
form {  font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
h1 {  font-family: Arial, Helvetica, sans-serif; font-size: 13pt}
h2 {  font-family: Arial, Helvetica, sans-serif; font-size: 12pt}
h3 {  font-family: Arial, Helvetica, sans-serif; font-size: 11pt}
body   {  font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
a {  font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: blue}

.AddTitel {width:100px}
.AddAuthor {width:100px}
.AddRubrik {width:100px}
.AddDatum {width:100px}
.AddEmail {width:100px}
.AddUrl {width:100px}
.AddKommentar {width:100px}
.AddGlobal {width:100px}

.w50 {width:50px}
.w100 {width:100px}
.w200 {width:300px}
.w300 {width:300px}
.w400 {width:400px}

#AddTable {background-color:#cccccc; width:400px}
-->
</style>

<body>
<?php
// Script: cdstock.php
// Topic:  CD-Stock Database Script
// Ver.:   1.0
// Update: 16-5-2000
// Author: hide@address.com
// Notice: modify and use!
/*
CREATE TABLE cdstock(
  id SMALLINT NOT NULL AUTO_INCREMENT,
  titel CHAR(30) NOT NULL,
  author CHAR(30) NOT NULL,
  datum CHAR(30) NULL,
  rubrik CHAR(30) NULL,
  email CHAR(30) NULL,
  url VARCHAR(40),
  kommentar VARCHAR(100) NULL,
  bild CHAR(20) NOT NULL,
  PRIMARY KEY(id),
  KEY(titel,author)
)
*/
// TODO:
// - remove-picture selection on zoom item
// - graphic file type regognition

// includions ***************************************************************************
require("shared.inc.php");

// functions ****************************************************************************
// none yet

// db specs *****************************************************************************
$host = "localhost";
$user = "root";
$db = "yourdatabase";
$pwd = "";

// globals ******************************************************************************
// modify your absolut directories
$tmp_path = "c:\\apache\\htdocs\\tmp\\";
$upload_path = "c:\\apache\\htdocs\\cdstock\\upload\\";
?>

<?php /* add item    **************************************************************/ ?>
<?php if (isset($cmd) && $cmd == "add"): ?>
<?php
  $conn = mysql_connect($host,$user,$pwd);
  if(!$conn) echo "connection failed<br>";
  $select = mysql_select_db("$db",$conn) or die ("database not found");

  if($userfile != "none"):
    $file = basename($userfile);
    $pct_id = substr($titel,0,2);
    $zeit = date("His");
    $bild = $pct_id.$zeit.".gif";
    if (!copy($tmp_path.$file, $upload_path.$bild)) echo "failed to copy file<br>\n";
  endif;

  $sql  = "INSERT INTO cdstock ";
  $sql .= "(titel,author,rubrik,datum,url,email,kommentar,bild) ";
  $sql .= "VALUES ";
  $sql .= "('$titel','$author','$rubrik','$datum','$url','$email','$kommentar','$bild')";
  $result = mysql_query($sql,$conn);
  mysql_close($conn);
  ?>
  <br>

  <h3>Die Datenbank wird aktualisiert ...</h3>

  <script language="JavaScript">
  var count= 0

  function warten() {
    count ++
    if(count == 1 ){           // delay of seconds: 1
      window.document.location.href='cdstock.php?cmd=list'
    }
    else{
      setTimeout("warten()",1000)
    }
  }

  warten();
  </script>
<?php endif; ?>

<?php /* mod item    **************************************************************/ ?>
<?php if (isset($cmd) && $cmd == "mod"): ?>
<?php
  $conn = mysql_connect($host,$user,$pwd);
  if(!$conn) echo "connection failed<br>";
  $select = mysql_select_db("$db",$conn) or die ("database not found");

  if($userfile != "none"):
    $sql  = "SELECT bild FROM cdstock WHERE id = '$id'";
    $result = mysql_query($sql,$conn);
    $trash = mysql_result($result,$count,"bild");
    if(isset($trash)) @unlink($upload_path.$trash);                      // delete old picture

    $newfile = basename($userfile);
    $pct_id = substr($titel,0,2);
    $zeit = date("His");
    $bild = $pct_id.$zeit.".gif";
    if (!copy($tmp_path.$newfile, $upload_path.$bild)) echo "failed to copy file<br>\n";
    $fields = "bild='$bild'";
    $sql = "UPDATE cdstock SET $fields WHERE id='$id'";                // update only, if nescessary
    $result = mysql_query($sql,$conn);
  endif;

  $fields  = "titel='$titel',";
  $fields .= "author='$author',";
  $fields .= "rubrik='$rubrik',";
  $fields .= "datum='$datum',";
  $fields .= "url='$url',";
  $fields .= "email='$email',";
  $fields .= "kommentar='$kommentar'";

  $sql = "UPDATE cdstock SET $fields WHERE id='$id'";
  $result = mysql_query($sql,$conn);
  mysql_close($conn);
  ?>
  <br>
<h3>Die Datenbank wird aktualisiert ...</h3>

  <script language="JavaScript">
  var count= 0

  function warten() {
    count ++
    if(count == 1 ){
      window.document.location.href='cdstock.php?cmd=list'
    }
    else{
      setTimeout("warten()",1000)
    }
  }

  warten();
  </script>
<?php endif; ?>

<?php /* del item    **************************************************************/ ?>
<?php if (isset($cmd) && $cmd == "del"): ?>
<?php
  $conn = mysql_connect($host,$user,$pwd);
  if(!$conn) echo "connection failed<br>";

  $select = mysql_select_db("$db",$conn) or die ("database not found");
  $sql  = "SELECT * FROM cdstock WHERE ";
  $sql .= "id = '$id'";
  $result = mysql_query($sql,$conn);

  $trash = mysql_result($result,$count,"bild");       // delete picture
  $tmp_path = "c:\\apache\\htdocs\\cdstock\\upload\\";
  @unlink($tmp_path.$trash);

  $sql  = "DELETE FROM cdstock WHERE ";               // delete db-entry
  $sql .= "id = '$id'";
  $result = mysql_query($sql,$conn);

  mysql_close($conn);
  ?>
  <br>

<h3>Die Datenbank wird aktualisiert ...</h3>

  <script language="JavaScript">
  var count= 0

  function warten() {
    count ++
    if(count == 1 ){
      window.document.location.href='cdstock.php?cmd=list'
    }
    else{
      setTimeout("warten()",1000)
    }
  }

  warten();
  </script>
<?php endif; ?>

<?php /* search items  *****************************************************************/ ?>
<?php if (isset($cmd) && $cmd == "search"): ?>
  <?php
  commonHeader("");
  ?>
  <h2>CD Suchresultate</h2>

  <script language="JavaScript">
  var count= 0

  function warten() {
    count ++
    if(count == 1 ){
      window.document.location.href='search.php'
    }
    else{
      setTimeout("warten()",250)   // a quater of a second
    }
  }
  warten();
  </script>
  <?php
  commonFooter();
  ?>
<?php endif; ?>

<? /* list items  *****************************************************************/ ?>
<? if (isset($cmd) && $cmd == "list"): ?>
  <?php
  commonHeader("");
  ?>
  <h2>CD &Uuml;bersicht</h2>
  <?php
  $conn = mysql_connect($host,$user,$pwd);
  if(!$conn) echo "connection failed";

  $select = mysql_select_db("$db",$conn) or die ("database not found");
  $sql = "SELECT * FROM cdstock ORDER BY id";
  $result = mysql_query($sql,$conn);

  if($result):
  ?>

<table border="0" cellpadding="1" cellspacing="0" bgcolor="#DDDDDD" width="400">
  <?php
  $total_rows = mysql_num_rows($result);
  $count = 0;
  ?>
  <TR bgcolor="#cccccc">
    <TD style="width:20px"><b>ID</b></TD>
    <TD style="width:120px"><b>Titel</b></TD>
    <TD style="width:100px"><b>Author</b></TD>
    <TD style="width:100px"><b>Rubrik</b></TD>
    <TD><b>Bild</b></TD>
    <TD><b></b></TD>
  </TR>
  <?php
  while($count < $total_rows):
    ?>
    <TR>
    <TD>
    <?php
      $id = mysql_result($result,$count,"id");
      echo $id; ?>
    </TD>
    <TD> <?php
      $titel = mysql_result($result,$count,"titel"); ?>
	  <a href="cdstock.php?cmd=zoom&id=<?php echo $id; ?>" title="Titel: <?php echo $titel; ?>">
	  <?php echo $titel; ?></a> 
    </TD>
    <TD><?php echo mysql_result($result,$count,"author"); ?></TD>
    <TD><?php echo mysql_result($result,$count,"rubrik"); ?></TD>
    <TD><?php
      $path = "/cdstock/upload/";
      $string = mysql_result($result,$count,"bild");
      if($string != ""):  // test if there is a picture
      ?>
      <img src="<?php echo $path.$string; ?>"width="15" height="15">
      <?php
      endif;
      ?>
      </TD>
    <TD align="right">
	  <a href="cdstock.php?cmd=del&id=<?php echo $id; ?>">
	  <img src="images/erase.gif" width="15" height="15" border="0" alt="delete entry!" name="erase.gif"> 
      </a> </TD>
    </TR>
    <?php
    $count++;
  endwhile;
  ?>
  </table>
  <?php
  else:
    echo "Datenbank ist noch leer<br>";
  endif;
  commonFooter("");
  ?>
<?php endif; ?>

<? /* zoom items  *****************************************************************/ ?>
<? if (isset($cmd) && $cmd == "zoom"): ?>
  <?php
  commonHeader("");
  ?>
  <h2>CD Einzelansicht</h2>
  <?php
  $conn = mysql_connect($host,$user,$pwd);
  if(!$conn) echo "connection failed";

  $select = mysql_select_db("$db",$conn) or die ("database not found");
  $sql = "SELECT * FROM cdstock WHERE id = '$id'";
  $result = mysql_query($sql,$conn);

  if($result):
  ?>
  <form action="cdstock.php" method="post" enctype="multipart/form-data">
  <input type="hidden" name="cmd" value="mod">
  <input type="hidden" name="id" value="<?php echo $id; ?>">
  <table border="0" cellpadding="1" cellspacing="0" bgcolor="#dddddd">
    <?php
  $total_rows = mysql_num_rows($result);
  $count = 0;
  ?>
    <TR>
      <TD style="width:60px"><b>ID</b></TD>
      <TD style="width:330px"> <?php
      $id = mysql_result($result,$count,"id");
      echo $id; ?> </TD>
    </TR>
    <TR>
      <TD style="width:60px"><b>Titel</b></TD>
      <TD style="width:330px">
        <input type="text" name="titel" class=w200
      value="<?php
      $string = mysql_result($result,$count,"titel");
      echo $string; ?>">
      </TD>
    </TR>
    <TR>
      <TD style="width:60px"><b>Author</b></TD>
      <TD style="width:330px">
        <input type="text" name="author" class=w200
      value="<?php
      $string = mysql_result($result,$count,"author");
      echo $string; ?>">
      </TD>
    </TR>
    <TR>
      <TD style="width:60px"><b>Rubrik</b></TD>
      <TD style="width:330px">
        <input type="text" name="rubrik" class=w200
      value="<?php
      $string = mysql_result($result,$count,"rubrik");
      echo $string; ?>">
      </TD>
    </TR>
    <TR>
      <TD style="width:60px"><b>Datum</b></TD>
      <TD style="width:330px">
        <input type="text" name="datum" class=w200
      value="<?php
      $string = mysql_result($result,$count,"datum");
      echo $string; ?>">
      </TD>
    </TR>
    <TR>
      <TD style="width:60px"><b>E-mail</b></TD>
      <TD style="width:330px">
        <input type="text" name="email" class=w200
      value="<?php
      $string = mysql_result($result,$count,"email");
      echo $string; ?>">
      </TD>
    </TR>
    <TR>
      <TD style="width:60px"><b>URL</b></TD>
      <TD style="width:330px">
        <input type="text" name="url" class=w200
      value="<?php
      $string = mysql_result($result,$count,"url");
      echo $string; ?>">
      </TD>
    </TR>
    <TR>
      <TD style="width:60px"><b>Kommentar</b></TD>
      <TD style="width:330px">
        <textarea type="text" name="kommentar" class=w200><?php
      $string = mysql_result($result,$count,"kommentar");
      echo $string; ?></textarea>
      </TD>
    </TR>
    <TR>
      <TD style="width:60px">&nbsp;</TD>
      <TD style="width:330px">&nbsp;</TD>
    </TR>
    <TR>
      <TD style="width:60px">&nbsp;</TD>
      <TD style="width:330px" align="center"><?php
            $test = mysql_result($result,$count,"bild");
            if(!empty($test)):
              $string = "/cdstock/upload/";
              $string .= mysql_result($result,$count,"bild"); ?><img src="<?php echo $string; ?>">
        <?php
            else:
              echo "Kein Bild vorhanden";
            endif;
            ?> </TD>
    </TR>
    <TR>
      <TD style="width:60px" valign="bottom"><b>Bild:</b></TD>
      <TD style="width:330px"> <br>
        <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="100000">
        <INPUT NAME="userfile" TYPE="file">
      </TD>
    </TR>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <TR>
      <TD colspan="2" align="center">
        <input type="submit" name="change" value="&auml;ndern">
      </TD>
    </TR>
  </table>
  </form>
  <?php
  else:
    echo "Datenbank ist noch leer<br>";
  endif;
  ?>
<?php endif; ?>

<? /* form      *****************************************************************/ ?>
<? if (isset($cmd) && $cmd == "form"): ?>
<?php
  commonHeader("");
?>
<form action="cdstock.php" method="post" enctype="multipart/form-data">
  <input type="hidden" name="cmd" value="add">
  <table border="0" cellpadding="2" cellspacing="1" ID=AddTable >
    <TR>
  <TD class=AddTitel><b>Titel</b></TD>
  <TD class=AddGlobal><b>Author</b></TD>
  <TD class=AddGlobal><b>Rubrik</b></TD>
  </TR>
  <TR>
    <TD><input type="text" name="titel" class=w100></TD>
    <TD><input type="text" name="author" class=w100></TD>
      <TD>
        <select name="rubrik" class=w100>
          <option selected>jazz</option>
          <option>rock</option>
          <option>funk</option>
          <option>klassisch</option>
          <option>ernst</option>
          <option>folklore</option>
        </select>
      </TD>
  </TR>
  <TR>
  <TD class=AddTitel><b>Datum</b></TD>
  <TD class=AddGlobal><b>URL</b></TD>
  <TD class=AddGlobal><b>email</b></TD>
  </TR>
  <TR>
    <TD><input type="text" name="datum" class=w100></TD>
    <TD><input type="text" name="url" class=w100></TD>
    <TD><input type="text" name="email" class=w100></TD>
  </TR>
  <TR>
    <TD colspan="4"><b>Kommentar</b></TD>
  </TR>
    <TR align="left">
      <TD colspan="4">
        <textarea name="kommentar" rows="5" cols="43"></textarea>
      </TD>
  </TR>
  <TR align="left">
    <TD colspan="4">
      <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="100000">
        <b>Bild: </b>
      <INPUT NAME="userfile" TYPE="file">
    </TD>
  </TR>
    <TR align="center">
      <TD colspan="4">
        <input type="submit" name="submit" value="senden">
      </TD>
  </TR>
  </table>
  </form>
<? endif; ?>

<? /* if no cmd   *****************************************************************/ ?>
<? if ($cmd == ""): ?>
<?php
  commonHeader("");
?>
  <H3>Willkommen zur CD Datenbank</H3><br>
  <FORM METHOD="get" ACTION="cdstock.php<?php echo urlencode("$QUERY_STRING"); ?>">
  <INPUT type="radio" name="cmd" value="list" CHECKED>CDs auflisten<BR>
  <INPUT type="radio" name="cmd" value="form">CD hinzuf&uuml;gen<p>
  <INPUT type="submit" value="weiter">
  </FORM>

<?php endif; ?>

</body>
</html>
Return current item: CD-Stock Database Script