如何在cpanel中自动编辑mysql数据库

jmo0nnb3  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(240)

我正在尝试建立一个登录系统,并使用namesheap将其托管在一个web服务器上。我决定使用cpanel mysql数据库来保存登录值(用户名、密码等),但是,我无法向mysql数据库添加值。我可以很容易地手动添加到数据库,但是如何使用代码添加(最好是python,但任何语言都可以)。
我没有任何尝试,因为我没有找到任何显示如何做到这一点。

f5emj3cl

f5emj3cl1#

我找到了一个使用php的解决方案,它和python中的一样好。

<?php

$username = $_POST['username'];
$password = $_POST['password'];

$servername = "localhost";
$server_username = "animfqrw_loginUsername";
$server_password = "loginPassword";
$dbname = "animfqrw_userLogin";
$userID = 1;

//Connects to the mysql database
$conn = new mysqli($servername, $server_username, $server_password, $dbname);

//Ends php code if php fails to connect to mysql database
if ($conn->connect_error) {
    die("Error");
    break;
} 

//Finds the last User_ID
$sql = "SELECT User_ID FROM Users ORDER BY User_ID DESC LIMIT 1;";
$result = $conn->query($sql);

//Fetches the last User ID so that the User ID's are all in order
while($row = $result->fetch_assoc()) {
    $userID = $row["User_ID"] + 1;
}

//Add's the row to the mysql database
$sql = "INSERT INTO Users (User_ID, Username, Password)
VALUES ($userID, '$username', '$password')";
$conn->query($sql);

?>

相关问题