我的代码存在这个问题一直困扰着我几天。我正在尝试做一个我的用户可以请求交付的项目,他可以根据他的需要选择一组项目。事情是,当我将数据传递到数组时,它只捕获第一行并忽略其余部分。
因为我在这里,如果有可能,任何人都可以帮我找到一种方法来插入我进入sql的数据。这是我目前的代码
<?php
$sql="select * from Material_Equipamento";
$res=mysql_query($sql);
$sql2="select * from Fornecedores";
$res2=mysql_query($sql2);
?>
<body>
<div class="container">
<h4 align="center"> Formulário Encomenda</h4>
<form class="form-inline" role="form" align="center" method="POST" action="index.php?cmd=ins-enc">
<div class="form-group">
<label class="control-label " for="dp">Data do Pedido:</label>
<div class="col-sm-10"><input type="text" class="form-control" name="dp" value="" placeholder="DD/MM/AAAA">
</div>
</div>
<div class="form-group">
<label class="control-label " for="de">Data de Entrega:</label>
<div class="col-sm-10"><input type="text" class="form-control" name="de" value="" placeholder="DD/MM/AAAA">
</div>
</div>
<div class="form-group">
<label class="control-label " for="f">Fornecedor :</label>
<select class="form-control" id="forn" name='forn'>
<?php
while ($lin2=mysql_fetch_array($res2, MYSQL_ASSOC)){
echo "<option value='";
echo $lin2['n_Fornecedor'];
echo "'>";
echo $lin2['Nome'];
echo "</option>";
}
?>
</select>
</div>
<br><br>
<div class="row clearfix">
<div class="col-md-12 table-responsive">
<table class="table table-bordered table-hover table-sortable" id="tab_logic">
<thead>
<tr >
<th class="text-center">Material</th>
<th class="text-center">Quantidade</th>
<th class="text-center">Valor previsto por Unidade</th>
<th class="text-center" style="border-top: 1px solid #ffffff; border-right: 1px solid #ffffff;">
</th>
</tr>
</thead>
<tbody>
<tr id='addr0' data-id="0" >
<td data-name="mat">
<select class="form-control" name="mat[]">
<?php
while ($lin=mysql_fetch_array($res, MYSQL_ASSOC)){
echo "<option value='";
echo $lin['n_Mat_Equip'];
echo "'>";
echo $lin['Nome'];
echo "</option>";
}
?>
</select>
</td>
<td data-name="quant">
<input type="text" name='quant[]' placeholder='Quantidade' class="form-control"/>
</td>
<td data-name="Valuni">
<input type="text" name='valuni[]' placeholder='Valor Previsto por Unidade' class="form-control"/>
</td>
<td data-name="del">
<button nam"del0" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<a id="add_row" class="btn btn-default pull-right">Outro item</a>
<br><input type="submit" value="Adicionar Material">
<input type="reset" value="Limpar ecrã">
</form>
</div>
</body>
</html>
表单和php从我的表中获取内容,我也运行此脚本,以便用户可以按照自己的意愿添加更多项目。
<script>
$(document).ready(function() {
$("#add_row").on("click", function() {
// Dynamic Rows Code
// Get max row id and set new id
var newid = 0;
$.each($("#tab_logic tr"), function() {
if (parseInt($(this).data("id")) > newid) {
newid = parseInt($(this).data("id"));
}
});
newid++;
var tr = $("<tr></tr>", {
id: "addr"+newid,
"data-id": newid
});
// loop through each td and create new elements with name of newid
$.each($("#tab_logic tbody tr:nth(0) td"), function() {
var cur_td = $(this);
var children = cur_td.children();
// add new td and element if it has a nane
if ($(this).data("name") != undefined) {
var td = $("<td></td>", {
"data-name": $(cur_td).data("name")
});
var c = $(cur_td).find($(children[0]).prop('tagName')).clone().val("");
c.attr("name", $(cur_td).data("name") + newid);
c.appendTo($(td));
td.appendTo($(tr));
} else {
var td = $("<td></td>", {
'text': $('#tab_logic tr').length
}).appendTo($(tr));
}
});
// add delete button and td
/*
$("<td></td>").append(
$("<button class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>")
.click(function() {
$(this).closest("tr").remove();
})
).appendTo($(tr));
*/
// add the new row
$(tr).appendTo($('#tab_logic'));
$(tr).find("td button.row-remove").on("click", function() {
$(this).closest("tr").remove();
});
});
// Sortable Code
var fixHelperModified = function(e, tr) {
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function(index) {
$(this).width($originals.eq(index).width())
});
return $helper;
};
$(".table-sortable tbody").sortable({
helper: fixHelperModified
}).disableSelection();
$(".table-sortable thead").disableSelection();
$("#add_row").trigger("click");
});
</script>
我不明白问题出在哪里= /我知道很多代码很抱歉如果你们中的任何人最终都读完了。但是问题是它只会在您进入页面时读取立即出现的预定义行,但不会读取用户添加的其他行。谢谢你的合作=)。顺便说一句,我使用bootstrap作为视觉效果
答案 0 :(得分:0)
我看到你还在使用mysql_ *函数,它们已被弃用我建议你切换到mysqli_ *函数或PDO。如果你想坚持使用mysql_ *函数,我建议你使用http://php.net/manual/en/function.mysql-fetch-row.php,否则我有一个PDO解决方案:
查询部分
<?php
$pdo = new PDO(...);
$query = $pdo->query('SELECT * FROM Material_Equipamento');
$materials = $query->fetchAll();
$query = $pdo->query('SELECT * FROM Fornecedores');
$results = $query->fetchAll();
?>
您需要以下链接:
http://php.net/manual/en/pdo.construct.php
http://php.net/manual/en/pdo.query.php
Foreach循环
<?php
foreach($materials as $material)
{
?>
<option value="<?=$material['n_Mat_Equip'];?>"><?=$material['Nome'];?></option>
<?php
}
?>
答案 1 :(得分:0)
You missing brackets in your javascript code when you duplicate the fields. Try this :
// loop through each td and create new elements with name of newid
$.each($("#tab_logic tbody tr:nth(0) td"), function() {
var cur_td = $(this);
var children = cur_td.children();
// add new td and element if it has a nane
if ($(this).data("name") != undefined) {
var td = $("<td></td>", {
"data-name": $(cur_td).data("name")
});
var c = $(cur_td).find($(children[0]).prop('tagName')).clone().val("");
//Test if the initial field have brackets and is define...
if($(cur_td).find($(children[0]).prop('tagName')).attr('name') != undefined){
//Is there bracket?
if( $(cur_td).find($(children[0]).prop('tagName')).attr('name').indexOf('[') != -1){
c.attr("name", $(cur_td).data("name") + newid + '[]');
}else{
c.attr("name", $(cur_td).data("name") + newid);
}
}else{
c.attr("name", $(cur_td).data("name") + newid);
}
c.appendTo($(td));
td.appendTo($(tr));
} else {
var td = $("<td></td>", {
'text': $('#tab_logic tr').length
}).appendTo($(tr));
}
});