DEV-0
DEV-0

Reputation: 63

Why can't I insert data to MySQL?

My idea is to put data in the textfield and pass it to the database MySQL and When the text field is filled and the button is clicked nothing happened. When I checked my database there is nothing there. Why is that? Could someone please help me?

<?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';
}
?>

This is my HTML code which I pass my data and when I clicked save and print it should pass the data to the database.

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

Upvotes: 0

Views: 112

Answers (2)

user1864610
user1864610

Reputation:

Your SQL queries don't have terminating semi-colons as part of the query, so when you concatenate them there is no way for the server to know where one query ends and the next starts.

Update eacch query like this:

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

Additionally, you should check the return value of your calls to mysqli to ensure that no errors are reported. If you don't, mysqli will fail silently.

This is easy for single queries - just check the return value and if it's false look at mysqli_error()

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

If you're using multi-query it's more complicated as you have to call mysqli_next_result() to check the return value of each query. More detals are given in the manual here;

Unless you have real performance issues multi-query() offers little benefit and much complication. I'd advise you to use a series of single queries. If you're concerned about having an 'all or nothing' update, wrap the whole series in a transaction.

Upvotes: 4

Mark B
Mark B

Reputation: 682

I can't see any separators between your sql statements.

mysqli_multi_query : Executes one or multiple queries which are concatenated by a semicolon.

Upvotes: 1

Related Questions