[英]EDIT/UPDATE comma separated values into mysql rows and add new
編輯/更新逗號將值分隔成行並添加新的
我正在嘗試使用不同行的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>» 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 | 產品名稱| 價格| 數量| bid_name |
| 4 | pro_1 | price_1 | quan_1 | bid_1 |
| 4 | pro_2 | 價格_2 | quan_2 | bid_1 |
| 5 | pro_3 | price_3 | quan_3 | bid_2 |
| 5 | pro_4 | price_4 | quan_4 | bid_2 |
您可以拉---檢查---更新---刪除
偽代碼,不起作用! 這是顯示流程:
// 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');
}
可能有更好的方法來執行此操作...這是我前一段時間遇到此問題時所采用的方法。
拉產品
$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'];
//因為這取決於您如何提交,所以我正在為($ for = 0; $ for <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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.