gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:19:16 25 4
gpt4 key购买 nike

如何在 update.php 脚本之前插入 if 语句?
if 语句将验证 jquery 删除列表中的项目是否在该列表中列出Mysql数据库,如果不是,则将其插入到lists/tables数据库中,然后运行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>

最佳答案

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

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

关于php - Jquery/PHP if 语句拖放更新数组并插入到 MySQL 表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18732827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com