无法将数据输入sql数据库

m4pnthwp  于 2021-06-20  发布在  Mysql
关注(0)|答案(4)|浏览(315)

我使用这段代码向我已经存在的sql数据库添加一些数据,但是似乎不能这样做,它也没有给出任何错误。我想尽一切办法都试过了。这是一个表单,允许用户输入数据,然后当用户单击提交时,它会在url中给出一条成功消息,但我得到了成功消息,但我的数据库中没有数据。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Signup Form</title>
</head>
<body>
    <form action="signup.php" method="POST">
        <input type="text" name="firstname" placeholder="First Name">
        <br>
        <input type="text" name="lastname" placeholder="Last Name">
        <br>
        <input type="text" name="email" placeholder="E-mail">
        <br>
        <input type="text" name="uid" placeholder="User name">
        <br>
        <input type="password" name="pwd" placeholder="Password">
        <br>
        <button type="submit" name="submit">Sign up</button>
        &nbsp;
    </form>

<?php
    $sql = "SELECT * FROM users;" ;
    $result = mysqli_query($conn,$sql);          //connects the database to the query we just generated
    $resultcheck = mysqli_num_rows($result);        // it returns the number of rows in the query

    if($resultcheck > 0){
        //the if condition checks if there is any data inside $resultcheck
        //The mysqli_fetch_assoc() function fetches a result row as an associative array.
        while($row = mysqli_fetch_assoc($result)){
            echo $row['user_uid'].'<br>';
        }
    }
?>

</body>
</html>
<?php
    include_once 'dbh.php';

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $uid = $_POST['uid'];
    $pwd = $_POST['pwd'];

    $sql = "INSERT INTO users (`user_firstname`, `user_firstname`, `user_email`, `user_uid`, `user_pwd` ) VALUES (\'$firstname\',\'$lastname\',\'$email\',\'$uid\', \'$pwd\');";

    //require 'dbh.php';

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

    /* if($result=$mysqli->query($sql)){
    echo "<p>User successfully added to database</p>".'<br>';
    }
    else{
        echo "Error enterting user into database!".mysql_error().'<br>';
    } */

    header("Location: index.php?signup=success");
?>
<?php
    $dbServername = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "login_system";               // selecting the database

    $conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName );
    //$mysqli = new mysqli('localhost','root',"",$dbName );

    if(mysqli_connect_errno()){
        printf("connection failed %s\n",mysqli_connect_error());
        exit();
    }

    $mysqli->select_db("login_system");
?>
kyxcudwk

kyxcudwk1#

应该是这样的

$sql = "INSERT INTO users (firstname, lastname, email, uid, pwd ) VALUES ('$firstname','$lastname','$email','$uid', '$pwd')";

mysqli_query($conn,$sql);
0g0grzrc

0g0grzrc2#

试试这个:

<?php
include_once 'dbh.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title> Signup Form </title>
    </head>
    <body>
        <form action="signup.php" method="POST">
            <input type="text" name="firstname" placeholder="First Name">
            <br>
            <input type="text" name="lastname" placeholder="Last Name">
            <br>
            <input type="text" name="email" placeholder="E-mail">
            <br>
            <input type="text" name="uid" placeholder="User name">
            <br>
            <input type="password" name="pwd" placeholder="Password">
            <br>
            <button type="submit" name="submit">Sign up</button>
            &nbsp;
        </form>
        <?php
        $sql = "SELECT * FROM users; " ;
        $result = $mysqli->query($sql);             //connects the database to the query we just generated
        $resultcheck = $result->num_rows;       // it returns the number of rows in the query

        if($resultcheck > 0){
            while($row = $result->fetch_assoc()){
                echo $row['user_uid'].'<br>';
            }
        }
        ?>
    </body>
</html>

注册.php

<?php
include_once 'dbh.php';

$firstname = $mysqli->real_escape_string($_POST['firstname']);
$lastname = $mysqli->real_escape_string($_POST['lastname']);
$email = $mysqli->real_escape_string($_POST['email']);
$uid = $mysqli->real_escape_string($_POST['uid']);
$pwd = $mysqli->real_escape_string($_POST['pwd']);

$sql = "INSERT INTO users (`user_firstname`, `user_lastname`, `user_email`, `user_uid`, `user_pwd` ) VALUES ('$firstname','$lastname','$email','$uid', '$pwd');";

if($result=$mysqli->query($sql)){
    echo "<p>User successfully added to database</p>".'<br>';
}
else{
    echo "Error enterting user into database!".$mysqli->error.'<br>';
}

header("Location: index.php?signup=success");

胸径.php

<?php

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "login_system";               // selecting the database

$mysqli = new mysqli($dbServername,$dbUsername,$dbPassword,$dbName);

if($mysqli->connect_errno){
    printf("connection failed %s\n",$mysqli->connect_error);
    exit();
}

请阅读此参考资料http://php.net/manual/en/book.mysqli.php

6yjfywim

6yjfywim3#

请删除$conn和$sql中的单引号

mysqli_query($conn, $sql);

在insert php文件中。

abithluo

abithluo4#

$sql = "INSERT INTO `users` (`user_firstname`, `user_lastname`, `user_email`, `user_uid`, `user_pwd` ) VALUES ('".$firstname."', '".$lastname ."', '".$email."', '".$uid."', '".$pwd."');";
$result=mysqli_query('$conn','$sql');
if($result)
{
    echo "succsessfuly...";
}
else
{
    echo "Not succsessfuly...";
}

相关问题