我在这里将 mysql 记录显示在带有删除按钮的 html 表中。我需要做的是如果两个数据库表中都存在记录,则禁用删除按钮。
如果两个表中都已存在记录,如何禁用每行的删除按钮?任何帮助将不胜感激。
$search = $mysqli1->real_escape_string($_POST['bid']);
$search = preg_replace("/[^A-Za-z0-9 ]/", '', $search);
$search = $_POST['bid'];
$res = $mysqli1->query("select * from code WHERE item LIKE '%$search%' OR item_code LIKE '%$search%' OR cat_code LIKE '%$search%' order by item_code ASC");
while($r = $res->fetch_assoc()){
echo "<tr>
<td><a href='#' id='".$r['id']."' class='del'><img src='../images/del.png'></a></td>
</tr>";
}
最佳答案
抛出一个简单的 if() 语句 与两个查询相关 在 pdo 中,您使用 ->rowCount() 不确定在 mysqli 中
所以你需要这个逻辑
query1 = 表 1 中的行数
query2 = table2 中的行数
就好像您说如果它存在于两个表中,则应该隐藏它,以便您将使用 if-or 语句
if(query1 == 0 || query2 == 0){
//show your button
}
这里的意思很简单:
if(query1 equals 0 rows OR query2 equals 0 rows){
//show your button
}
//While you don't put up the else with something else it won't show anything
//so if there are the value of 1+ rows in both query1 and query2 this won't show anything
如果你想让我提供一个 pdo 示例,只需回复
编辑:
PDO 类使连接更容易
class Database extends PDO
{
private $db;
public function Database($host, $user, $pass, $db) {
try {
$this->db = new PDO("mysql:dbname=".$db.";host=".$host.";", $user, $pass);
} catch(PDOEXCEPTION $e) {
die('An error has occurred! [Code: '.$e->getCode().']! <br/>More info: ['.$e->getMessage().']!');
}
}
public function runQuery($query) {
try{
return $this->db->query($query);
} catch(PDOEXCEPTION $e) {
die('An error has occurred! [Code: '.$e->getCode().']!<br/>More info: ['.$e->getMessage().']!');
}
}
}
现在行计数:已更新 && 和 2x query1 检查出现了一些错误
$consite = new Database('DBHost','DBUsername','DBPassword','DBName');
$query1 = $consite->runQuery("SELECT * FROM TABLE1");
$query2 = $consite->runQuery("SELECT * FROM TABLE2");
if($query1->rowCount() == 0 || $query2->rowCount() == 0) {
//do your while statement to loop through it
//if you done your while statement it only shows the delete button
//for items that are NOT in both tables
}
抱歉我懒得添加 while 语句;) 如果我是正确的,您可以在 sql 查询中检查多个表,这样如果您在 1 个查询之后进行多重检查,则可以使用 1 个查询来完成此操作!
编辑: 此问题的逻辑步骤:
1) 连接
到数据库
2) 对table1
进行查询
3) 计数
table1
中的条目
(记录
)
4) 对table2
进行查询
5) 计数
table2
中的条目
(记录
)
6) 检查
其中其中一个
是否等于0
7) 如果其中其中一个
等于0
条目
(记录
(行
)) 然后显示按钮
关于php - 如果多个表中存在记录,则禁用删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22902701/