if(isset($ _ POST ['submit']))编码表单动作不起作用

时间:2017-09-26 07:32:08

标签: php

嗨我是一个初学者在php和mysql这里如果有问题,它是我的isset帖子提交不工作或我的编码有一些错误,谁能告诉我在编码中我错过了什么?提前谢谢你:)

这是我的php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tempahperalatan";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

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

    $pemohon = $_POST['namaPemohon'];
    $trkhMula = $_POST['tmula'];
    $trkhAkhir = $_POST['takhir'];
    $n_program = $_POST['namaProgram'];
    $lokasi = $_POST['lokasi'];
    $n_anjuran = $_POST['namaAnjuran'];
    $catatan = $_POST['catatan'];

    $sql = "INSERT INTO daftartempah (pemohon, trkhMula, trkhAkhir, n_program, lokasi, n_anjuran, catatan) VALUES ('$namaPemohon', '$tmula', '$takhir', '$namaprogram', '$lokasi', '$namaAnjuran', '$catatan')";

    if (mysqli_query($conn, $sql)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}


mysqli_close($conn);
?> 

这是我的表格:

<form action="page1.php" method="POST">

    <div class="form-group row text-left">
        <label for="example-text-input" class="col-2 col-form-label">Nama Pemohon: </label>
        <div class="col-10">
            <input class="form-control" type="text" name="namaPemohon" id="namaPemohon">
        </div>
    </div>

    <div class="form-group row text-left">
        <label for="example-date-input" class="col-2 col-form-label">Tarikh Mula: </label>
        <div class="col-10">
            <input class="form-control" type="date" value="0000-00-00" name="tmula" id="tmula">
        </div>
    </div>

    <div class="form-group row text-left">
        <label for="example-date-input" class="col-2 col-form-label">Tarikh Akhir: </label>
        <div class="col-10">
            <input class="form-control" type="date" value="0000-00-00" name="takhir" id="takhir">
        </div>
    </div>  

    <div class="form-group row text-left">
        <label for="example-text-input" class="col-2 col-form-label">Nama Program: </label>
        <div class="col-10">
            <input class="form-control" type="text" name="namaProgram" id="namaProgram">
        </div>
    </div>

    <div class="form-group row text-left">
        <label for="example-text-input" class="col-2 col-form-label">Lokasi: </label>
        <div class="col-10">
            <input class="form-control" type="text" name="lokasi" id="lokasi">
        </div>
    </div>              

    <div class="form-group row text-left">
        <label for="example-text-input" class="col-2 col-form-label">Dept/Kelab/Anjuran: </label>
        <div class="col-10">
            <select class="form-control" name="namaAnjuran" id="namaAnjuran">
                <option name="1" value="1">BK - B. Kewangan</option>
                <option name="2" value="1">BP - B. Pentadbiran</option>
                <option name="3" value="1">BPK - B. Perancangan Hal Ehwal Korporat</option>
                <option name="4" value="1">BPPP - B. Pemb. Penilaian Projek</option>
                <option name="5" value="1">BPPS - B. Pemb. Projek Sosial</option>
                <option name="6" value="1">UAD - Unit Audit Dalaman</option>
                <option name="7" value="1">PWT - Pej. wilayah Temerloh</option>
                <option name="8" value="1">PWB - Pej. Wilayah Barat</option>
                <option name="9" value="1">KOOP - Koperasi LKPP</option>
                <option name="10" value="1">KSKLKPP - KSK LKPP Negeri Pahang</option>           
                <option name="11" value="1">KSKCIP - KSK LKPP Cawangan Ibu Pejabat</option>
                <option name="12" value="1">IBUPEJ - IBU PEJABAT</option>   
                <option name="13" value="1">KESATUAN - KESATUAN SEKERJA</option>                                    
            </select>
        </div>
    </div>

    <div class="form-group row text-left">
        <label for="exampleTextarea" class="col-2 col-form-label">Catatan: </label>
        <div class="col-10">
            <textarea class="form-control" id="exampleTextarea" rows="3" name="catatan" id="catatan"></textarea>
        </div>
    </div>

    <center><button type="submit" class="btn btn-info">Submit</button></center>                     

</form>

当我点击提交时,没有错误,但数据没有插入数据库。

6 个答案:

答案 0 :(得分:1)

试试这样:

<button type="submit" class="btn btn-info" name="submit" value="submit" >Submit</button>

答案 1 :(得分:1)

if (isset($_POST['submit'])) {这不起作用,原因是您没有提供提交按钮的名称<button type="submit" class="btn btn-info">Submit</button>

添加提交按钮的名称,如下所示。

<button type="submit" name="submit" class="btn btn-info">Submit</button>

OR

if (isset($_POST)) {

答案 2 :(得分:1)

如果要将数据从表单传递到php脚本,则必须记住为该数据分配了name属性,以便在php脚本中查找。

<button type="submit" class="btn btn-info">Submit</button>

在按钮中,您没有分配名称属性。所以在php脚本之后的post方法isset($ _ POST ['submit'])没有找到任何提交属性。所以它返回false。

<button type="submit" class="btn btn-info" name="submit" value="submit" >Submit</button>

所以你必须使用这一行作为按钮。

答案 3 :(得分:1)

始终记住,这是将表格链接到php脚本的name属性。因此应该是:

<button type="submit" name="submit" class="btn btn-info">Submit</button>

答案 4 :(得分:0)

只需检查$_POST是否为空

if(!empty($_POST)) // form has been submitted

您的案例

中没有名称为submit的表单输入

答案 5 :(得分:0)

您可以按下按钮的名称

<center><button type="submit" name="submit" class="btn btn-info">Submit</button></center>

这将起作用