duanhong8839 2017-11-13 09:32
浏览 54
已采纳

isset($ _ POST ['submit']在提交表单后未设置

I'm new to php and I'm trying to register a user to a database using a form.

This is the code for the form (using the bootstrap framework):

<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>

Here is the file (mysqli_connect.php) for connecting to the database. This is working:

<?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');

The file connectivity-reserve.php is what I'm using to add the user into the database. Here is the code:

<?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";
    }
}

?>

I'm able to connect to the database since the code echo "yay" is displayed on the browser. The issue is with the if(isset($_POST['submit'])) condition. I thought it should work since my button's type and name are 'submit' on the form:

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

Help will be greatly appreciated.

Edit: Thanks guys! It was the , after psword. Also I know the code isn't secure. I'm just starting out with php so that isn't really a priority for me right now.

  • 写回答

4条回答 默认 最新

  • doulangpeng3933 2017-11-13 09:39
    关注
    $q = "INSERT INTO users (user_id, cname, psword ) VALUES (' ', '$Customername', '$pwd')";     
    

    Remove the comma from psword. Tested your code in my local db and is working just fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!