在Bootstrap中提交表单的问题

时间:2015-09-13 08:53:36

标签: php twitter-bootstrap

我有以下自助联系表格:

            <form role="form">
                <div class="form-group">
                    <div class="input-group">
                        <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                        <input type="text" class="form-control" placeholder="First Name" name="firstname" id="firstname" value="<?php echo $_POST['firstname']; ?>">
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group">
                        <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                        <input type="text" class="form-control" placeholder="Family Name" name="familyname" id="familyname" value="<?php echo $_POST['familyname']; ?>">
                    </div>
                </div>
                <div class="form-group">
                    <div class="input-group">
                        <span class="input-group-addon"><span class="glyphicon glyphicon-earphone"></span></span>
                        <input type="text" class="form-control" placeholder="Phone Number" name="phone" id="phone" value="<?php echo $_POST['phone']; ?>">
                    </div>
                </div>
                <div class="form-group">
                        <button type="submit" class="btn btn-primary" onclick="">Submit</button>
                        <input type="hidden" name ="mode" value="submit">
                </div>
            </form>

在提交时,我想插入联系表,如下所示:

if (($_POST['mode']) == 'submit'){
    var_dump("Testing if I entered this if statement"); die();

    $add_contact = mysqli_query($conn, "INSERT INTO `contact` (
    `cont_id`, //it is auto increment id 
    `cont_fname`, 
    `cont_family`, 
    `cont_phone`
    ) VALUES (
    '" . $_POST['firstname'] . "', 
    '" . $_POST['familyname'] . "', 
    '" . $_POST['phone'] . "'
    )") or die(mysqli_error($conn));

}

我遇到了两个问题。首先点击提交按钮后,程序没有进入if语句?第二,数据库插入有什么问题?我在不同的脚本中尝试了插入,但它没有用。如果你能提供帮助,我感激不尽。

这是我的连接文件工作正常,这意味着我连接到我的数据库:

$conn=mysqli_connect("localhost","contacts");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

1 个答案:

答案 0 :(得分:0)

使用method = "POST"标记中的<form>和隐藏类型。

<form role="form" method = "POST">
<!-- blah3 -->
<input type="hidden" name ="check">
<input type="submit" name ="submit" value="submit">
</form>

并在php中放置isset

if (isset($_POST['check'])){
    echo "Im inside the if-statement duh!";

    $add_contact = mysqli_query($conn, "INSERT INTO `contact` (
    `cont_id`,  
    `cont_fname`, 
    `cont_family`, 
    `cont_phone`
    ) VALUES (
    '" . $_POST['firstname'] . "', 
    '" . $_POST['familyname'] . "', 
    '" . $_POST['phone'] . "'
    )") or die(mysqli_error($conn));

}

修改

  

connection.php:

<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "yourdbnamehere");

if (!$conn) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($conn);
?>

来源:PHP: mysqli_connect