【发布时间】:2016-03-28 03:44:33
【问题描述】:
See the image here 我有一个表格收集以下信息:年份(int)、案例编号(varchar)和收到时间(varchar)。由于 year 是数字,我在 MySQL 中设置了 int,然后 Case number 和 Time Received 既有数字又有字母,因此我在 MYSQL 中设置了 varchar。我使用数组是因为我插入了很多记录。我使用 mysql_real_escape_string() 作为数组。但它没有用。如何将包含字母、符号和数字的数组传递到 MySQL 中?谢谢你。
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
if (isset($_REQUEST['submit']) && isset($_REQUEST['year']) ) {
foreach ($_REQUEST['year'] as $k=> $value ){ // loop through array
$year = $_REQUEST['year'];
$c_no = mysql_real_escape_string($_REQUEST['cs']);
$t_r = mysql_real_escape_string($_REQUEST['t_r']);
$mysqli->query("INSERT INTO firearms_id_secs (year, case_no, t_received) VALUES
($year[$k], $c_no[$k], $t_r[$k])");
}
}
?>
这是 HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add more fields using jQuery</title>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var maxField = 10; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
var fieldHTML = '<div><input type="text" name="year[]" value="" placeholder="Year"/><input type="text" name="cs[]" value="" placeholder="Case no"/><input type="text" name="t_r[]" value="" placeholder="Time Received"/><a href="javascript:void(0);" class="remove_button" title="Remove field"><img src="remove-icon.png"/></a></div>'; //New input field html
var x = 1; //Initial field counter is 1
$(addButton).click(function(){ //Once add button is clicked
if(x < maxField){ //Check maximum number of input fields
x++; //Increment field counter
$(wrapper).append(fieldHTML); // Add field html
}
});
$(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
x--; //Decrement field counter
});
});
</script>
<style type="text/css">
input[type="text"]{height:20px; vertical-align:top;}
.field_wrapper div{ margin-bottom:10px;}
.add_button{ margin-top:10px; margin-left:10px;vertical-align: text-bottom;}
.remove_button{ margin-top:10px; margin-left:10px;vertical-align: text- bottom;}
</style>
</head>
<body>
<form name="codexworld_frm" action="" method="post">
<div class="field_wrapper">
<div>
<a href="javascript:void(0);" class="add_button" title="Add field"><img src="add-icon.png"/></a>
</div>
</div>
<input type="submit" name="submit" value="SUBMIT"/>
</form>
</body>
</html>
【问题讨论】:
-
最好设置年份 VARCHAR(4) ,案例编号 VARCHAR(255) 和接收时间 DATETIME/TIMESTAMP。只是我的两分钱
-
谢谢 Willy Pt 是的。嗯,谢谢提醒。我听说了一个数据类型字典,用于正确初始化大小和数据类型的类型。我还需要那个日期时间/时间戳。可以只在 MySQL 中添加新列并将 DATETIME/TIMESTAMP 放在那里吗?
-
@WillyPt 的建议是正确的。您可以使用
TIMESTAMP数据类型并将其默认设置为CURRENT_TIMESTAMP,因此您不需要始终将其包含在查询中,它会自动包含在其中。 -
是否可以更改 html/javascript 以获得稍微不同的 POST 数据格式?
-
强烈建议您使用PDO而不是mysqli。