EDIT / UPDATE逗号将值分隔成mysql行并添加新的

时间:2015-05-29 00:54:06

标签: php jquery ajax dynamic mysqli

EDIT / UPDATE逗号将值分隔成行并添加新的

我正在尝试使用不同行中的ajax编辑产品,然后更新并添加新行 使用相同名称[]

创建动态字段的表单

但不知道怎么拉它们来编辑它们然后在同一个地方更新它们因为它们在我需要更新的不同行和表中

我能想到编辑它们的唯一方法就像这个例子 http://www.infotuts.com/ajax-table-add-edit-delete-rows-dynamically-jquery-php/ 但这是每行使用1行,我有不同行的产品

不知道是否应该像这样编辑它们,但我如何拉旧行并添加新行以及链接到相同的顺序 http://www.aquim.com/web-article-229.html

我正在尝试编辑它们然后更新它们,但是我如何更新旧产品以及添加新产品并将它们链接到不同的表

编辑表格

产品1 [产品1排] [价格1] [数量1] [总计1]

产品2 [产品2排] [价格2] [数量2] [总计2]

然后用表格添加新产品

<pre>`
  <form name="test" method="post" action="">
    <div id="container">
    <p id="add_field"><a href="#"><span>&raquo; Add New Bid</span>    </a></p>
    </div>
  </form>
 $('p#add_field').click(function(){
    count += 1;
    $('#container').append(
        '<strong>Link #' + count + '</strong><br />' 
        + '<input id="field_' + count + '" name="fields[]' + '" type="text" placeholder="Product Name" /><br />' 
        + '<input id="field_' + count + '" name="price[]' + '" type="text"  placeholder="Price"/><br />' 

        );

   });`

选择旧产品并更新然后获取ID并添加新产品并链接到表

我认为可能正在进行更新的唯一方法是删除所有内容并重新插入它但我认为如果有多人这样做会导致将来发生冲突

             quotes TABLE 1

| quoteID | bid_name |状态|总计|

| ------------------------------ |

| 1 | bid1 |待定| 100 |

| 2 | bid2 | stuff_y | 200 |

              quote_products_link TABLE 2

| quoteID | productID |
| ------------------------------ |

| 1 | 4 |

| 1 | 4 |

| 2 | 5 |

| 2 | 5 |

              quote_products_link TABLE 3

| productID | product_name |价格|数量| bid_name |

| 4 | pro_1 | price_1 | quan_1 | bid_1 |

| 4 | pro_2 | price_2 | quan_2 | bid_1 |

| 5 | pro_3 | price_3 | quan_3 | bid_2 |

| 5 | pro_4 | price_4 | quan_4 | bid_2 |

2 个答案:

答案 0 :(得分:0)

你可以拉---检查---更新---删除

Psudo Code,不工作!这是为了显示流程:

// Get the current database rows that are being edited
$results = mysql_query('SELECT * FROM entries WHERE table_id=10');
$itemsInDB = [];
while($itemsInDB[] = mysql_fetch_row($results)) {}

// Iterate over the fields sent to the server
foreach($_POST['fields'] as $index=>$value) {
    // New Values that were submitted
    $field = $value;
    $price = $_POST['prices'][$index];

    // Get Rid Of Updated Rows From Pulled Array
    $found = false;
    foreach($itemsInDB as $dbindex=>$dbvalue) {
       // Check if the row exists in the database
       if ($dbvalue['field'] == $field) {
           $found = true;

           // Update the values that have changed
           if ($price !== $dbvalue['price'])
               UpdateDatabase('SET price=$price WHERE field=$field');

           // Remove the row from the pulled database rows
           array_shift($itemsInDB($dbindex,1);
       }
    }

    // insert it because it wasnt in the database
    if (!$found) 
       InsertDatabase('Values($field,$price)');
}

// Now that you have updated all the rows that were submitted
// The rows that are in the database that were not updated
// Need to be deleted because the user has removed them from the table
foreach($itemsInDB as $row) {
    $field = $row['field'];
    DeleteDatabaseRow('WHERE field='.$field.' LIMIT 1');
}

可能有更好的方法来做到这一点......这就是我在不久前遇到这个问题时的做法。

答案 1 :(得分:0)

拉动产品

$sqlString = "SELECT  
 GROUP_CONCAT(productID) AS id,
GROUP_CONCAT(productName) AS productsname,
GROUP_CONCAT(price) AS prices,
bidid FROM products GROUP BY bidid";

while($row = $result->fetch_array(MYSQLI_ASSOC)){
   $currentbid[$row['bidid']][] = $row;  

}

foreach ($currentbid as $bid => $itemList){

   foreach($itemList as $itemInfo => $loas){

 // count ids, by create a rande array or counting the wors
foreach(explode(",",$loas['id']) as $itemInfoss){

then inside the foreach - create the table rows

   $str.='<tr id="'.$itemInfoss.'" 

更新它们 取决于你如何更新它们 我正在使用序列化函数和一些自定义数据

更新它们

获取ID,我使用javascript将它们推入数组

  mysqlrow.push($js(this).attr('data-rowid'));

首先删除它们

   if(!empty($deleteoldproductsfrom)){

                foreach($erasecount as $deleteold){

                 $deleteProducts = "DELETE FROM products WHERE 

productID ='$ deleteold'“;

然后更新

    $sql_quote_add = "SELECT * FROM quotes WHERE bid_name ='$quotename' "; 

    $client_data = $con->query($sql_quote_add); 

获取ID以查看它是否与提交的任何ID匹配

   $getquoteid= mysqli_fetch_assoc($client_data);   

    $oldquoteid = $getquoteid['quoteID']; 

//这个取决于你提交它的方式,我正在做它ajax          for($ for = 0; $ for&lt; count($ data ['productName']); $ for ++){
然后在for lopp里面 我们检查ID是否与任何当前产品匹配

我爆炸了一个我提交过的数组,这是我想做的最简单的方法,然后只是在里面进行计数以便它可以循环

 if($tagArray[$for] == $pro_old_id){

    $updateold = "UPDATE products SET price='$priceNew', 

   } else
 $sql_bid = sprintf("INSERT INTO products

 }