gpt4 book ai didi

php - MySQL 在整数前不存储零(字段设置为文本(或 varchar))

转载 作者:可可西里 更新时间:2023-11-01 08:06:49 24 4
gpt4 key购买 nike

我正在通过 HTML 将表单提交到 POST PHP 页面,然后该页面将该信息存储到 MySQL 数据库中。输入字段之一是可以从零开始的数字字段。我已将 HTML 数据类型设置为表单上的文本,并尝试将 MySQL 数据类型设置为文本和 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 数据库时被删除了。我不确定为什么要这样做......

最佳答案

intval() 的意思是“获取字符串并将其转换为整数”,这就是导致字符串 "01234" 以数字形式出现的原因1234

$pairs[] = '('.intval($key).','.intval($value).')'; 和数字字段中删除它,因为你想将其作为文本插入;将其封装在引号 '$value' 中。

关于php - MySQL 在整数前不存储零(字段设置为文本(或 varchar)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11838345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com