[英]Is it possible to create a database, user, password using php and a form (with only cpanel, no WHM)
I can connect but not create the database. 我可以连接但不能创建数据库。 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>
The issue (I think) is I only have cpanel and do not have WHM so I do not have a root mySQL password. 问题(我认为)是我只有cpanel而没有WHM,所以我没有root mySQL密码。 I want to do this all via a form, create database, users, passwords, tables, upload data, etc. 我想通过表单完成所有操作,创建数据库,用户,密码,表,上传数据等。
The goal is to have an end user with no technical skills be able to create, view, add data in a marketing database via a form. 目标是使没有技术技能的最终用户能够通过表单在营销数据库中创建,查看和添加数据。
So it turns out not having a WHM root sql password is limiting. 因此,事实证明, 没有 WHM根sql密码是有限制的。 Using only cPanel this works. 仅使用cPanel即可。 but you must have the xmlapi.php loaded on your server [link below]. 但您必须在服务器上加载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>
You can download the xmlapi here https://github.com/CpanelInc/xmlapi-php/blob/master/xmlapi.php 您可以在这里https://github.com/CpanelInc/xmlapi-php/blob/master/xmlapi.php下载xmlapi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.