简体   繁体   English

编辑/更新逗号分隔值到mysql行并添加新

[英]EDIT/UPDATE comma separated values into mysql rows and add new

EDIT/UPDATE comma separated values into rows and add new 编辑/更新逗号将值分隔成行并添加新的

I am trying to edit products with ajax in different rows then update and add new rows from a form creating dynamic fields using the same name[] 我正在尝试使用不同行的Ajax编辑产品,然后从使用相同名称创建动态字段的表单中更新和添加新行[]

but dont know how to pull them to edit them then update them in the same spot since they are in different rows and tables where i need updating 但不知道如何拉动它们进行编辑,然后在同一位置更新它们,因为它们位于我需要更新的不同行和表中

the only way i can think of editing them is like this example http://www.infotuts.com/ajax-table-add-edit-delete-rows-dynamically-jquery-php/ but this is using the 1 row for every line and i have products in different rows 我能想到的编辑它们的唯一方法是像以下示例:http://www.infotuts.com/ajax-table-add-edit-delete-rows-dynamically-jquery-php/,但这是每行使用1行线,我有不同行的产品

dont know if should edit them live like this, but how would i pull the old rows and add new rows as well linked to the same order http://www.aquim.com/web-article-229.html 不知道是否应该像这样编辑它们,但是我将如何拉旧的行并添加新的行以及链接到相同顺序的http://www.aquim.com/web-article-229.html

I was trying to edit them then update them something like, But how do i update the old products as well as adding new products and linking them to the different tables 我试图编辑它们,然后更新它们,例如,但是我如何更新旧产品以及添加新产品并将它们链接到不同的表

EDIT FORM 编辑表格

product 1 [product 1 row] [price 1] [quanity 1] [total 1] 产品1 [产品1行] [价格1] [数量1] [总计1]

product 2 [product 2 row] [price 2] [quanity 2] [total 2] 产品2 [产品2行] [价格2] [数量2] [总计2]

then with a form add new products 然后用表格添加新产品

<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 />' 

        );

   });`

SELECT OLD PRODUCTS AND UPDATE THEN GET ID AND ADD NEW PRODUCTS AND LINK TO TABLE 选择旧产品并更新,然后获取ID并添加新产品并链接到表格

The only way i thought about probably doing the updating is erasing everything and reinserting it but i assume that would cause a clash in the future if multiple people are doing it 我考虑可能进行更新的唯一方法是擦除所有内容并重新插入它,但我认为如果多个人这样做,将来会引起冲突

             quotes TABLE 1

| | quoteID | quoteID | bid_name | bid_name | status | 状态| total | 总计

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

| | 1 | 1 | bid1 | bid1 | pending | 待处理| 100 | 100 |

| | 2 | 2 | bid2 | bid2 | stuff_y | stuff_y | 200 | 200 |

              quote_products_link TABLE 2

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

| | 1 | 1 | 4 | 4 |

| | 1 | 1 | 4 | 4 |

| | 2 | 2 | 5 | 5 |

| | 2 | 2 | 5 | 5 |

              quote_products_link TABLE 3

| | productID | productID | product_name | 产品名称| price | 价格| quanity | 数量| bid_name | bid_name |

| | 4 | 4 | pro_1 | pro_1 | price_1 | price_1 | quan_1 | quan_1 | bid_1 | bid_1 |

| | 4 | 4 | pro_2 | pro_2 | price_2 | 价格_2 | quan_2 | quan_2 | bid_1 | bid_1 |

| | 5 | 5 | pro_3 | pro_3 | price_3 | price_3 | quan_3 | quan_3 | bid_2 | bid_2 |

| | 5 | 5 | pro_4 | pro_4 | price_4 | price_4 | quan_4 | quan_4 | bid_2 | bid_2 |

You could Pull --- Check --- Update --- Delete 您可以拉---检查---更新---删除

Psudo Code, Non-working! 伪代码,不起作用! This is to show the flow: 这是显示流程:

// 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');
}

There may be a better way to do this... This is how I did it when I encountered this problem a while ago. 可能有更好的方法来执行此操作...这是我前一段时间遇到此问题时所采用的方法。

to pull the products 拉产品

$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.'" 

to update them depends on how you are updating them I am updating them using the serialize function and some custom data 更新它们取决于您如何更新它们,我正在使用序列化功能和一些自定义数据来更新它们

to get the ids, i use javascript to push them in an array 要获取ID,我使用JavaScript将其推入数组中

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

to delete them first 首先删除它们

   if(!empty($deleteoldproductsfrom)){

                foreach($erasecount as $deleteold){

                 $deleteProducts = "DELETE FROM products WHERE 

productID='$deleteold' "; productID ='$ deleteold'“;

then update 然后更新

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

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

get the id to see if it matches any of the ids submitted 获取ID以查看其是否与任何提交的ID匹配

   $getquoteid= mysqli_fetch_assoc($client_data);   

    $oldquoteid = $getquoteid['quoteID']; 

// for this depends how you are submitting it, i am doing it ajax for ( $for =0; $for < count($data['productName']); $for++) { //因为这取决于您如何提交,所以我正在为($ for = 0; $ for <count($ data ['productName']); $ for ++){
then inside the for lopp we check if the ids match any of the current products 然后在for lopp内部,我们检查ID是否与任何当前产品匹配

I exploded an array i had submitted, easiest way i thought of doing it, then just do a count inside so it can loop 我分解了一个提交的数组,这是我想到的最简单的方法,然后在里面做一个计数,这样它就可以循环

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

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

   } else
 $sql_bid = sprintf("INSERT INTO products

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM