将数据发送到数据库的问题

时间:2015-07-10 22:59:44

标签: php html

您好,我发送数据时发送数据时遇到了一些问题,确实说数据库中已找到连接购买数据。我是php的新手...请帮助。

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Login File </title>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/css/materialize.min.css">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/js/materialize.min.js"></script>

</head>

<body>
    <script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>


    <div class="container">

        <div class="row">

            <form action="login_process.php" method="post">

                <legend><h1 style="color:red;">Form title </h1></legend>

                <div class="input-field col s6">

                    <div class="form-group">

                        <span class="help-block"></span>

                        <input type="text" name="username" class="form-control" placeholder="Username">

                    </div>

                </div>

                <div class="input-field col s6">

                    <div class="form-group">

                        <span class="help-block"></span>

                        <input type="password" name="password" class="form-control" placeholder="Password">

                    </div>

                </div>

                <button type="submit" class="btn">Submit</button>

            </form>

        </div>

    </div>

</body>

</html>

连接文件:

<?php

    $connection = mysqli_connect('localhost', 'root', '', 'demo');

    if(!$connection) {

    die("Database connection failed". mysqli_error()); } else {echo "Connected";}

?>

和流程文件:

<?php

require_once 'connection.php';

if($connection) {

    echo "<h1>Connected</h1>";

}

if (isset($_POST['submit'])) {

    $username = $_POST['username'];

    $password = $_POST['password'];

    $query = "INSERT INTO user_tbl(username,password) VALUES ('$username','$password')";

    $result= mysqli_query($connection,$query);

    if (!$result) {

        die("Query failed");

    }

}
$result = mysql_query("SELECT * FROM user_tbl");
echo $result;

?>

1 个答案:

答案 0 :(得分:1)

您的问题与您的表单有关。

你的php脚本:

if (isset($_POST['submit']))

正在寻找一个应该是name="submit"

的命名变量

您正在检查名为submit的提交按钮,该按钮不存在。

更改此

<button type="submit" class="btn">Submit</button>

<input type="submit" name="submit" class="btn" value="submit" />