【问题标题】:Is it possible to create a database, user, password using php and a form (with only cpanel, no WHM)是否可以使用 php 和表单创建数据库、用户、密码(只有 cpanel,没有 WHM)
【发布时间】:2019-06-25 04:43:00
【问题描述】:

我可以连接但不能创建数据库。 Connected to Database

 <body>
   <?php
    //Database Connection Variables

     $servername = "localhost";
     $username = "******";
     $password = "********";
     $conn = new mysqli($servername, $username, $password);
    //The Connection and test
     if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
     }
     echo "Connected successfully";
     //If the form is submitted create the database
     if ($_SERVER["REQUEST_METHOD"] == "POST") {
     $dbname = $_POST['dbname']; 
     $user = $_POST['user'];
     $password = $_POST['password'];
     $sql = "CREATE DATABASE $dbname";
     $sql = "CREATE USER $user";
     $sql = "GRANT ALL PRIVILEGES ON $dbname.* To '$user' IDENTIFIED BY '$password'";
    if ($conn->query($sql) === FALSE) {
    echo "Error creating database: " . $conn->error;
    }
    }
    ?>

   <div class="container">

   <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
   <div class="row">

   <div class="col-25">
     <label for="dbname">New DB Name: </label>
   </div>

   <div class="col-75">
     <input type="text" name="dbname" placeholder="Enter New Database Name">

  </div>


  <div class="col-25">
     <label for="user">New User Name: </label>
   </div>

   <div class="col-75">
     <input type="text" name="user" placeholder="Enter New User Name">

  </div>


<div class="col-25">
     <label for="password">New Password: </label>
   </div>

   <div class="col-75">
     <input type="text" name="password" placeholder="Enter New Password">
     <input type="submit" name="submit" value="GO" />
  </div>

  </div>
  </div>
  </form>

问题(我认为)是我只有 cpanel 而没有 WHM,所以我没有 root mySQL 密码。我想通过一个表单来完成这一切,创建数据库、用户、密码、表格、上传数据等。

目标是让没有技术技能的最终用户能够通过表单在营销数据库中创建、查看和添加数据。

【问题讨论】:

  • 您只执行GRANT,因为所有3条语句都使用$sql,并且只执行最后一条。
  • 使用多查询mysqli::multi_query
  • 根据问题,我认为不会,它会在执行查询时检查您的数据库用户,并且它会得到您的许可,如果您对这些查询有许可,您就可以做到。如果您的用户有权,那么它可能

标签: php mysql mysqli


【解决方案1】:

所以事实证明 not 拥有 WHM root sql 密码是有限制的。仅使用 cPanel 即可。但您必须在服务器上加载 xmlapi.php [链接如下]。

<body>
<?php
//Database Connection Variables

 include("xmlapi.php"); //your need to have this on the server same folder 

 $db_host = 'yourdomain.com'; //your URL
 $cpaneluser = 'yourcpaneluser'; // the user you use to sign into cpanel
 $cpanelpass = 'yourcpanelpassword'; // the cpanel user password
 ///If the form is submitted create the database
 if ($_SERVER["REQUEST_METHOD"] == "POST") {
 $databasename = $_POST['dbname']; 
 $databaseuser = $_POST['user'];
 $databasepass = $_POST['password'];}
 $xmlapi = new xmlapi($db_host);    
 $xmlapi->password_auth("".$cpaneluser."","".$cpanelpass."");    
 $xmlapi->set_port(2082);
 $xmlapi->set_debug(1);//output actions in the error log 1 for true and 0 false  
 $xmlapi->set_output('array');//set this for browser output  
 //create database    
 $createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", 
 array($databasename));   
 //create user 
 $usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($databaseuser,      
 $databasepass));   
 //add user 
 $addusr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduserdb", 
 array("".$cpaneluser."_".     $databasename."", "".$cpaneluser."_".$databaseuser."", 
 'all'));
 ?>




<div class="container">
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="row">

   <div class="col-25">
     <label for="dbname">New DB Name: </label>
   </div>
<div class="col-75">
     <input type="text" name="dbname" placeholder="Enter New Database Name">
</div>


  <div class="col-25">
     <label for="user">New User Name: </label>
   </div>

   <div class="col-75">
     <input type="text" name="user" placeholder="Enter New User Name">

  </div>


<div class="col-25">
     <label for="password">New Password: </label>
   </div>

   <div class="col-75">
     <input type="text" name="password" placeholder="Enter New Password">
     <input type="submit" name="submit" value="GO" />
  </div>

  </div>
  </div>
  </form>

你可以在这里下载xmlapihttps://github.com/CpanelInc/xmlapi-php/blob/master/xmlapi.php

【讨论】:

    猜你喜欢
    • 2019-07-27
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2013-12-24
    • 2011-03-01
    • 1970-01-01
    • 2016-05-06
    相关资源
    最近更新 更多