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

用于从登录页面向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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器