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. 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. Using only cPanel this works. but you must have the xmlapi.php loaded on your server [link below].
<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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.