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

时间:2019-03-11 08:13:15

标签: php jquery html mysql

此刻我真的需要帮助。 我正在尝试制作一个记录现金/银行付款的应用程序。

我有一个称为“现金簿”的表。它存储有关现金/银行付款的数据。它有8列:

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)

我正在制作一个程序,当满足某些条件时,该程序会将数据插入“现金簿”表的特定列中。我定义了4 记录现金/银行付款的条件。

下面是用于收集数据的html表单:

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

下面是将数据添加到“现金簿”表的脚本:

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

我的问题是,插入数据的唯一时间是$ keyb =='2'和$ keyc =='2'(这是条件之一)。 但是,对于其他三个条件,即使在满足这些条件的情况下也不会插入任何内容。

请帮助我。

2 个答案:

答案 0 :(得分:0)

这并不是真正的解决方案,而是更多地找出出问题所在的方式:

确保获得正确的值

将不同的keyb(pmode)和keyc(desig)值发送到PHP脚本,然后在脚本的最上方执行var_dump($_REQUEST);exit;以查看您的身份得到。您是否正在获得期望的价值?

确保SQL查询有效

对于每个SQL查询(mysql_query()调用),请确保捕获任何错误。不仅仅是像您在做的那样调用它:

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

添加if()来捕获所有错误:

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

简化

最后,这是一个风格上的注释,我可能会重新做一下您的结构,以使elseif()部分不多。也许可以这样做,以便您根据值格式化查询 string ,然后在代码中触发SQL查询一次

答案 1 :(得分:-1)

首先检查您的查询:

model boolCheck_OK1
  Real a = sin(time);
  Real b = cos(time);
  Real c;

function inRegionCheck
  input Real a;
  input Real b;
  output Real c;
algorithm 
  c := if a>b then 1 else 0;
end inRegionCheck;
protected
  Real isInReg = inRegionCheck(a, b);

equation 
  c = if inRegionCheck(a, b)>Modelica.Constants.eps then 1.3*a^b else 0.7*b^a;
end boolCheck_OK1;

使用$result = mysqli_query($mysqli, "INSERT..."); 捕获错误:

也使用此代码来接收错误

if()