如何在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> </p>
</div>
答案 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()