if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
}
if (isset($_GET['action'])) {
    if ($_GET['action'] == "new") {
        if ($_GET['processed'] == 1) {
            $packageid = $_POST['packageid'];
            $packageid = $mysqli->real_escape_string($packageid);
            $packagename = $_POST['packagename'];
            $packagename = $mysqli->real_escape_string($packagename);
            $cost = $_POST['cost'];
            $cost = $mysqli->real_escape_string($cost);
            $email = $_POST['email'];
            $email = $mysqli->real_escape_string($email);
            $newkey = get_new_key();
            $transid = mt_rand();
            $insertkey = "INSERT INTO `activationkeys` (`ID`, `transid`, `activationkey`, `used`, `type`, `package`) VALUES (NULL, '" . $transid . "', '" . $newkey . "', '0', '" . $packageid . "', '" . $packagename . "');";
            $result = $mysqli->query($insertkey) or die($mysqli->error . __LINE__);
            $inserttrans = "INSERT INTO `notifications` (`ID`, `item_name`, `item_number`, `payment_status`, `payment_amount`, `payment_currency`, `transaction_id`, `receiver_email`, `payer_email`) VALUES (NULL, '" . $packagename . "', '" . $packageid . "', 'MANUAL', '" . $cost . "', 'USD', '" . $transid . "', 'MANUAL', '" . $email . " ');";
            $result2 = $mysqli->query($inserttrans) or die($mysqli->error . __LINE__);
        }
        echo '
   <form class="form-horizontal" action="dash.php?action=new&processed=1" method="post">
<fieldset>

<!-- Form Name -->
<legend>Add new key</legend>

<!-- Text input-->
<div class="form-group">
 }
 /* make your connection */
 $sql = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
 if (mysqli_connect_errno()) {
     $error = "Failed to connect to MySQL: " . mysqli_connect_error();
     file_put_contents($file, $error, FILE_APPEND | LOCK_EX);
 }
 $trans = "INSERT INTO notifications VALUES( DEFAULT, '{$item_name}','{$item_number}','{$payment_status}','{$payment_amount}','{$payment_currency}','{$txn_id}','{$receiver_email}','{$payer_email}')";
 if ($sql->query($trans)) {
     $status = "[" . date("m.d.y H:i:s") . "] A new transaction has been added with the `id` of {$sql->insert_id}.\n";
     file_put_contents($file, $status, FILE_APPEND | LOCK_EX);
 } else {
     $status = "[" . date("m.d.y H:i:s") . "] -ERROR- There was a problem:<br />{$query}<br />{$sql->error}\n";
     file_put_contents($file, $status, FILE_APPEND | LOCK_EX);
 }
 $key = get_new_key();
 $addcode = 'INSERT INTO activationkeys VALUES(DEFAULT, "' . $txn_id . '", "' . $key . '", "0", "' . $item_number . '", "' . $item_name . '")';
 if ($sql->query($addcode)) {
     $status = "[" . date("m.d.y H:i:s") . "] A new key has been added with the `id` of {$sql->insert_id}.\n";
     file_put_contents($file, $status, FILE_APPEND | LOCK_EX);
 } else {
     $status = "[" . date("m.d.y H:i:s") . "] -ERROR- There was a problem:<br />{$query}<br />{$sql->error}\n";
     file_put_contents($file, $status, FILE_APPEND | LOCK_EX);
 }
 $status = "[" . date("m.d.y H:i:s") . "] End of notification \n";
 file_put_contents($file, $status, FILE_APPEND | LOCK_EX);
 /* close our connection */
 $sql->close();
 // Email messages
 $keyemail = '
 <html>