将多个具有相同名称的文本输入插入到 MySql 中的一个单元格中
Insert multiple text inputs with same name, into one cell in MySql
我有多个输入
<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)
在表单提交(使用 $_POST)上,我想获取所有输入(顺便说一下,我们现在永远不会有多少输入 [动态 jquery 添加/删除输入表单])并将它们组合成一个变量,所有这些都用 ^^^ 很好地分隔。
顺便说一句,我正在使用这种格式插入到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());
所以最后我想将所有动态输入打包到一个变量中,每个变量用 ^^^ 分隔。下面的示例是我希望变量$allmixtapetracks的外观,因此当我插入它时,它看起来完全一样。
输入值 1^^^输入值 2^^^输入值 3^^^输入值 4^^^
编辑:
此编码导致我的错误。当输入是动态创建的,php $_POST 会忽略它们。为什么?我不确定。
JavaScript:
<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>
.HTML:
<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']) . '^^^';
测试代码:
<?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>
我强烈建议不要在一列中存储多个值。正确的策略是创建一个规范化的表,将混音带链接到轨道:
CREATE TABLE tapetracks (
mixtapeid INT NOT NULL,
trackname VARCHAR(256) NOT NULL
);
然后循环访问输入值并将每个值插入到表中:
$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;
}
}
在一列中存储多个值时,您将无法在查询时使用列索引,并且查询单个值变得非常麻烦,需要调用 REPLACE()
和 FIND_IN_SET()
如果曲目名称包含逗号,则可能会失败。 这确实是错误的策略。
是的,当然可以:
$combined = "";
foreach ( $_POST['track'] as $t ){
$combined .= $t . "^^^";
}
$combined = substr(0,-3, combined) // only if you want to strip last 3 "^^^" symbols
这应该可以做到....
相关文章:
- 使用jquery在单击时在单元格中输入值
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- 可以'我不明白为什么;t将行和单元格添加到表中
- 如何在Angular中的表的所有单元格中添加链接
- 表中单元格的总和
- ExtJS网格单元格编辑器,防止焦点松动问题
- 有没有一种方法可以仅使用HTML/CSS来隐藏基于特定值的数据单元格
- 区分电子表格中的空单元格和0值
- ui网格将单元格显示为选择标记
- 单元格的工具提示或标题不显示超过2000个字符
- ui网格日期单元格过滤器,过滤日期格式导致显示错误的日期
- 允许表单元格内容水平展开
- 如何将jQuery/AAJAX结果放入表格单元格
- 使用jQuery只返回选中复选框后的第一个表单元格值
- jQuery-通过AJAX调用动态添加具有值的表单元格
- 需要将单元格值复制到html表中的其他单元格中
- 从Javascript中选定的行中获取特定单元格的值
- 有没有一种方法可以通过列类型/属性来格式化MySQL单元格数据
- 将多个具有相同名称的文本输入插入到 MySql 中的一个单元格中
- 如何根据用户所在的页面将值插入到一个单元格的mysql数据库中