MySQL INSERT命令将输入​​转换为不同的格式/值

时间:2012-08-07 19:15:12

标签: php javascript mysql html arrays

我有一个HTML输入表单,它作为PHP_SELF发送,然后存储在MySQL表中。输入字段由JS动态控制。有4个输入:数字(文本),金额(整数),批准(文本)和日期(文本)。当输入通过POST发送到PHP文件时,输入都是所有数组。它们存储在MySQL表中,列字段为:ID(主键),num(text(4)),amnt(十进制(8,2)),app(text(10))和日期(varchar( 10))。当我尝试运行页面时,我遇到了三个重大错误。

  1. 如果在表单的数字字段中输入了这样的数字0125,它将在MySQL表中存储为125。这与字段存储为文本字符串无关。

  2. 当仅输入整数时,Approval字段可以正常工作,但是,当只插入文本或文本和数字的组合时,MySQL查询会产生以下错误:错误:未知列'aft859'在'字段列表'。例如,当输入853234时,一切都很顺利,但是当输入aft859时,就会产生错误。

  3. 输入日期后,它将作为十进制值输入MySQL表。例如,日期为08/07/2012的日期保存为“0.00056802”。

  4. 我检查了每一行,以确保在PHP或HTML进程中没有转换任何内容,并且我已回显每一行以确保正确处理这些值。经过大量调试后,我会相信以下两个部分可能会导致我的问题:

    //Check To See If User Has Already Created Table
    $sql = "CREATE TABLE IF NOT EXISTS $tablename_cc (
    ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(ID),
    cc_num TEXT(4),
    cc_amnt DECIMAL(8,2),
    cc_app TEXT(10),
    cc_date VARCHAR(10)
    );";
    

    或者可能是这样:

    if (!mysql_query('INSERT INTO ' .$tablename_cc. '(cc_num, cc_amnt, cc_app, cc_date) VALUES '.implode(',',$pairs)))
        die('Error: ' . mysql_error());
        else
            echo '<strong>', "Your information have been submitted and will be added to your account upon approval.", '</strong>', "</br>", "</br>", "</br>";
    

    我对PHP,HTML或MySQL不太熟悉(这是我的第一个程序),我不确定我是否错过了什么。我试图检查所有报价并确保它们是正确的。我在Wordpress中运行所有这些,以防万一这是罪魁祸首。以下是我的完整代码供参考:

    <?php
    
    if(isset($_POST['submit']))
    {
    //Get Current User Login
    global $current_user;
    $current_user = wp_get_current_user();
    $ulog = $current_user->user_login;
    $tablename_cc = "cc_".$ulog;
    
    //Check To See If User Has Already Created Table
    $sql = "CREATE TABLE IF NOT EXISTS $tablename_cc (
    ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(ID),
    cc_num TEXT(4),
    cc_amnt DECIMAL(8,2),
    cc_app TEXT(10),
    cc_date VARCHAR(10)
    );";
    
    mysql_query($sql);
    
    $cc_num = $_POST['cc_num'];
    $cc_amnt = $_POST['cc_amnt'];
    $cc_app = $_POST['cc_app'];
    $cc_date = $_POST['cc_date'];
    
    $items = array_map(null,$cc_num,$cc_amnt,$cc_app,$cc_date);
    $pairs = array();
    
    foreach ($items as $sub) {
        if(implode(',', $sub) != ",,,")
        $pairs[] = '('.implode(',', $sub).')';
    }
    
    echo implode(',',$pairs);
    
    if (!mysql_query('INSERT INTO ' .$tablename_cc. '(cc_num, cc_amnt, cc_app, cc_date) VALUES '.implode(',',$pairs)))
        die('Error: ' . mysql_error());
        else
            echo '<strong>', "Your information has been submitted and will be added to your account upon approval.", '</strong>', "</br>", "</br>", "</br>";
    
    }
    ?>
    
    <!--raw-->
    <html>
    
    <head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script src="jquery.maskedinput.js" type="text/javascript"></script>
    
    <script type="text/javascript">
        $(document).ready(function() {
            $('#btnAdd').click(function() {
                var num     = $('.ccinput').length; // how many "duplicatable" input fields we currently have
                var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added
    
                // create the new element via clone(), and manipulate it's ID using newNum value
                var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
    
                // insert the new element after the last "duplicatable" input field
                $('#input' + num).after(newElem);
    
                // enable the "remove" button
                $('#btnDel').attr('disabled','');
    
                $("*#date").mask("99/99/9999");
    
                // business rule: you can only add 20 names
                if (newNum == 20)
                    $('#btnAdd').attr('disabled','disabled');
            });
    
            $('#btnDel').click(function() {
                var num = $('.ccinput').length; // how many "duplicatable" input fields we currently have
                $('#input' + num).remove();     // remove the last element
    
                // enable the "add" button
                $('#btnAdd').attr('disabled','');
    
                // if only one element remains, disable the "remove" button
                if (num-1 == 1)
                    $('#btnDel').attr('disabled','disabled');
            });
    
            $("*#date").mask("99/99/9999");
        });
    </script>
    </head>
    
    <body>
    
    Please fill in your information in the form below and press submit. If you need to add more, please click the "Add" button at the bottom of the form. You may enter a maximum of 20 at a time. Leave all unused fields blank.
    
    <form method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
    <fieldset>
    <legend>Information:</legend>
    <div id="input1" class="ccinput">
        # (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
    </div>
    <div id="input2" class="ccinput">
        # (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
    </div>
    <div id="input3" class="ccinput">
        # (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
    </div>
    <div id="input4" class="ccinput">
        # (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
    </div>
    <div id="input5" class="ccinput">
        # (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
    </div>
    <div>
        <input type="button" id="btnAdd" value="Add" />
        <input type="button" id="btnDel" value="Remove" />
    </div>
    </fieldset>
    <input type="submit" value="Submit" name="submit" />
    </form>
    
    </body>
    </html>
    <!--/raw-->
    

2 个答案:

答案 0 :(得分:2)

您的文本值被截断(丢失前导零)的原因是因为您将它们作为数字插入而MySQL正在丢弃前导零,并且日期变为小数是因为您将它们作为方程式和MySQL插入正在评估它们。

原因是这一行:

$pairs[] = '('.implode(',', $sub).')';

没有任何值被引号括起来。您可以通过以下方式解决此问题:

$pairs = '("'.implode('","', $sub).'")';

然后,您在该行已经内爆的列表上再次使用implode()

if (!mysql_query('INSERT INTO ' .$tablename_cc. '(cc_num, cc_amnt, cc_app, cc_date) VALUES '.implode(',',$pairs)))

将此行更新为:

if (!mysql_query('INSERT INTO ' .$tablename_cc. '(cc_num, cc_amnt, cc_app, cc_date) VALUES ' .$pairs . ))

答案 1 :(得分:1)

if (!mysql_query('INSERT INTO ' .$tablename_cc. '(cc_num, cc_amnt, cc_app, cc_date) VALUES '.implode(',',$pairs)))

您需要将输入值包装在撇号中;否则,它们将被视为数字 - 这就是为什么你得到Unknown column错误,并且日期被视为总和而不是日期。

if (!mysql_query("INSERT INTO " .$tablename_cc. " (cc_num, cc_amnt, cc_app, cc_date) VALUES '".implode("','",$pairs) . "'"))

我认为会这样做。