Jquery / PHP if语句拖放更新数组并插入MySQL表?

时间:2013-09-11 04:41:07

标签: php javascript jquery mysql

如何在update.php脚本之前插入if语句?
if语句将验证列表中的jquery已删除项是否在列出Mysql数据库中,并且如果不是,它会将其插入列表/表数据库,然后运行update.php


update.php

require("db.php");

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

if ($action == "updateRecordsListings"){

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

///////currently stuck//////////
    if (???ID not present in lists database???){
    $sql = "INSERT INTO records1 SELECT * FROM records WHERE ID = (???missing array element ID???)
///////////////////////////////

        $query = "UPDATE records1 SET recordListingID = " . $listingCounter . " WHERE ID = " . $IDValue;
        mysql_query($query) or die('Error, insert query failed');
        $listingCounter = $listingCounter + 1;  
}else{
       $query = "UPDATE records1 SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
        mysql_query($query) or die('Error, insert query failed');
        $listingCounter = $listingCounter + 1;  
    }

if语句需要引用$ updateRecordsArray并验证列表MySQL表中是否存在数组中的所有ID。如果某个项目没有,则插入并运行update else run update。

未经更改的工作update.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, insert 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.';
}

使用Javascript:

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

HTML:

    <div id="contentleft">
    <ul></UL>
    </div>

        <div id="contentRight">
      <p>Array will be displayed here.</p>
      <p>&nbsp; </p>
    </div>

2 个答案:

答案 0 :(得分:0)

if (!SELECT 1 from records WHERE ID = $IDValue)

以上行将告诉您记录是否在records表中可用。然后,如果满足此条件,您可以插入记录。

答案 1 :(得分:0)

我遇到的主要问题是创建一个我必须学习的论据 PHP Operates才能解决这个问题......

/////////  **PLUS OR MINUS 1 /OR/ NO CHANGE**   ///////////
If ($resultCount == $numRows -1 || $resultCount == $numRows || $resultCount == $numRows + 1){
        $sql = mysql_query("INSERT IGNORE INTO records1 SELECT * FROM records WHERE recordID = $recordIDValue");
    }

由于我只需要当前数字/加或减1 ,我唯一需要的是从数据库表中获取当前行数。由于我在此之前就已经在$numRows

进行了查询
$result = mysql_query("SELECT recordListingID FROM records1");
$numRows = mysql_num_rows($result);

$resultCount语句是if语句的另一部分:

$updateRecordsArray     = $_POST['recordsArray'];
$ifArray = $updateRecordsArray;
$resultCount = count($ifArray);

我在count()清空$updateRecordsArray时出现问题,因此我将其与$ifArray一起复制,然后执行了count()