php - EDIT/UPDATE comma separated values into mysql rows and add new

138

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[]

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

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

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]

product 2 [product 2 row] [price 2] [quanity 2] [total 2]

then with a form add new products


SELECT OLD PRODUCTS AND UPDATE THEN GET ID AND ADD NEW PRODUCTS AND LINK TO TABLE

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

             {-code-2}

| quoteID | bid_name | status | total |

|

707

Answer

|

| 1 | bid1 | pending | 100 |

| 2 | bid2 | stuff_y | 200 |

              {-code-3}

| quoteID | productID |
|

172

Answer

|

| 1 | 4 |

| 1 | 4 |

| 2 | 5 |

| 2 | 5 |

              quote_products_link TABLE 3

| productID | product_name | price | quanity | 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 |

438

Answer

Solution:

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.

802

Answer

Solution:

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

  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' ";

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

   $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++) {
then inside the for lopp we check if the ids match any of the current products

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

 }

People are also looking for solutions to the problem: php - Mysqli to PDO conversion

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.