简体   繁体   English

如果在MySql中退出,则替换为另一列

[英]Replace to another column if exits in MySql

I have a HTML table having values from Mysql. 我有一个具有Mysql值的HTML表。 My HTML table looks like this 我的HTML表格如下所示

-------   ------   ------   ------
 type      name     b_id     Edit
-------   ------   ------   ------
Laptop     mac      E1:23    edit
Desktop    dell     D2:45    edit

I am inserting values from input textboxes for type and name. 我正在从输入文本框中插入类型和名称的值。 And a select dropdown for b_id. 并为b_id选择下拉菜单。 I can edit each row values by clicking edit and i have a dropdown displaying all my b_id's . 我可以通过单击“ edit来编辑每行的值,并且有一个下拉列表显示所有b_id's

I need to update b_id values so that, if it already exists replace with new delete the old. 我需要更新b_id值,以便如果它已经存在,请用新的删除旧的替换。

For suppose i'l hit edit on Desktop dell row. 假设我要点击“ 桌面” dell行上的“ edit”。 And i'l select E1:23(which is already mapped for laptop mac ). 然后我选择E1:23(已经为笔记本电脑mac映射了)。 On saving this, E1:23 should map to desktop dell and previous mapping (laptop mac) should be deleted . 保存时, E1:23应该映射到桌面Dell,并且应该删除以前的映射(laptop mac)。 I mean it should be empty 我的意思是应该是空的

My Query 我的查询

$id = $_REQUEST['id']; // Auto Increment
$b_id = $_REQUEST['b_id']; //b_id from select dropdown on edit
$sql = "SELECT * FROM table_name WHERE b_id='$b_id'";
$rs=parent::_executeQuery($sql);
$rs=parent::getAll($rs);
if($rs) // if b_id already exists
{
    //First empty old b_id which has mapped already
    $sql2 = "UPDATE table_name SET b_id='$b_id' WHERE id='$id'";
    $rs=parent::_executeQuery($sql);
}

So how to empty previous mapped b_id. 那么如何清空以前映射的b_id。 Not the type and name 不是类型和名称

If you want to empty the data in b_id, your query should be something like this: 如果要清空b_id中的数据,则查询应如下所示:

if($rs) // if b_id already exists
{
    //First empty old b_id which has mapped already
    $sql2 = "UPDATE table_name SET b_id='' WHERE b_id='$b_id'";
    $rs=parent::_executeQuery($sql2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM