[英]Insert multiple text inputs with same name, into one cell in MySql
I have multiple inputs我有多个输入
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
...and so on (depending on how many they want)
On form submit (using $_POST), i want to take all of the inputs (which by the way, we'll never now how many will be there [dynamic jquery add/remove input form] ) and combine them into one variable, all nicely separated by ^^^.在提交表单时(使用 $_POST),我想获取所有输入(顺便说一句,我们现在永远不会知道会有多少 [动态 jquery 添加/删除输入表单])并将它们组合成一个变量,都被 ^^^ 很好地分开了。
BTW, I am using this format to insert into mysql顺便说一句,我正在使用这种格式插入 mysql
$sql = mysql_query("INSERT INTO mixtapes (title, songs, posted_by_id, description, date)
VALUES('$mixtapetitle','$allmixtapetracks','$posted_by_id','$mixtapedescription', now())")
or die (mysql_error());
So in the end i want to pack all of the dynamic inputs into one single variable, each one separated by ^^^.所以最后我想将所有动态输入打包到一个变量中,每个变量由 ^^^ 分隔。 The example below is how i want the variable $allmixtapetracks to look, so when I insert it, it looks EXACTLY like that.下面的示例是我希望变量 $allmixtapetracks 的外观,所以当我插入它时,它看起来完全像那样。
Value of input 1^^^Value Of Input 2^^^Value of Input 3^^^Value of input 4^^^输入1的值^^^输入2的值^^^输入3的值^^^输入4的值^^^
This coding is causing my error.此编码导致我的错误。 When the inputs are dynamicly created, the php $_POST ignores them.动态创建输入时,php $_POST 会忽略它们。 Why?为什么? I am not sure.我不知道。
<script type="text/javascript">
$(document).ready(function(){
var counter = 3;
$("#addButton").click(function () {
if(counter>40){
alert("The maximum allowed tracks is 40");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.html('<table><tr><td width="88"><label>Track #'+ counter + ' : </label></td><td><input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" ></td></tr></table>');
newTextBoxDiv.appendTo("#TextBoxesGroup").hide().show(50);
counter++;
});
$("#removeButton").click(function () {
if(counter==3){
alert("Your mixtape must contain at least two tracks!");
return false;
}
counter--;
$("#TextBoxDiv" + counter).remove().show().hide(40);
});
});
</script>
<form action="add-mixtape.php" name="addmixtapeForm" class="addmixtapeForm" id="addmixtapeForm" autocomplete="off" method="post" enctype="multipart/form-data">
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<table>
<tr>
<td width="88"><label>Track #1: </label></td>
<td><input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" ></td>
</tr>
</table>
<table>
<tr>
<td width="88"><label>Track #2: </label></td>
<td><input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" ></td>
</tr>
</table>
</div>
</div>
<input type="button" value="Add Track" id="addButton" class="addREMOVEmixtapetrack">
<input type="button" value="Remove Track" id="removeButton" class="addREMOVEmixtapetrack">
</form>
$alltracks = implode('^^^', $_POST['track']) . '^^^';
Test code:测试代码:
<?php
$alltracks = implode('^^^', $_POST['track']) . '^^^';
echo $alltracks;
?>
<form action="test.php" method="post">
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input size="32" class="mixtapetrack" type="text" id="track[]" name="track[]" >
<input type="submit">
</form>
I strongly recommend against storing multiple values in one column.我强烈建议不要在一列中存储多个值。 The correct strategy is to create a normalized table which links mixtapes to tracks:正确的策略是创建一个规范化表,将混音带链接到曲目:
CREATE TABLE tapetracks (
mixtapeid INT NOT NULL,
trackname VARCHAR(256) NOT NULL
);
Then loop over the input values and insert each into the table:然后遍历输入值并将每个值插入表中:
$mixtapeid = <the current tape you are adding to>;
foreach ($_POST['track'] AS $track) {
$track = mysql_real_escape_string($track);
$result = mysql_query("INSERT INTO tapetracks (mixtapeid, trackname) VALUES ($mixtapeid, '$track')");
if (!$result) {
echo mysql_error();
break;
}
}
When storing multiple values in one column, you lose the ability to utilize a column index when querying, and it becomes a big hassle to query for individual values, requiring calls to REPLACE()
and FIND_IN_SET()
which may fail if the track names contain commas.当在一列中存储多个值时,您在查询时失去了使用列索引的能力,并且查询单个值变得很麻烦,需要调用REPLACE()
和FIND_IN_SET()
如果曲目名称包含,这可能会失败逗号。 It really is the wrong strategy.这确实是错误的策略。
Yes, sure you can:是的,你当然可以:
$combined = "";
foreach ( $_POST['track'] as $t ){
$combined .= $t . "^^^";
}
$combined = substr(0,-3, combined) // only if you want to strip last 3 "^^^" symbols
This should do it....这应该做到......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.