0

如何在update.php脚本前插入 if 语句?
if 语句将验证 jquery 在 list 中删除的项目是否在该列表 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 并验证数组中的所有 ID 都存在于列表 MySQL 表中。如果一个项目不是然后插入并运行更新,否则运行更新。

未更改的工作 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>
4

2 回答 2

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()

于 2013-09-18T05:08:08.273 回答
0
if (!SELECT 1 from records WHERE ID = $IDValue)

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

于 2013-09-11T05:21:53.223 回答