2012-09-24 152 views
0

我有一个表单,用户可以在其中添加更多字段,因为他想...如何在数据库中存储动态添加行的值

他可以添加1,2,3行....田..

字段被成功添加,我用的是这里的代码,

<?php 
echo ' 
<form method="post" action=""> 
    <input type="hidden" name="mod" value="'.$mod.'" /> 
    <table style="width: 700px"> 
    <tr> 
     <th>Description</th> 
     <th>Quantity</th> 
     <th>Price</th> 
    </tr>'; 

// Loop to prepare the display of 100 product lines 
for ($i=0; $i<100; $i++) { 

    if ($text['quantity'][$i] == "") $text['quantity'][$i] = 1; 
    if ($text['unit'][$i] == "") $text['unit'][$i] = "0.00"; 
    // Display only the first line 
    if ($nbr_ligne == 0) $nbr_ligne = 1; 
    if ($i >= $nbr_ligne) $display = 'style="display:none"'; 
    echo ' 
    <tr id="cell'.$i.'" '.$display.'> 
     <td> 
      <textarea name="text[detail]['.$i.']">'.stripslashes($text['detail'][$i]).'</textarea> 
      <br /> 
      <a href="javascript:void(0)" onclick="javascript:document.getElementById(\'cell'.($i+1).'\').style.display=\'table-row\'; this.style.display=\'none\'">[+]</a> 
     </td> 
     <td> 
      <input name="text[quantity]['.$i.']" id="q'.$i.'" value="" size="4" /> 
     </td> 
     <td> 
      <input name="text[unit]['.$i.']" id="u'.$i.'" value="" size="7" /> USD 
     </td> 
    </tr>'; 
} 

echo ' 
    </table> 
    <input type="submit" name="save" value="Save" /> 
</form> 
'; 
?> 

现在我想将这些字段的值存储在数据库中。

我用这个代码:

if(isset($_POST['save'])) 
{ 
    echo mysql_error($db); 


     extract($_POST); 
     $insert=mysql_query(" insert into add (description, quantity, price) values ('{$text['detail'][$i]}','{$text['quantity'][$i]}','{$text['unit'][$i]}')") or die("unable to insert"); 
} 

,但它不工作。 PLZ帮助我的家伙。我非常需要它。

+0

你得到什么错误? – Gapchoos

+0

没有错误,但值不存储在数据库中。 –

+0

@Gapchoos:屏幕上显示的输出是“无法插入” –

回答

0

这里的问题是,$我没有在你的脚本初始化用于保存到数据库。您需要循环播放0到100,如果没有数字,则需要BREAK。您也正在使用reserved keyword添加。在保留的关键字上使用反引号。

extract($_POST); 

foreach(range(0,100) as $i){ 
    if(isset($text['detail'][$i]) && isset(isset($text['quantity'][$i]) && isset(isset($text['unit'][$i])){ 
     $insert= mysql_query(" INSERT INTO `add` (`description`, `quantity`, `price`) VALUES ('".$text['detail'][$i]."','".$text['quantity'][$i]."','".$text['unit'][$i]."')") or die(mysql_error()); 
    } 
    else{ 
     break; 
    } 
} 

不太喜欢循环插入的方式,但上面的代码应该可以工作,即使它显然不是最好的解决方案。一个更好的方法是创建一个单一的INSERT查询插入多行:

INSERT INTO example (name, age) 
VALUES ('John', 24), ('Katrina', 21), ('Michael', 22), ('Hui Ling', 25), ('Catherine', 29) 

如果必须循环在不同的查询,您应该使用性能方面的原因准备好的语句。

其他问题:

  1. 你打开SQL injection
  2. 您正在使用不再推荐的mysql函数。改用PDO或mysqli。
+0

没有好友。它也有相同的输出,“无法插入”...但thanx来帮助我。尝试不同的东西来解决这个问题我认为问题是别的。 –

+0

代替或死(“无法插入”)使用或死(mysql_error()) –

+0

这是错误。你的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,在第1行的'add(description,quantity,price)values('','1','0.00')'附近使用正确的语法 –

0

你需要把MySQL查询在一个循环中,如果$ i等于行的NR补充说:

if(isset($_POST['save'])) 
{ 
for ($e = 1; $e <= $i; $e++) { 
$insert=mysql_query(" insert into add (description, quantity, price) values ('".$text['detail'][$e]."','".$text['quantity'][$e]."','".$text['unit'][$e]."')") or die("unable to insert"); 
} 
} 
+0

这将导致大量的错误通知被抛出。 –

+0

@faq:没有好友。它也有相同的输出,“无法插入”...但thanx来帮助我。尝试不同的东西来解决这个问题 –

+0

没问题的老兄 – faq

相关问题