isset($ _ POST ['submit']在提交表单

时间:2017-11-13 09:32:56

标签: php html mysql

我是php的新手,我正在尝试使用表单将用户注册到数据库。

这是表单的代码(使用bootstrap框架):

<form class="form-horizontal" role="form" method="POST" action="connectivity-reserve.php">
                <div class="form-group">
                    <label class="col-sm-2 control-label">Customer's name</label>
                    <div class="col-sm-6">
                        <div class="input-group">
                        <input type="text" class="form-control" id="Customername" name="Customername" placeholder="Customer name">
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">Password</label>
                    <div class="col-sm-6">
                        <div class="input-group">
                        <input type="password" class="form-control" id="pwd" name="pwd" placeholder="Password">
                        </div>
                    </div>
                </div>

                </div>
                 <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" name ="submit" class="btn btn-primary">Reserve</button>
                    <button type="button" class="btn btn-default btn-sm"
                   data-dismiss="modal">Cancel</button>
                    </div>
                </div>
                <div class="form-group">
                <div class="alert alert-warning alert-dismissible" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                    <strong>Warning:</strong>: Please <a href="tel:+85212345678" class="alert-link">
                    call</a> us to reserve for more than six guests.
                </div>
                </div>
            </form>

这是用于连接数据库的文件( mysqli_connect.php )。这是有效的:

<?php
// This provides the information for accessing the database. 
// It also creates a connection to MySQL, 
// It selects the database, and sets the encoding.
// Set the database access information as constants:
DEFINE ('DB_USER', 'xxxx');
DEFINE ('DB_PASSWORD', 'xxxx');
DEFINE ('DB_HOST', 'xxxx');
DEFINE ('DB_NAME', 'xxxx');
// Make the connection:
$dbcon = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
// Set the encoding...
mysqli_set_charset($dbcon, 'utf8');

文件 connectivity-reserve.php 是我用来将用户添加到数据库中的方法。 这是代码:

<?php
require ('mysqli_connect.php');

echo "yay";

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

$Customername = $_POST['Customername'];
$pwd  = $_POST['pwd'];
$q = "INSERT INTO users (user_id, cname, psword, ) VALUES (' ', '$Customername', '$pwd')";        

 $result = @mysqli_query ($dbcon, $q);

 if($result) 
    { 
        echo "YOUR REGISTRATION IS COMPLETED..."; 
    }
    else
    {
        echo "nah";
    }
}

?>

我能够连接到数据库,因为代码echo "yay"显示在浏览器上。问题在于if(isset($_POST['submit']))条件。我认为它应该有效,因为我的按钮的类型和名称在表单上是“提交”:

<button type="submit" name ="submit" class="btn btn-primary">Reserve</button>

非常感谢帮助。

编辑:谢谢你们!这是在psword之后。我也知道代码不安全。我刚刚开始使用php,所以现在不是我的优先考虑。

4 个答案:

答案 0 :(得分:1)

替换

<button type="submit" name ="submit" class="btn btn-primary">Reserve</button>

通过

<input type="submit" name ="submit" class="btn btn-primary" value="Reserve">

查询错误

$q = "INSERT INTO users (user_id, cname, psword, ) VALUES (' ', '$Customername', '$pwd')";
psword

之后的

逗号

答案 1 :(得分:0)

$q = "INSERT INTO users (user_id, cname, psword ) VALUES (' ', '$Customername', '$pwd')";     

psword删除逗号。在我的本地数据库中测试了你的代码并且工作得很好。

答案 2 :(得分:0)

您的查询中存在错误:

声明如下:

$q = "INSERT INTO users (user_id, cname, psword, ) VALUES (' ', '$Customername', '$pwd')";    

应该是:

$q = "INSERT INTO users (user_id, cname, psword) VALUES (' ', '$Customername', '$pwd')"; 

还有一个额外的逗号会使查询失败并导致错误。

其次,您应该检查if(isset($_POST['submit']))Customername

,而不是检查pwd
if(isset($_POST['Customername']) && isset($_POST['pwd'])) {

答案 3 :(得分:0)

INSERT INTO users (cname, psword, ) VALUES ('{$Customername}', '{$pwd}')

您应该删除user_id,因为它已经自动