提交时未在数据库中插入数据

时间:2017-11-17 08:33:10

标签: php html database

我已经设置了一个表单来将数据插入数据库。它连接到它很好,可以显示没有问题的记录。但是,当我想插入数据时,我单击提交按钮,它将其从表单中删除但不将其插入数据库。我现在尝试使用2个不同的数据库重写它大约3次但是我无法弄清楚我在哪里。

<html>
<head>
</head>

<body>
<form action="input.php" meathod="post">
Username: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br>
<input type="submit" name="submit">
</form>


<?php 
if( isset($_POST['submit'])){

//connecting to a databse
$conn = mysqli_connect("localhost","root","");
mysqli_select_db("test",$conn);

if($conn){
        echo 'connected';
    }
    else {
        die('failed to connect');
    }

$sql="INSERT INTO users(username,password) VALUES ('$_POST[username]','$_POST[password]')"; 

mysql_query($sql,$conn);

mysqli_close($conn);
};  
?>
</body>
</html>

4 个答案:

答案 0 :(得分:1)

您的拼写错误"meathod=post"应为method="post"mysql_query($sql,$conn)应为mysqli_query($conn,$sql)mysqli_select_db("test",$conn)应为mysqli_select_db($conn,"test")

 <html>
    <head>
    </head>
    <body>
    <form action="input.php" method="post">
    Username: <input type="text" name="username"><br>
    Password: <input type="text" name="password"><br>
    <input type="submit" name="submit">
    </form>
    <?php 
    if( isset($_POST['submit'])){
    //connecting to a databse
    $conn = mysqli_connect("localhost","root","");
    mysqli_select_db($conn,"test");
    if($conn){
            echo 'connected';
        }
        else {
            die('failed to connect');
        }
    $sql="INSERT INTO users(username,password) VALUES ('" . $_POST['username'] . "','" . $_POST['password'] . "')"; 
    mysqli_query($conn,$sql);
    mysqli_close($conn);
    };  
    ?>
    </body>
    </html>

答案 1 :(得分:0)

我认为问题在这里

mysql_query($sql,$conn);

替换为

mysqli_query($conn,$sql);

答案 2 :(得分:0)

你写过“meathod = post”而不是“method = post” 我也改变了

undefined is not a constructor (evaluating 'sinon.spy(authorizationInterceptor, 'responseError').andCallThrough()'
)

$sql="INSERT INTO users(username,password) VALUES ('$_POST[username]','$_POST[password]')"; 

这种风格更好

$sql="INSERT INTO users(username,password) VALUES ('" . $_POST['username'] . "','" . $_POST['password'] . "')"; 

答案 3 :(得分:0)

我希望此代码可以帮助您

<html>
    <head>
    </head>

    <body>
    <form action="input.php" method="post">
    Username: <input type="text" name="username"><br>
    Password: <input type="text" name="password"><br>
    <input type="submit" name="submit">
    </form>


    <?php 
    if( isset($_POST['submit'])){

    //connecting to a databse
    $conn = mysqli_connect("localhost","root","");
    mysqli_select_db("test",$conn);

    if($conn){
            echo 'connected';
        }
        else {
            die('failed to connect');
        }

  $sql="INSERT INTO users(username,password) VALUES (".$_POST["username"].",".$_POST["password"].")"; 

    mysqli_query($conn,$sql);
    mysqli_close($conn);

    ?>
    </body>
    </html>