为什么我不能将数据插入MySQL?

时间:2015-06-20 00:50:56

标签: php mysql

我的想法是将数据放在文本字段中并将其传递给数据库MySQL。当文本字段填满并单击按钮时,没有任何反应。当我检查我的数据库时,那里什么也没有。这是为什么?有人可以帮帮我吗?

<?php
//Connection string for the database MySQL
require 'db.php';
//Check if it's set.
if(isset($_POST['dateLoanGrantedMonth'], 
         $_POST['dateLoanGrantedDay'], 
         $_POST['dateLoanGrantedYear'], 
         $_POST['maturityMonth'], 
         $_POST['maturityDay'], 
         $_POST['maturityYear'],                          
         $_POST['expiryMonth'], 
         $_POST['expiryDay'], 
         $_POST['expiryYear'], 
         $_POST['mrs'], 
         $_POST['name'], 
         $_POST['resident'], 
         $_POST['loanInWords'], 
         $_POST['pesos'], 
         $_POST['interest'],              
         $_POST['daysMonths'], 
         $_POST['pesosInWords'], 
         $_POST['pesos2'], 
         $_POST['penaltyInterest'], 
         $_POST['itemType'], 
         $_POST['grams'], 
         $_POST['idPresented'], 
         $_POST['contactNumber'],                
         $_POST['birthdayMonth'], 
         $_POST['birthdayDay'], 
         $_POST['birthdayYear'], 
         $_POST['sex'], 
         $_POST['citizenship'], 
         $_POST['complexion'], 
         $_POST['principal'],                                      
         $_POST['interestAbsoluteAmount'], 
         $_POST['serviceChargeAmount'], 
         $_POST['netProceeds'], 
         $_POST['perAnum'], 
         $_POST['perMonth'], 
         $_POST['others'], 
         $_POST['descriptionPawn']))
{
    //loan_assignment
    $dateLoanGrantedMonth = mysqli_real_escape_string($connection, $_POST['dateLoanGrantedMonth']);
    $dateLoanGrantedDay   = mysqli_real_escape_string($connection, $_POST['dateLoanGrantedDay']);
    $dateLoanGrantedYear  = mysqli_real_escape_string($connection, $_POST['dateLoanGrantedYear']);
    //collateral
    $itemType             = mysqli_real_escape_string($connection, $_POST['itemType']);
    $descriptionPawn      = mysqli_real_escape_string($connection, $_POST['descriptionPawn']);
    $maturityMonth        = mysqli_real_escape_string($connection, $_POST['maturityMonth']);
    $maturityDay          = mysqli_real_escape_string($connection, $_POST['maturityDay']);
    $maturityYear         = mysqli_real_escape_string($connection, $_POST['maturityYear']);
    $expiryMonth          = mysqli_real_escape_string($connection, $_POST['expiryMonth']);
    $expiryDay            = mysqli_real_escape_string($connection, $_POST['expiryDay']);
    $expiryYear           = mysqli_real_escape_string($connection, $_POST['expiryYear']);

    $mrs                  = mysqli_real_escape_string($connection, $_POST['mrs']);
    $resident             = mysqli_real_escape_string($connection, $_POST['resident']);
    $loanInWords          = mysqli_real_escape_string($connection, $_POST['loanInWords']);
    $pesos                = mysqli_real_escape_string($connection, $_POST['pesos']);
    $interest             = mysqli_real_escape_string($connection, $_POST['interest']);
    $daysMonths           = mysqli_real_escape_string($connection, $_POST['daysMonths']);
    $pesosInWords         = mysqli_real_escape_string($connection, $_POST['pesosInWords']);
    $pesos2               = mysqli_real_escape_string($connection, $_POST['pesos2']);
    $penaltyInterest      = mysqli_real_escape_string($connection, $_POST['penaltyInterest']);
    $grams                = mysqli_real_escape_string($connection, $_POST['grams']);
    //pawner
    $name                 = mysqli_real_escape_string($connection, $_POST['name']);
    $idPresented          = mysqli_real_escape_string($connection, $_POST['idPresented']);
    $contactNumber        = mysqli_real_escape_string($connection, $_POST['contactNumber']);
    $birthdayMonth        = mysqli_real_escape_string($connection, $_POST['birthdayMonth']);
    $birthdayDay          = mysqli_real_escape_string($connection, $_POST['birthdayDay']);
    $birthdayYear         = mysqli_real_escape_string($connection, $_POST['birthdayYear']);

    $sex                  = mysqli_real_escape_string($connection, $_POST['sex']);
    $citizenship          = mysqli_real_escape_string($connection, $_POST['citizenship']);
    $complexion           = mysqli_real_escape_string($connection, $_POST['complexion']);
    //loan
    $principal            = mysqli_real_escape_string($connection, $_POST['principal']);
    $interestAbsoluteAmount = mysqli_real_escape_string($connection,$_POST['interestAbsoluteAmount']);
    $serviceChargeAmount  = mysqli_real_escape_string($connection, $_POST['serviceChargeAmount']);
    $netProceeds          = mysqli_real_escape_string($connection, $_POST['netProceeds']);

    $perAnum              = mysqli_real_escape_string($connection, $_POST['perAnum']);
    $perMonth             = mysqli_real_escape_string($connection, $_POST['perMonth']);
    $others               = mysqli_real_escape_string($connection, $_POST['others']);


    // Insert query for loan.
    $sql = "INSERT INTO loan (Principal_Loan, Interest_Absolute_Amount, Service_Charge, Net_Proceeds)".
            "VALUES ($principal, $interestAbsoluteAmount, $serviceChargeAmount, $netProceeds);"; 

    // Insert query for pawner.
    $sql .= "INSERT INTO pawner (Name, Birthdate, Contact_Number, Resident, Valid_ID)". 
            "VALUES ('".$name."', STR_TO_DATE('".$birthdayYear."-".$birthdayMonth."-".$birthdayDay."', '%Y-%M-%d'), 
            '".$contactNumber."', '".$resident."', '".$idPresented."');";

    // Insert query for collateral.
    $sql .= "INSERT INTO collateral (Type, Description, Maturity_Date, Expiry_Date)".
            "VALUES ('".$itemType."', '".$descriptionPawn."', STR_TO_DATE('".$maturityYear."-".$maturityMonth."-".$maturityDay."', '%Y-%M-%d'), 
            STR_TO_DATE('".$expiryYear."-".$expiryMonth."-".$expiryDay."', '%Y-%M-%d'));";

    // Insert query for loan_assignment.
    $sql .= "INSERT INTO loan_assignment (Date_Loan_Granted, Renewal_Date)".
            "VALUES (STR_TO_DATE('".$dateLoanGrantedYear."-".$dateLoanGrantedMonth."-".$dateLoanGrantedDay."', '%Y-%M-%d'), CURDATE())";

    mysqli_multi_query($connection, $sql);
    mysqli_close($connection);  
//require 'pdf';
}
?>

这是我传递数据的HTML代码,当我点击保存并打印时,它应该将数据传递给数据库。

<html>
<head>
    <meta charset="utf-8">
<title>Home</title>
<!--<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/custom.css">-->
<link href="css/simple-sidebar.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-custom navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <p class="systemName">Pawn Broking Management System
                    <a href="#menu-toggle" class="btn btn-default" id="menu-toggle">Toggle Menu</a>
                </p>
            </div>
            <div class="logout" align="right"><a href="#" class="textLogout">Logout</a> </div>
        </div>
    </nav>
    <br><br><br>
    <div id="wrapper">
        <div id="sidebar-wrapper">
            <ul class="sidebar-nav">
                <br><br><br><br>  
                <li>
                    <a href="PriceReference.php">Price Reference</a>
                </li>
                <li>
                    <a href="Scan.php">Scan Barcode</a>
                </li>
                <li>
                    <a data-toggle="collapse" data-target="#demo"> Manage Peónline Account <i class="fa fa-fw fa-caret-down"></i></a>
                    <ul id="demo" class="collapse">
                        <li>
                            <a href="Upload.php">Upload Photos</a>
                        </li>
                        <li>
                            <a href="ViewPost.php">View Posts</a>
                        </li>
                        <li>
                            <a href="ViewReservation.php">View Reservation</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="AllTransactions.php">All Transactions</a>
                </li>
            </ul>
        </div>
        <div id="page-content-wrapper">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <form method="post" class="formFont">
                            <div class="container">
                                <div class="row">
                                    <div class="col-md-6"></div>
                                        <div class="col-md-5"> <br><br>
                                            Maturity Date: &nbsp;&nbsp;&nbsp;
                                            <select name="maturityMonth" id = "month" onfocus = "monthFunction();">
                                                <option value = "">Month</option>
                                            </select>
                                            <select name="maturityDay" id = "day" onfocus = "dayFunction();">
                                                <option value = "">Day</option>
                                            </select>
                                            <select name="maturityYear" id = "year" onfocus = "yearFunction();" >
                                                <option value = "">Year</option>
                                            </select>
                                            <br>
                                            Expiry Date: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            <select name="expiryMonth"id = "emonth"  onfocus = "emonthFunction();">
                                                <option value = "">Month</option>
                                            </select>
                                            <select name="expiryDay" id = "eday" onfocus = "edayFunction();">
                                                <option value = "">Day</option>
                                            </select>
                                            <select name="expiryYear" id = "eyear" onfocus = "eyearFunction();" >
                                                <option value = "">Year</option>
                                            </select>
                                        </div>
                                        <div class="col-lg-10">
                                            Date Loan Granted: &nbsp;&nbsp;&nbsp;
                                            <select name="dateLoanGrantedMonth" id = "dlgmonth" onfocus = "dlgmonthFunction();">
                                                <option value = "">Month</option>
                                            </select>
                                            <select name="dateLoanGrantedDay" id = "dlgday" onfocus = "dlgdayFunction();">
                                                <option value = "">Day</option>
                                            </select>
                                            <select name="dateLoanGrantedYear" id = "dlgyear" onfocus = "dlgyearFunction();" >
                                                <option value = "">Year</option>
                                            </select>
                                            <div class="form-group">
                                                <br>
                                                <label class="radio-inline">
                                                    <input type="radio" name="mrs" value="Mr.">Mr.
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="radio" name="mrs" value="Mrs.">Mrs.
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="radio" name="mrs" value="Ms.">Ms.
                                                </label>
                                                <input type="text" name="name" class="input-custom">
                                                a resident of <input type="text" name="resident" class="input-custom"> <br><br>
                                                for a loan of <input type="text" name="loanInWords" class="input-custom"> PESOS
                                                &nbsp; &nbsp; &nbsp; &nbsp; ₱ <input type="text" name="pesos" class="input-custom-peso">
                                                with an interest of &nbsp;&nbsp;&nbsp;
                                                <select name="interest" name="interest" id="interestOf" onfocus="interestOfFunction();">
                                                    <option value="">%</option>
                                                </select><br><br>
                                                for   &nbsp; &nbsp;
                                                <select name="count" name="daysMonths" id="for" onfocus="forFunction();">
                                                    <option value = "">choose</option>
                                                </select>
                                                &nbsp;&nbsp;&nbsp;
                                                <input type="radio" name="daysM" value="days"> day/s &nbsp;&nbsp;
                                                <input type="radio" name="daysM" value="months"> month/s &nbsp;&nbsp;&nbsp;
                                                has pledge to this Pawnee as security for the loan, article(s) described below appraised<br><br>
                                                at PESOS <input type="text" name="pesosInWords" class="input-custom"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                                                ₱ <input type="text" name="pesos2"class="input-custom-peso">  &nbsp;
                                                subject to the terms and conditions <br><br>stated on the reverse side hereof. <br><br>
                                                Penalty interest, if any:
                                                <select name="penaltyInterest" id="penaltyInterest" onfocus="penaltyInterestFunction();">
                                                    <option value="0">0%</option>
                                                </select><br><br><br>
                                                <div class="col-md-6">
                                                    Description of Pawn <br><br>
                                                    <textarea class="form-control-custom2" name="descriptionPawn" rows="3" placeholder="ring, necklace, etc."></textarea> <br>
                                                    Item type  &nbsp;&nbsp;
                                                    <select name="itemType" id="itemType" onFocus="itemTypeFunction();">
                                                        <option value="">Choose</option>
                                                    </select><br><br>
                                                    Grams &nbsp;&nbsp;
                                                    <input type="text" class="customerHW"><br>
                                                    ID Presented  &nbsp;&nbsp;
                                                    <select name="idPresented" id="idPresented" onFocus="idPresentedFunction();">
                                                        <option value="">Choose</option>
                                                    </select><br><br><br>
                                                    Contact Number: <br>
                                                    <input type="text" name="contactNumber" class="contactNumber"> <br><br>
                                                    Miscellaneous <br><br> 
                                                    Birthday &nbsp;&nbsp;
                                                    <select name="birthdayMonth" id = "bmonth" onfocus = "bmonthFunction();">
                                                        <option value = "">Month</option>
                                                    </select>
                                                    <select name="birthdayDay" id = "bday" onfocus = "bdayFunction();">
                                                        <option value = "">Day</option>
                                                    </select>
                                                    <select name="birthdayYear" id = "byear" onfocus = "byearFunction();" >
                                                        <option value = "">Year</option>
                                                    </select> <br><br>
                                                    Sex &nbsp;&nbsp; 
                                                    <label class="radio-inline">
                                                        <input type="radio" name="sex" value="Male">Male
                                                    </label>
                                                    <label class="radio-inline">
                                                         <input type="radio" name="sex" value="Females">Female
                                                    </label> <br><br>
                                                    Citizenship <br>
                                                    <input type="text" name="citizenship" class="customerHW"><br>
                                                    Complexion <br>
                                                    <input type="text" name="complexion" class="customerHW"><br>
                                                    <hr>
                                                </div>
                                                <br><br>
                                            <div class="col-md-6">
                                                <a class="principalText">Principal &nbsp;&nbsp;</a>
                                                ₱ <input type="text" name="principal" class="input-custom"> <br><br>
                                                <a class="principalText">Interest in absolute amount</a>
                                                <select name="interestAbsoluteAmount" id="interestAbsoluteAmount" onfocus="interestAbsoluteAmountFunction();">
                                                    <option value="0">0%</option>
                                                </select><br><br>
                                                <a class="SCA">Service Charge in amount</a>
                                                <input type="text" name="serviceChargeAmount" class="input-custom"> <br><br>
                                                <a class="NP">Net Proceeds</a>
                                                ₱ <input type="text" name="netProceeds" class="input-custom"> <br><br><br>
                                                Effective Interest Rate in Percent <br>
                                                Please check: <br><br>
                                                <input type="checkbox" name="perAnum" value="perAnum"> &nbsp;Per Anum &nbsp;&nbsp;
                                                <input type="checkbox" name="perMonth" value="perMonth"> &nbsp;Per Month &nbsp;&nbsp; <br>
                                                <input type="checkbox" name="others" value="others"> &nbsp;Others
                                                <input type="text" class="input-custom"> <br><br><br><br>  
                                                <hr><br><br><br><br><br><br><br><br><br><br><br><br>
                                                Appraiser Name
                                                <input type="text" name="appraiserName" class="input-custom"> <br><br><br><br>
                                                    <button id="" type="submit" class="btn btn-danger" align="right">Save and Print</button>
                                            </div> 
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <script src="js/jquery.js"></script>
        <script src="js/custom.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script>
            $("#menu-toggle").click(function(e) {
                e.preventDefault();
                $("#wrapper").toggleClass("toggled");
            });
        </script>
<footer>
    <center> Copyright 2015 - PBMS. All rights reserved. </center>
</footer>
</body>
</html>

2 个答案:

答案 0 :(得分:4)

您的SQL查询没有终止分号作为查询的一部分,因此当您连接它们时,服务器无法知道一个查询的结束位置和下一个查询的开始位置。

像这样更新eacch查询:

// Insert query for loan.
$sql = "INSERT INTO loan".
       "(Principal_Loan, Interest_Absolute_Amount, Service_Charge, Net_Proceeds)".
       "VALUES ('".$principal."', '".$interestAbsoluteAmount."', '".$serviceChargeAmount."', '".$netProceeds."');";
Over there -->                                                                              Add semi-colon here ^

此外,您应该检查调用mysqli的返回值,以确保不会报告任何错误。如果不这样做,mysqli将无声地失败。

这对于单个查询来说很简单 - 只需检查返回值,如果是错误,请查看mysqli_error()

$result = mysqli_query($con, 'Some Query...');
if ($result === false) {
    echo mysqli_error($con);
    exit;
}

如果您使用的是multi-query,则会更复杂,因为您必须调用mysqli_next_result()来检查每个查询的返回值。手册here;

中给出了更多的详细信息

除非您有真正的性能问题,否则multi-query()几乎没有任何好处和复杂性。我建议你使用一系列单个查询。如果您担心“全部或全部”更新,请将整个系列包装在一个事务中。

答案 1 :(得分:1)

我在sql语句之间看不到任何分隔符。

mysqli_multi_query:执行一个或多个以分号连接的查询。