2

我有两张表,一张用于用户,一张用于搜索。当我将搜索拖到用户时,它会添加整个搜索列表。如何只插入拖动的项目而不是整个搜索数组?

在此处输入图像描述在此处输入图像描述

问题是recordsArray

<?php 
require("db.php");

$action                 = mysql_real_escape_string($_POST['action']);
$updateRecordsArray     = $_POST['recordsArray'];

if ($action == "updateRecordsListings"){

$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue) {

    $query = "UPDATE records1 SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
    mysql_query($query) or die('Error, update query failed');

        //INSERTS array item that does NOT EXIT in userTable
        if (mysql_affected_rows()==0) {
            $query = mysql_query("INSERT INTO records1 SELECT * FROM records WHERE recordID = " . $recordIDValue);
            $query = "UPDATE records1 SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
            mysql_query($query) or die('Error, insert/update query failed');
        }
    $listingCounter = $listingCounter + 1;  
}

echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>


两个列表数组 id 必须保持相同的 id 名称,以便上述项目确认和处理列表项目。为简单起见,我只是对 Search, User 格式使用了相同的代码:

<li id="recordsArray_<?php echo $result['recordID']; ?>"><?php echo $result['recordID'] . ". " . $result['recordText']; ?></li>


Javascript:

$(function() {
    $("#contentLeft ul, #main ul").sortable({ accept: '.draggable', connectWith: "#contentLeft ul",  opacity: 0.6, cursor: 'move', update: function() {
        var order = $(this).sortable("serialize") + '&action=updateRecordsListings'; 
        $.post("updateDB.php", order, function(theResponse){
            $("#contentRight").html(theResponse);
        });                                                              
    }                                 
    });
}).disableSelection();

}); 


我怎样才能只调用recordsArray来自的项目#content ul

4

1 回答 1

0

每次刷新我只能得到一个插入,但它确实拖放插入......

仍然需要一个 if 语句来解决可能的错误,因为 +/-1 if

<?php 
require("db.php");

$action                 = mysql_real_escape_string($_POST['action']); 
$updateRecordsArray     = $_POST['recordsArray'];

$uRA = $updateRecordsArray;

// Get total number of elements in $updateRecordsArray
$ifArray = $updateRecordsArray;
$resultCount = count($ifArray);
//echo  '<br />';
//print_r($resultCount);

// Get total number of rows in $records1
$recordTable = array();
$result = mysql_query("SELECT recordListingID FROM records1");
$numRows = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
    $recordTable = array_merge($recordTable, array_map('trim', explode(",",    $row['recordListingID'])));
}

//Gets $id/$val of elements not in both arrays
$results = array_diff($uRA, $recordTable);
//echo  '<br />';
//print_r($numRows);

//Give variables for +/- 1 $numRows
$plusNumRows = $numRows + 1;
$minusNumRows = $numRows - 1;
//echo  '<br />';
//print_r($minusNumRows);

// Normal jQuery drag and drop ranking found online
if ($action == "updateRecordsListings"){

$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue) {

//If statement for for +/- 1 $numRows
If ($resultCount == $numRows -1 || $resultCount == $numRows || $resultCount == $numRows + 1){
$sql = mysql_query("INSERT INTO records1 SELECT * FROM records WHERE recordID = $recordIDValue");
}

    $query = "UPDATE records1 SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
            mysql_query($query) or die('Error, update query failed');

    $listingCounter = $listingCounter + 1;
}


echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>
于 2013-09-16T00:51:28.003 回答