我通过 HTML 将表单提交到 POST PHP 页面,然后将该信息存储到 MySQL 数据库中。输入字段之一是可以从零开始的数字字段。我已经在表单上将 HTML 数据类型设置为 text 并尝试将 MySQL 数据类型设置为 text 和 varchar,并且在整数被删除之前都设置为零。我不太确定我做错了什么。
这是我用于创建表的 PHP 代码:
$sql = "CREATE TABLE IF NOT EXISTS $tablename_db (
ID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID),
num text(4),
amnt DECIMAL(8,2)
);";
这是表单域的样子:
<div id="input1" class="cinput">
# (last 4 digits): <input id="cnum" type="text" name="num[]" maxlength="4" size="4" /> Amount: <input id="camnt" type="int" name="amnt[]" /> </br>
</div>
使用这个,输入到'cnum'的0125这样的数字被保存为125。我做错了什么?
编辑:这是完整的代码,所以很清楚我在做什么。这不是一个很长的代码(我试图将内容转移到这里时可能会出现拼写错误)。
<?php
if(isset($_POST['submit']))
{
//Get Current User Login
global $current_user;
$current_user = wp_get_current_user();
$ulog = $current_user->user_login;
$tablename = "db_".$ulog;
//Check To See If User Has Already Created Table
$sql = "CREATE TABLE IF NOT EXISTS $tablename (
ID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID),
num text(4),
amnt DECIMAL(8,2)
);";
mysql_query($sql);
$num = $_POST['num'];
$amnt = $_POST['amnt'];
$items = array_combine($num,$amnt);
$pairs = array();
foreach($items as $key=>$value)
{
if($key != 'submit')
{
if($value != '')
{
$pairs[] = '('.intval($key).','.intval($value).')';
}
}
}
if (!mysql_query('INSERT INTO ' .$tablename. '(num, amnt) 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>', "/n";
}
?>
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></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','');
// 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');
});
$('#btnDel').attr('disabled','disabled');
});
</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="num[]" maxlength="4" size="4" /> Amount: <input id="camnt" type="int" name="amnt[]" /> </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>
经过多次调试,我认为错误在这两个部分的某个地方。
第 1 节:
$sql = "CREATE TABLE IF NOT EXISTS $tablename_db (
ID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID),
num text(4),
amnt DECIMAL(8,2)
);";
或者它可能在这一行:
if (!mysql_query('INSERT INTO ' .$tablename. '(num, amnt) VALUES '.implode(',',$pairs)))
代码似乎一直到这一行都完美地保持了格式。因此,我认为在插入 MySQL 数据库时会删除零。我不确定它为什么会这样做......