dongsang6899
2019-07-06 05:21
浏览 117
已采纳

用于从登录页面向MySQL数据库提交数据的代码

I have created a simple HTML Login page. I have created a PHP file to transfer data to MySQL database(Using MySQL workbench 8.0) However, when I click submit, a page just open with some code from my PHP file and no data is transferred to my database. How can I transfer data from my Login page to my MYSQL database?

I have saved my PHP file in the C://

Code for Login Page:

<html>
   <head>
         <title>Login</title>
  </head>
   <body>
       <form action="/Test1.php">
        Username:<br>
        <input type="text" value="username"><br>
        Password:<br>
        <input type="text" value="password"><br>
        <input type="submit" value="submit"><br>
        </form>
    </body>
</html>

CODE for PHP:

<html>
<? php
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
    $host = "localhost:3306";
    $dbusername = "root";
    $dbpassword = "";
    $dbname = "login";

    // Create Connection
    $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
    if (mysql_connect_error()){
        die ( 'Connect Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
    }
    else {
        $sql = "INSERT INTO login.user (username, password)
        values ('$username', '$password')";
        if ($conn->query($sql)){
            echo "New record is inserted successfully";
        }
        else {
            echo "Error:".$sql."<br>".$conn->error;
        }
        $conn->close();
}
}
else { 
echo "Password should not be empty";
die ();
}

else {
    echo "Username should not be empty";
    die();
}

?>
</html>

I expect data from HTML file to be inputed in MYSQL Database but instead a new page open up with this:

query($sql)){ echo "New record is inserted successfully"; } else { echo "Error:".$sql."
".$conn->error; } $conn->close(); } } else { echo "Password should not be empty"; die (); } else { echo "Username should not be empty"; die(); } ?>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dounaoji2054 2019-07-06 08:05
    已采纳

    Try this:

    <html>
    <?php
    $username = filter_input(INPUT_POST, 'username');
    $password = filter_input(INPUT_POST, 'password');
    
    if (empty($username)) {
        echo 'Username should not be empty';
        exit;
    }
    if (empty($password)) {
        echo 'Password should not be empty';
        exit;
    }
    
    $host = 'localhost:3306';
    $dbusername = '';
    $dbpassword = '';
    $dbname = '';
    
    // Create Connection
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    try {
        $connection = new mysqli($host, $dbusername, $dbpassword, $dbname);
    } catch (Exception $exception) {
        exit('Error connecting to database');
    }
    $connection->set_charset('utf8mb4');
    
    try {
        $statement = $connection->prepare("INSERT INTO login.user (username, password) values (?, ?)");
        $statement->bind_param('ss', $username, $password);
        $statement->execute();
        $statement->close();
    
        echo 'New record is inserted successfully';
    } catch (Exception $exception) {
        echo 'Error: ' . $exception->getMessage();
    }
    
    $connection->close();
    
    ?>
    </html>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题