如何在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> </p>
</div>