php pdo函数2表通过单个函数

时间:2016-05-11 09:09:24

标签: php mysql pdo

通过管理面板添加服务器,当创建服务器时应该在其他基础创建另一个表..

服务器的基本名称是“服务器” 您需要添加的第二个表的基本名称是“billing”

我想添加一个函数来在结算中打印一个额外的表,如果有可能的话......因此,它会自动

SQL:

-- --------------------------------------------------------

--
-- Table structure for table `billing`
--

CREATE TABLE IF NOT EXISTS `billing` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Status` varchar(255) NOT NULL,
  `UserID` varchar(255) NOT NULL,
  `ServerID` varchar(255) NOT NULL,
  `Hostname` varchar(255) NOT NULL,
  `PaymentDeadline` datetime NOT NULL,
  `CreateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `servers`
--

CREATE TABLE IF NOT EXISTS `servers` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `UserID` varchar(255) NOT NULL,
  `BoxID` varchar(255) NOT NULL,
  `TemplateID` varchar(255) NOT NULL,
  `Type` varchar(255) NOT NULL,
  `Status` varchar(255) NOT NULL,
  `Hostname` varchar(255) NOT NULL,
  `ServerIP` varchar(255) NOT NULL,
  `ServerPort` varchar(255) NOT NULL,
  `SlotNumber` varchar(255) NOT NULL,
  `Expires` datetime NOT NULL,
  `Price` varchar(255) NOT NULL,
  `Username` varchar(255) NOT NULL,
  `Password` varchar(255) NOT NULL,
  `CreateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

添加服务器funcktion:

<?php
  session_start();
  $ROOT = realpath(__DIR__);
include $ROOT.'/../core/config.php';
  include 'log.php';
  set_include_path($ROOT.'/../SSH2');
  include 'Net/SSH2.php';
  set_include_path('');
  $PagePermision = '2';
  include 'session.php';

  try {
    $SQL = 'SELECT * FROM Users ORDER BY ID DESC';
    $SQL = $CONN->prepare($SQL);
    $SQL->execute();
    $Clients = $SQL->fetchAll();
  } catch(PDOException $e) {
     echo "Error: " . $e->getMessage();
  }

  try {
    $SQL = 'SELECT * FROM Games';
    $SQL = $CONN->prepare($SQL);
    $SQL->execute();
    $Games = $SQL->fetchAll();
  } catch(PDOException $e) {
     echo "Error: " . $e->getMessage();
  }

  try {
    $SQL = 'SELECT * FROM Boxes';
    $SQL = $CONN->prepare($SQL);
    $SQL->execute();
    $Boxes = $SQL->fetchAll();
  } catch(PDOException $e) {
     echo "Error: " . $e->getMessage();
  }

  if(isset($_GET['GameID'])) {
    $GameID = $_GET['GameID'];
  } else {
    $GameID = NULL;
  }

  try {
    $SQL = 'SELECT * FROM Templates WHERE GameID = :GameID ORDER BY ID ASC';
    $SQL = $CONN->prepare($SQL);
    $SQL->execute(array('GameID' => $GameID));
    $Mods = $SQL->fetchAll();
  } catch(PDOException $e) {
     echo "Error: " . $e->getMessage();
  }

  if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $UserID = $_POST['UserID'];
    $BoxID = $_POST['BoxID'];
    $TemplateID = $_POST['TemplateID'];
    $Hostname = $_POST['Hostname'];
    $SlotNumber = $_POST['SlotNumber'];
    $Expires = $_POST['Expires'];
    $Price = $_POST['Price'];

    try {
      $SQL = 'SELECT * FROM Boxes WHERE ID = :ID';
      $SQL = $CONN->prepare($SQL);
      $SQL->execute(array('ID' => $BoxID));
      $BoxData = $SQL->fetch(PDO::FETCH_ASSOC);
    } catch(PDOException $e) {
       echo "Error: " . $e->getMessage();
    }

    try {
      $SQL = 'SELECT * FROM Templates WHERE ID = :ID';
      $SQL = $CONN->prepare($SQL);
      $SQL->execute(array('ID' => $TemplateID));
      $TemplateData = $SQL->fetch(PDO::FETCH_ASSOC);
    } catch(PDOException $e) {
       echo "Error: " . $e->getMessage();
    }

    try {
      $SQL = 'SELECT ServerPort FROM Servers INNER JOIN Templates ON Templates.ID = Servers.TemplateID WHERE Templates.BoxID = :BoxID AND Templates.GameID = :GameID ORDER BY Servers.ServerPort DESC LIMIT 1';
      $SQL = $CONN->prepare($SQL);
      $SQL->execute(array('BoxID' => $BoxID, 'GameID' => $TemplateData['GameID']));
      $ServerData = $SQL->fetch(PDO::FETCH_ASSOC);
      $ServerPort = $ServerData['ServerPort'];
    } catch(PDOException $e) {
       echo "Error: " . $e->getMessage();
    }

    $ServerPort = $ServerPort + 1;

    try {
      $SQL = 'SELECT Username FROM Servers WHERE BoxID = :BoxID ORDER BY Username DESC LIMIT 1';
      $SQL = $CONN->prepare($SQL);
      $SQL->execute(array('BoxID' => $BoxID));
      $ServerData = $SQL->fetch(PDO::FETCH_ASSOC);
      $Username = $ServerData['Username'];
    } catch(PDOException $e) {
       echo "Error: " . $e->getMessage();
    }

    $Username = filter_var($Username, FILTER_SANITIZE_NUMBER_INT) + 10;
    $Username = 'SRV'.$Username;

    function RandomPassword() {
      $Alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
      $Pass = array();
      $AlphaLength = strlen($Alphabet) - 1;
      for ($I = 0; $I < 10; $I++) {
        $N = rand(0, $AlphaLength);
        $Pass[] = $Alphabet[$N];
      }
      return implode($Pass);
    }

    $Password = RandomPassword();

    try {
      $SQL = 'INSERT INTO Servers (UserID, BoxID, TemplateID, Type, Status, Hostname, ServerIP, ServerPort, SlotNumber, Expires, Price, Username, Password) VALUES (:UserID, :BoxID, :TemplateID, :Type, :Status, :Hostname, :ServerIP, :ServerPort, :SlotNumber, :Expires, :Price, :Username, :Password)';
      $SQL = $CONN->prepare($SQL);
      $SQL->execute(array('UserID' => $UserID, 'BoxID' => $BoxID, 'TemplateID' => $TemplateID, 'Type' => '1', 'Status' => '0', 'Hostname' => $Hostname, 'ServerIP' => $BoxData['BoxIP'], 'ServerPort' => $ServerPort, 'SlotNumber' => $SlotNumber, 'Expires' => $Expires, 'Price' => $Price, 'Username' => $Username, 'Password' => $Password));
    } catch(PDOException $e) {
       echo "Error: " . $e->getMessage();
    }

    $SSH2_Host = $BoxData['BoxIP'];
    $SSH2_Username = $BoxData['Username'];
    $SSH2_Password = $BoxData['Password'];

    $SSH2 = new Net_SSH2($SSH2_Host);
    if (!$SSH2->login($SSH2_Username, $SSH2_Password)) {
      echo 'SSH2 Login Error!';
    }
    $SSH2->enablePTY();

    $SSH2->exec('screen -S "'.$Username.'"');
    $SSH2->setTimeout(1);
    $SSH2->write("useradd -s /usr/sbin/nologin -m ".$Username."\n");
    $SSH2->setTimeout(1);
    $SSH2->write("echo ".$Username.":".$Password." | chpasswd\n");
    $SSH2->setTimeout(1);
    $SSH2->write("cp -R ".$TemplateData['FilesPath']."* /home/".$Username."\n");
    $SSH2->setTimeout(1);
    $SSH2->write("chown -R ".$Username.":".$Username." /home/".$Username." && exit\n");
    $SSH2->read();

    LogMessage('Server Addet! User: '.$UserID.'.', $CONN);

    header("location: servers.php");
    exit();
  }

  include 'layout/header.php';
?>

0 个答案:

没有答案