douyingbei1458 2019-03-11 08:13
浏览 95

PHP脚本没有将HTML表单数据提交到数据库

I really need help at this moment. I am trying to make an application that records cash/bank payments.

I have a table called 'cashbook'. It stores data about cash/bank payments. It has 8 columns:

id (stores transaction id)
enter code here
tdate (stores transaction date
chequeno (stores cheque number, if the payment is made by cheque)
originator (stores the name of the person to whom payment is made)
cr_amount (stores the amount of cash/bank payment)
tclass (stores the information about the class of transaction)
pmode (stores the mode of transaction-cash or bank)
payer (stores the name of the person making the payment)

I am making a program that will insert data into specific columns of the 'cashbook' table when certain conditions are met. I have defined 4 conditions on which to record cash/bank payments.

Below is the html form that is used to collect data:

$(document).ready(function(){
    $('#designation').on('change', function(){
        if ( this.value == '1')
        {
            $("#supp").show();
            $("#paye").hide();
        }
        else if ( this.value == '2')
        {
            $("#paye").show();
            $("#supp").hide();
        }
        else
        {
            $("#supp").hide();
        }
    });
});
    

$(document).ready(function(){
    $('#pmoder').on('change', function(){
        if ( this.value == '2')
        {
            $("#chequeno").show();
        }
        else
        {
            $("#chequeno").hide();
        }
    });
});
<script  src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form role="form" method="post" action="registercash.php">
    <div class="form-group">
        <label>Date</label>
        <input class="form-control" name="tdate" type="date">
        </div>
        <div class="form-group">
            <label>Mode of Payment</label>
            <select class="form-control" id="pmoder" name="pmode">
                <option>0</option>
                <option>1</option>
                <option>2</option>
            </select>
        </div>
        <div class="form-group" id="chequeno" style="display: none;">
            <label>Cheque no</label>
            <input class="form-control" name="chequeno" >
            </textarea>
        </div>
        <div class="form-group" >
            <label>Amount</label>
            <input class="form-control" name="tamount" >
            </textarea>
        </div>
        <div class="form-group">
            <label>Designation / Supplier</label>
            <select class="form-control" id='designation' name="desig">
                <option>0</option>
                <option>1</option>
                <option>2</option>
            </select>
        </div>
        <div class="form-group" id="paye" style="display: none;">
            <label>Name of Payee</label>
            <input class="form-control" name="payeename" >
            </textarea>
        </div>
        <div class="form-group" id='supp' style="display: none;">
            <label>Select Supplier</label>
            <select class="form-control" name="suppliername">
                <option>Ministry of Trade and Industry</option>
                <option>Okaikwei North Municipal Assembly</option>
            </select>
        </div>
        <div class="form-group">
            <label>Class</label>
            <select class="form-control" name="tclass">
                <option>Personnel Emoluments</option>
                <option>Materials & Office consumables</option>
                <option>Serminars & Conferences</option>
            </select>
        </div>
        <button type="submit" name="Submit" class="btn btn-default">Record</button>
    </form>

Below is the script that adds data to the 'cashbook' table:

<html>
    <head>
        <title>Add Data</title>
    </head>

    <body>
    <?php
    //including the database connection file
    include_once("config.php");

    $keyb = "";
    $keya = "";
    $chequeno = "";
    $keyc = "";
    $payeename = "";
    $suppliername = "";
    $tdate = "";
    $tclass = "";
    $tamount = "";
    if(isset($_POST['Submit'])) {   
        $keyb = mysqli_real_escape_string($mysqli, $_POST['pmode']);
        $chequeno = mysqli_real_escape_string($mysqli, $_POST['chequeno']);
        $keyc = mysqli_real_escape_string($mysqli, $_POST['desig']);
        $payeename = mysqli_real_escape_string($mysqli, $_POST['payeename']);
        $suppliername = mysqli_real_escape_string($mysqli, $_POST['suppliername']);
        $tdate = mysqli_real_escape_string($mysqli, $_POST['tdate']);
        $tclass = mysqli_real_escape_string($mysqli, $_POST['tclass']);
        $tamount = mysqli_real_escape_string($mysqli, $_POST['tamount']);
    }

    if(empty($keyb) || empty($keyc)|| empty($chequeno)|| empty($payeename)|| empty($suppliername)|| empty($tdate)|| empty($tclass)|| empty($tamount)) {
        if(empty($keyb)) {
            echo "<font color='red'>Payment mode is empty.</font<br/>";
        }

        if(empty($keyc)) {
            echo "<font color='red'>Designation is empty.</font><br/>";
        }
        if(empty($tdate)) {
            echo "<font color='red'>Transaction date is empty.</font><br/>";
        }
        if(empty($tclass)) {
            echo "<font color='red'>Transaction class is empty.</font><br/>";
        }
        if(empty($tamount)) {
            echo "<font color='red'>Transaction amount is empty.</font><br/>";
        }
    } elseif ($keyb=="1" && $keyc =="2"){
        $result = mysqli_query($mysqli, "INSERT INTO cashbook(tdate, originator,     cr_amount, tclass, pmode, payer) VALUES   ('$tdate', '$payeename','$tamount', '$tclass','$keyb','$payeename')");
            echo "<font color='green'>Data added successfully.";
        echo "<br/><a href='index.php'>View Result</a>";

    }elseif ($keyb=="2" && $keyc =="1"){
        $result = mysqli_query($mysqli, "INSERT INTO cashbook(tdate, chequeno, originator, cr_amount, tclass, pmode, payer) VALUES('$tdate', '$chequeno','$suppliername','$tamount','$tclass','$keyb','$payeename')");
        echo "<font color='green'>Data added successfully.";
        echo "<br/><a href='index.php'>View Result</a>";

    }elseif ($keyb=="2" && $keyc =="2") {
        $result = mysqli_query($mysqli, "INSERT INTO cashbook(tdate, chequeno, originator, cr_amount, tclass, pmode, payer) VALUES('$tdate','$chequeno', '$payeename','$tamount', '$tclass','$keyb','$payeename')");
        echo "<font color='green'>Data added successfully.";
        echo "<br/><a href='index.php'>View Result</a>";
            
    }elseif ($keyb=="1" && $keyc =="1") {
        $result = mysqli_query($mysqli, "INSERT INTO cashbook(tdate, originator, cr_amount, tclass, pmode, payer) VALUES('$tdate','$suppliername','$tamount','$tclass','$keyb','$payeename')");
        echo "<font color='green'>Data added successfully.";
        echo "<br/><a href='index.php'>View Result</a>";
    }else {
        echo "error,,";
    }
    ?>
    </body>

</html>

My problem is that, the only time data gets inserted is when $keyb=='2' and $keyc=='2'(this is one of the conditions). However, for the other 3 conditions, nothing gets inserted even in instances when those conditions are met.

Kindly help me out.

</div>
  • 写回答

2条回答 默认 最新

  • dssqq64884 2019-03-11 08:31
    关注

    This isn't really a solution, but more a way of finding out exactly what is going wrong:

    Make sure you're getting the right values

    Send different keyb (pmode) and keyc (desig) values to the PHP script, and then do a var_dump($_REQUEST);exit; at the very top of the script to see what you're getting. Are you getting the values you're expecting?

    Make sure the SQL query is valid

    For each SQL query (mysql_query() call), make sure you catch any errors. Instead of just calling it like you're doing:

    elseif ($keyb=="1" && $keyc =="2"){
        $result = mysqli_query($mysqli, "INSERT INTO ...");
        echo "<font color='green'>Data added successfully.";
        echo "<br/><a href='index.php'>View Result</a>";
    }
    

    Add an if() to catch any errors:

    elseif ($keyb=="1" && $keyc =="2"){
        if(!$result = mysql_query($mysql, "INSERT..."){
            die('Invalid query: ' . mysql_error());
        }
        echo "<font color='green'>Data added successfully.";
        echo "<br/><a href='index.php'>View Result</a>";
    }
    

    Simplify

    Finally, and this is more a stylistic note, I'd probably re-do your structure to not have so many elseif() sections. Maybe have it so that you format the query string based on the values and then just fire off the SQL query once in your code.

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)