【问题标题】:MySQL INSERT command is converting inputs into different formats/valuesMySQL INSERT 命令正在将输入转换为不同的格式/值
【发布时间】:2012-08-04 20:34:49
【问题描述】:

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

  1. 如果在表单的数字字段中输入了诸如 0125 之类的数字,则在 MySQL 表中将其存储为 125。这与将字段存储为文本字符串这一事实无关。

  2. 当只输入整数时,审批字段工作得非常好,但是,当只插入文本或文本和数字的组合时,MySQL 查询会产生以下错误:错误:'中的未知列'aft859'字段列表”。比如输入853234,一切正常,但是输入aft859,就报错了。

  3. 当输入日期时,它会以十进制值的形式输入到 MySQL 表中。例如,日期 08/07/2012 保存为“0.00056802”。

我检查了每一行以确保 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-->

【问题讨论】:

    标签: php javascript mysql html arrays


    【解决方案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) . "'"))
    

    我认为这样就可以了。

    【讨论】:

      【解决方案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 . ))
      

      【讨论】:

      • 第二次更新实际上产生了一个错误,但 $pairs[] 的第一次更新运行良好!我没有意识到它将所有内容都插入为整数。好眼力!
      • @user1562781 啊,是的 - 第二次更新中断了,因为我将第一次更新为$pairs[] 而不是将其更改为$pairs;我已经编辑了我的答案以反映 - 但它仍然有效。
      猜你喜欢
      • 2011-04-11
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      相关资源
      最近更新 更多