<HTML>
  <HEAD>
    <TITLE>Voting - 2</TITLE>
  </HEAD>

  <BODY>
    <H1>Voting - 2</H1>

    <?
      trim($email);

      if ((!$email) || (!$choice)) {

        echo "<P>You haven't entered your <STRONG>";


        if (!$email) {

          echo "email";

          if (!$choice) {
            echo "</STRONG> or your <STRONG>";
          }

        }

        if (!$choice) {
          echo "choice";
        }

        echo "</STRONG>.</P>";

        echo "<P>Please go back and try again.</P>";
        exit;

      }

    ?>


    <?
      $db = mysql_pconnect("localhost","pannis","sampdb");

      if (!$db) {
        echo "Error: Couldn't connect to database. Please try again later.";
        exit;
      }

      mysql_select_db ("sfdgDb");

      $query = "select * from email where address like \"" . $email . "%\"";

      $result = mysql_query($query);
      $num_results = mysql_num_rows($result);

      if (1 == $num_results) {

        if ("N" == $choice) {
          $choice = 0;
        }

        $row = mysql_fetch_array($result);
 
        $query = "INSERT INTO vote (restaurant_id, voter_id) VALUES ("
                  . $choice . "," . $row["id"] . ")";

        $result = mysql_query($query);

        if ($result) {
          echo mysql_affected_rows() . " vote inserted into database.";
        }

      }

    ?>

    <P>$email is [<? echo $email; ?>]</P>

    <P>$choice is [<? echo $choice; ?>]</P>

  </BODY>
</HTML>