2012-02-17 50 views
0

希望这是有道理的,如果有人能帮助,请提前致谢!在数组中插入额外的行并输入到数据库中

我有一个数据显示在一个表中的数组,我可以编辑数据,然后用编辑的数据更新我的数据库。

<input type='button' value='new row' id='save' onClick="addRow()"/> 
<table id='mech_table'> 
<tr> 
<td></td> 
<td></td> 
<td><input type='text' size='30' name='value_total'id='mech_value_total' value='£0.00' readonly='readonly'/></td> 
<td></td> 
<td><input type='text' size='30' name='value_claim' id='mech_claim_total' value='£<?php echo $row_MECHT['mech_total']; ?>' readonly='readonly'/></td> 
</tr><tr> 
<th>ID</th> 
<th>Description</th> 
<th>Value</th> 
<th>%</th> 
<th>Claim</th> 
</tr> 
<?php while ($MECH = mysql_fetch_array($result)) { 
?> 
<form name='form_update' method='post' action="<?php echo $editFormAction; ?>"> 
<tr> 
<td><?php echo $MECH['id']; ?><input type='hidden' name='id[]' value='<?php echo $MECH['id'];?>' /></td> 
<td class='description_row'><input type='text' size='70' name='description[]' value='<?php echo $MECH['description'];?>' /></td> 
<td class='value_row'><input class='mech_value_total' type='text' size='30' name='value[]' value='<?php echo $MECH['value'];?>' /></td> 
<td class='percent_row'><input class='percent' type='text' size='10' name='percentclaim[]' value='<?php echo $MECH['percentclaim'];?>' /></td> 
<td class='claim_row'><input class='mech_claim_total' type='text' size='30' name='claim[]' value='<?php echo $MECH['claim'] ?>' readonly='readonly'/></td> 
</tr> 
<?php ++$ia; 
} 
?> 
</table> 
<input type='submit' value='save' id='save' /><br /><br /><br /> 
</form> 


$editFormAction = $_SERVER['PHP_SELF']; 
if (isset($_SERVER['QUERY_STRING'])) { 
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
} 

$size = count($_POST['id']); 
$i = 0; 
while($i<$size){ 
$description= $_POST['description'][$i]; 
$value= $_POST['value'][$i]; 
$percentclaim= $_POST['percentclaim'][$i]; 
$claim= $_POST['claim'][$i]; 
$id = $_POST['id'][$i]; 
$query = "INSERT INTO applicationmech 
        SET id='$id', 
         description='$description', 
         value='$value', 
         percentclaim='$percentclaim', 
         claim='$claim' 
ON DUPLICATE KEY UPDATE description='$description', 
         value='$value', 
         percentclaim='$percentclaim', 
         claim='$claim'"; 
mysql_query($query) or die ("Error in query: $query"); 
$updateGoTo = "preditvaluation.php"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; 
    $updateGoTo .= $_SERVER['QUERY_STRING']; 
} 
header(sprintf("Location: %s", $updateGoTo)); 
++$i; 
} 

我用javascript创建了一个简单的按钮,它允许我将额外的行添加到我的表中。

function addRow() { 

$('#mech_table tr:last').after('<tr><td><input type="hidden" name="id[]" value="" /></td><td class="description_row"><input type="text" size="70" name="description[]" value="" /></td><td class="value_row"><input class="mech_value_total" type="text" size="30" name="value[]" value="" /></td><td class="percent_row"><input class="percent" type="text" size="10" name="percentclaim[]" value="" /></td><td class="claim_row"><input class="mech_claim_total" type="text" size="30" name="claim[]" value="" readonly="readonly"/></td></tr>'); 

} 

我的问题是,当我点击更新按钮时,我的新行中的数据不会保存。

我一直在尝试很多不同的方法,以及如何让它工作,但每次都失败。

+0

任何消息错误? – chenio 2012-02-17 14:03:30

+0

没有错误消息,所有现有的行更新正常,但新行不会插入。即使我将代码更改为INSERT,其他行也会被忽略? – user1209361 2012-02-18 23:02:17

回答

0

新行中的数据不会有一个id值,因此数据库将无法更新UPDATE操作中的行。你需要在服务器端代码做一些事情,如:

$newids = array(); 

if (!empty($row->id)) { 
    $DB->update('tablename', $data); 
} else { 
    $newid = $DB->insert('tablename', $data); 
    $newids[] = $newid; 
} 

// Now send the new ids back to the browser so that they can be put into those rows 
// and an update operation will be performed next time 
echo json_encode($ids); 

在浏览器端,是这样的:

var ids = jQuery.parseJSON(dataFromAjax); 
var inputsNeedingIds = $('input:inputclass').filter(
    function() { 
     return ($(this).val() == "") && ($(this).attr('name') == 'id[]'); }); 
inputsNeedingIds.each(
    function(i) { 
     this.id.value = ids[i];}); 
+0

感谢您的回复,但我只是可以得到它的工作!我在这方面还很新,不太明白该怎么做。 我也从昨天开始尝试了其他一些东西,但仍然没有任何效果,似乎在INSERT INTO ON DUPLICATE KEY UPDATE位运行时忽略了添加的行? – user1209361 2012-02-18 22:49:39

0

只有在这里有问题的是,DOM是不是与你添加新的形式正确更新元素由addRow()函数补充。该表单是用开始时显示的表单元素进行分段的。

仅当您使用Ajax提交时才有可能。最容易的方法是http://jquery.malsup.com/form/#ajaxForm它很容易整合。试一试! ;)

如果form_update是表单的ID代码,然后将

$('#form_update').ajaxForm({ 
     target:  '#result-sub', 
     success:  function(){ 
      $('#result-sub').show(); 
      $('#form_update').hide(); 
    } 
}); 

添加一个div或东西id为“结果子”回馈插入的响应。