表格未写入数据库

时间:2018-08-23 15:15:48

标签: php html sql

所以我是php的新手,我在reg.php文件中有此表格

OpcoesProdutoDialogo opcProduto = OpcoesProdutoDialogo.criar(itemPedido);
        opcProduto.show(getFragmentManager(), "opc_produto_editar");
        opcProduto.setOnDismissListener(d->{
            adapterItens.notifyItemChanged(posicao);
        });

我有signup.php,其中包含注册代码

<form method="post" action="scripts/signup.php" class="ui form"">
                <div class="field">
                    <label>UserName</label>
                    <input type="text" name="username" placeholder="Choose a unique UserName">
                </div>
                <div class="field">
                    <label>Name</label>
                    <input type="text" name="name" placeholder="Name" required>
                </div>
                <div class="field">
                    <label>Address</label>
                    <input type="text" name="address" placeholder="Address" required>
                </div>
                <div class="field">
                    <label>E-mail</label>
                    <input type="email" name="email" placeholder="Your E-Mail" required>
                </div>
                <div class="field">
                    <label>Password</label>
                    <input type="password" name="password" placeholder="Your Password" required>
                </div>
                <div class="field">
                    <label>Confirm Password</label>
                    <input type="password" name="password2" placeholder="Confirm Password" required>
                </div>


                <button type="submit" name="submit" class="ui  big inverted red button" value="submit">Register</button>
                <br/>
                <br/>
            </form>

dbh.php仅包含变量,并且只为db设置,主行是

    <?php


if (isset($_POST['submit'])) {
    include_once 'dbh.php';

    $username = mysqli_escape_string($conn, $_POST['username']);
    $name = mysqli_escape_string($conn, $_POST['name']);
    $address = mysqli_escape_string($conn, $_POST['address']);
    $email = mysqli_escape_string($conn, $_POST['email']);
    $password = mysqli_escape_string($conn, $_POST['password']);



        // cccheck if username is already in db
        $query = "SELECT * FROM users WHERE username='$username'";
        $res = mysqli_query($conn, $query);
        $rescheck = mysqli_num_rows($res);

        //if the query returns a row or multiple
        if ($rescheck > 0) {
            header("Location: ../reg.php?reg=usernamesame");
            exit();

        } else {

            //password hashing
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);


            //write user too db
            $sql_insert = "INSERT INTO users(username, name1, address, email, pass)VALUES ('$username','$name','$address','$email','$hashed_password');";

            $results = mysqli_query($conn, $sql_insert);

            echo $results;
            header("Location: ../reg.php?reg=succcess");
            exit();

        }




} else {
    header("Location: ../reg.php?reg=Error");
    exit();
}

问题是,当我提交表单时,我收到成功消息,但是在进一步检查时,用户并未添加到数据库中。

任何帮助将不胜感激

抱歉,这是我的全部错,我没有将通行证设置为接收如此大的字符串。谢谢大家的帮助。

@LahiruTM是最有用的答案,它总是最小的东西

4 个答案:

答案 0 :(得分:1)

在signup.php文件底部的

注释的下面两行,这是对结果和标头重定向的回显。然后回显$ sql_insert以查看已执行的sql查询。提交表单后,获取该查询并在phpmyadmin sql query部分中运行它。如果查询中有任何错误,您将轻松找到它。同样,使用小度量标准来解决您的问题。

        //write user too db
        $sql_insert = "INSERT INTO users(username, name1, address, email, pass)VALUES ('$username','$name','$address','$email','$hashed_password');";

        $results = mysqli_query($conn, $sql_insert);
        echo $sql_insert;
        //echo $results;
        //header("Location: ../reg.php?reg=succcess");
        exit();

答案 1 :(得分:0)

嗨,您可以像这样修改脚本php:

 <?php


if (isset($_POST['submit'])) {
    include_once 'dbh.php';

    $username = mysqli_escape_string($conn, $_POST['username']);
    $name = mysqli_escape_string($conn, $_POST['name']);
    $address = mysqli_escape_string($conn, $_POST['address']);
    $email = mysqli_escape_string($conn, $_POST['email']);
    $password = mysqli_escape_string($conn, $_POST['password']);



        // cccheck if username is already in db
        $query = "SELECT * FROM users WHERE username='$username'";
        $res = mysqli_query($conn, $query);
        $rescheck = mysqli_num_rows($res);

        //if the query returns a row or multiple
        if ($rescheck > 0) {
            header("Location: ../reg.php?reg=usernamesame");
            exit();

        } else {

            //password hashing
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);


            //write user too db
            $sql_insert = "INSERT INTO users (username, name1, address, email, pass) VALUES ('$username','$name','$address','$email','$hashed_password');";

            $results = mysqli_query($conn, $sql_insert);

            echo $results;
            header("Location: ../reg.php?reg=succcess");
            exit();
        }

} else {
    header("Location: ../reg.php?reg=Error");
    exit();
}

答案 2 :(得分:0)

在您的SQL插入语句中,您要用分号关闭VALUES。

将您的声明更改为此:

 $sql_insert = "INSERT INTO users(username, name1, address, email, pass)VALUES ('$username','$name','$address','$email','$hashed_password') ";

有关进一步的通知,请出于安全目的使用PDO。

答案 3 :(得分:0)

以此方式重写代码。如果有任何错误,它将显示在屏幕上。 而且,当您重定向到另一个页面时,您无需回显结果。

<?php

if (isset($_POST['submit'])) {
    include_once 'dbh.php';

    $username = mysqli_escape_string($conn, $_POST['username']);
    $name = mysqli_escape_string($conn, $_POST['name']);
    $address = mysqli_escape_string($conn, $_POST['address']);
    $email = mysqli_escape_string($conn, $_POST['email']);
    $password = mysqli_escape_string($conn, $_POST['password']);



        // cccheck if username is already in db
        $query = "SELECT * FROM users WHERE username='$username'";
        $res = mysqli_query($conn, $query);
        $rescheck = mysqli_num_rows($res);

        //if the query returns a row or multiple
        if ($rescheck > 0) {
            header("Location: ../reg.php?reg=usernamesame");
            exit();

        } else {

            //password hashing
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);


            //write user too db
            $sql_insert = "INSERT INTO users(username, name1, address, email, pass)VALUES ('$username','$name','$address','$email','$hashed_password')";

            $results = mysqli_query($conn, $sql_insert) or die(mysqli_error());

            echo $results;
            header("Location: ../reg.php?reg=succcess");
            exit();

        }




} else {
    header("Location: ../reg.php?reg=Error");
    exit();
}