404将表单数据提交到sql数据库时

时间:2018-01-15 06:01:37

标签: php html mysql

我一直在提交表单输入或尝试这样做我在这个PHP的末尾获得了404这个PHP它在php snippit的顶部我不确定这是否是问题的线索。我有与insert.php关联的操作我不确定发生了什么。给我的一切看起来都是正确的。但显然不是。我确保header('location: index.html')是正确的。正如另一篇文章所说。这是我的代码

HTML

<!Doctype html>
<html>
<head>
<title>Sign Up</title>
<link rel="stylesheet" href="css/live/app-d159020cbe.min.css">
<link rel="stylesheet" href="css/live/core-89ce772293.min.css">
</head>
<body>
    <div class="containsignuptext">
        <p>We believe the developer community is stronger togeather, We believe it takes the right community for us to reach the next stage in the future we believe in adding real tools to developers tool belts.</p>
    </div>
    <form action="insert.php" method="POST"  accept-charset="UTF-8" role="form" id="loginForm">
        <div class="form-group">
            <div class="form-group">
                <label for="login_email" class="sr-only">Full Name</label>
                <input class="form-control input-lg" id="fullname" placeholder="Full Name" required="required" name="fullname" type="text">
            </div>
            <div class="form-group">
                <label for="login_email" class="sr-only">Username</label>
                <input class="form-control input-lg" id="username" placeholder="Username" required="required" name="username" type="text">
            </div>
        </div>
        <div>
            <label for="login_email" class="sr-only">Email</label>
            <input class="form-control input-lg" id="email" placeholder="Email" required="required" name="email" type="text">
        </div>
        <div class="form-group">
            <label for="login_password" class="sr-only">Password</label>
                <input class="form-control input-lg" id="password" placeholder="Password" required="required" name="password" type="password">
            <div>
                <label for="login_email" class="sr-only">Codelangs</label>
                <input class="form-control input-lg" id="codelang" placeholder="Code Language You Like the Most" required="required" name="codelangs" type="text">
            </div>
            <button method="POST" type="submit" class="btn btn-lg btn-primary btn-block ladda-button" data-style="zoom-in" type="submit" name="insert">
                <span class="ladda-label">Get Hacking</span>
            </button>
    </form>
</body>
</html>

PHP

<?php echo $_SERVER['PHP_SELF']; ?> 

我在url idk中得到它,如果它有助于其余的是inser.php代码。

<?php
$servername = "localhost";
$username = "Placeholder";
$pass = "FakeForShow";
$dbname = "sign up new";


$fullname=$_POST['fullname'];
$username=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['password'];
$codelangs=$_POST['codelangs'];

// Create connection
$conn = new mysqli($servername, $username, $pass, $dbname);
// Check connection
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO newusers (fullname, username, password, email, codelang)
VALUES ( '$fullname', '$username', '$email',  '$password', '$codelangs');";

if ($conn->query($sql) === TRUE) {
    header("location: index.html");
} 
else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

?>

2 个答案:

答案 0 :(得分:0)

您的数据库连接应与此类似。

$s = 'localhost';
$u = 'root';
$p = 'yourUsername';
$d = 'yourPassword';
$mysqli = new mysqli($s, $u, $p, $d);

检查是否先点击了屁股。

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

执行这样的错误检查,你需要添加到这里这是非常基本的。

$fullname=$_POST['fullname'];
$username=$_POST['username'];
$email=$_POST['email'];
$password=$_POST['password'];
$codelangs=$_POST['codelangs'];   
$alert = '';    
$errorFullname = '';
if($_POST['fullname'] == '' || $_POST['username'] || $_POST['email'] || $_POST['password']){
if($_POST['fullname'] == ''){$errorFullname "Cannot be empty.";}//and so on...
$alert = "All fields are required.";} else {

如果没有插入错误。在插入密码之前加密密码。

$hashedWord = password_hash($password, PASSWORD_BCRYPT, array("cost" => 17));
$insertdata = $mysqli->prepare("INSERT INTO  newusers (fullname, username, password, email, codelang) VALUES('"$fullname"', '"$username"',  '"$hashedWord"', '"$email"', '"$codelangs"') ");
$insertdata->execute();
$insertdata->close();      
header("location: index.html");
}
}
include 'YourHtmlPage';

您可以像这样调用错误消息。

<div class="form-group">
<label for="login_email" class="sr-only">Full Name</label>
<input class="form-control input-lg" id="fullname" placeholder="Full Name" required="required" name="fullname" type="text">
</div>
<div><?php echo $errorFullname; ?></div>

您可以使用类似的方法检查哈希密码。运行选择并将其与键入的内容进行匹配。

password_verify($passwordTheyTyped, $YourHashedPassFromTheDatabase);

希望它有所帮助。

答案 1 :(得分:-1)

这可能会对你有帮助。

HTML代码:index.php

<!Doctype html>
<html>
    <head>
        <title>Sign Up</title>
        <link rel="stylesheet" href="css/live/app-d159020cbe.min.css">
        <link rel="stylesheet" href="css/live/core-89ce772293.min.css">
    </head>
    <body>
        <div class="containsignuptext">
            <p>We believe the developer community is stronger togeather, We believe it takes the right community for us to reach the next stage in the future we believe in adding real tools to developers tool belts.</p>

        </div>
        <form action="insert.php" method="POST"  accept-charset="UTF-8" role="form" id="loginForm">
            <div class="form-group">
                <label for="login_email" class="sr-only">Username</label>
                <input class="form-control input-lg" id="username" placeholder="Username" required="required" name="username" type="text">
            </div>
            <!-- Other form elements-->
            <button method="POST" type="submit" class="btn btn-lg btn-primary btn-block ladda-button" data-style="zoom-in" type="submit" name="insert">
                <span class="ladda-label">Get Hacking</span>
            </button>
        </form>

    </body>
</html>

PHP代码:insert.php

<?php
$servername = "localhost";
$db_username = "DB_USER_NAME";
$db_pass = "DB_USER_PASSWORD";
$dbname = "DB_NAME";

$username = $_POST['username'];
// Other POST elements

$conn = new mysqli($servername, $db_username, $db_pass, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO newusers (username) VALUES ('$username');";

if ($conn->query($sql) === TRUE) {
    header("location: index.php");
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
?>

您可以根据自己的要求添加更多表单元素,Javascript Validations等。