在PHP

时间:2016-01-20 02:52:07

标签: javascript php mysql comma separator

你好,我可以帮助我的代码。我创建了一个输入字段 数千或数百万时自动输入逗号分隔符。但是当我将它插入到mysql数据库中时 不会插入整个整数。在我的数据库中,我将数量设置为整数只是一个例子我在输入字段中放了12,000但是当我查看数据时它只有12个

这是我在逗号分隔符中的javascript代码:

<script>
$(document).ready(function(){

    $('input.amount').keyup(function(event) {
        var x = $(this).val();
        $(this).val(x.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ","));
    });

});

这是我的HTML代码:

<table>
<tr>
    <td>Amount</td>
    <td>
        <div class="input-group">
            <div class="input-group-addon">?</div>
            <input type="text" class="form-control amount" placeholder="Amount" id="exampleInputAmount" size="16" name="amount" required="required" />
            <div class="input-group-addon">.00</div>
        </div>
    </td>
</tr>

这是我在php中的流程代码

<?php

$transaction_id = $_POST['transid'];
$transaction_date = $_POST['dateOnlyPicker'];
$sender_id = $_POST['senderid'];
$sender_name = $_POST['sendername'];
$sender_department = $_POST['sender_department'];
$receiver_id = $_POST['receiverid'];
$receiver_name = $_POST['receivername'];
$receiver_department = $_POST['receiver_department'];
$vendor_class = $_POST['vendor_class'];
$vendor_name = $_POST['vendor_name'];
$doc_type = $_POST['doc_type'];
$doc_num = $_POST['doc_num'];
$amount = number_format($_POST['amount'], 2);
$remarks = $_POST['remarks'];

include ("dbconnection_set.inc.php");
include ("dbconnection_create.inc.php");

$sql = "INSERT INTO `tbl_encode_transmittal`(transid, transdate, sender_id, receiver_id, 
            vendor_id, doctype_id, document_number, total_amount, remarks, status_id)
            VALUES ('" . $transaction_id . "', '" . $transaction_date . "', (SELECT userid FROM `tbl_userlist` WHERE username = '" . $sender_id . "' LIMIT 1),
            (SELECT userid FROM `tbl_userlist` WHERE username = '" . $receiver_id . "' LIMIT 1),
            (SELECT vendor_id FROM `tbl_vendor` WHERE vendor_name = '" . $vendor_name . "' LIMIT 1),
            (SELECT doc_id FROM `tbl_doctype` WHERE document_type = '" . $doc_type . "' LIMIT 1), '" . $doc_num . "', '" . $amount . "', '" . $remarks . "',
            (SELECT status_id FROM tbl_status WHERE status_id = '1'))";

if($conn->query($sql) === TRUE){
    #echo "New Record created successfully";
    header('refresh: 5; url=encode_transmittal.php');
    echo "<div class=\"notification top-left\"></div>";
}
else{
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

&GT;

1 个答案:

答案 0 :(得分:1)

在保存到数据库之前删除逗号,因为如果不这样,则值将是这样的(ex.amount是'12,000'):

VALUES ('1', '01/21/2016', 12, 000)

这意味着为total_amount插入的唯一值是逗号之前的第一个值,即12。

你可以使用它,它使用简单的字符串操作:

$amount = str_replace(',', '', $_POST['amount']);

或者您在评论中提到的内容,它使用正则表达式,然后将字符串结果转换为float:

$amount = floatval(preg_replace("/[^-0-9\.]/","",$_POST['amount']));