拖放数组NOT EXIST然后插入MySQL

时间:2013-09-11 18:12:29

标签: php mysql arrays events

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

enter image description here enter image description here

问题是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名称,以便上面的项目确认并处理列表项。保持简单我只是使用相同的代码搜索,用户格式:

<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项?

1 个答案:

答案 0 :(得分: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.';
}
?>