代码之家  ›  专栏  ›  技术社区  ›  Aniket G

如何在Cpanel中自动编辑MySQL数据库

  •  0
  • Aniket G  · 技术社区  · 7 年前

    我正在尝试建立一个登录系统,并使用namescape将其托管在一个web服务器上。我决定使用Cpanel MySQL数据库来保存登录值(用户名、密码等),但是,我无法向MySQL数据库添加值。我可以轻松地手动添加到数据库中,但如何使用代码添加(最好是python,但任何语言都可以)。

    我以前没有任何尝试,因为我没有找到任何显示如何做到这一点的东西。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Aniket G    7 年前

    <?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);
    
    ?>