<?php
$dbcnx = @mysql_connect("213.171.218.228", "blacm", "rainySeason");
if (!$dbcnx) {
  echo( "<P>Unable to connect to the " .
        "database server at this time.</P>" );
  exit();
}
echo "connection...OK<br>";

if (! @mysql_select_db("atmosphere") ) {
  echo( "<P>Unable to locate the atmosphere " .
        "database at this time.</P>" );
  exit();
}

echo "found atmosphere database...OK";

     $fields=mysql_list_fields("atmosphere","tblMoves",$dbcnx);
     $columns=mysql_num_fields($fields);
     
     for($i=0;$i<$columns;$i++)
     {
        echo mysql_field_name($fields,$i)."\n";
     }


    $result = mysql_query("SELECT COUNT(*) FROM tblMagnets");
    if (!$result) {
      echo("<P>Error performing query: " .
           mysql_error() . "</P>");
      exit();
    }
    while ($row=mysql_fetch_row($result)){
          if($row[0]<=30)
          {
             //30 or less - ok to proceed
             //calculate expiry date, now + 1 week(in seconds)
             $ts=time();
             $ts=$ts+604800;
             echo($ts);
             $add_magnet="INSERT INTO tblMagnets SET Expires=FROM_UNIXTIME($ts)";
             if (mysql_query($add_magnet))
             {
                echo("<P>Your magnet has been added.</P>");
                //set cookie using id (cast as string) as value
                
                setCookie("magnet","".mysql_insert_id(),$ts);
                //call javascript function to show magnet and set initial position
                //echo("cookie value=".$_COOKIE['magnet']);
                echo("magnet id=".mysql_insert_id());
             }
             else
             {
                echo("<P>Error adding magnet: " .
                mysql_error() . "</P>");
             }


          }
          else
          {
             //no magnets available - try again when next magnet expires
             echo("There are no magets available");
          }
    }
    mysql_free_result($result);
?>

<HTML>
<HEAD>
</HEAD>
<BODY>
</BODY>
</HTML>
