如果多个表中存在记录,则禁用删除按钮

时间:2014-04-07 02:34:24

标签: php mysql sql

我这里的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>";
}

1 个答案:

答案 0 :(得分:0)

抛出一个简单的if()语句 与两个查询相关联 在pdo中你使用 - &gt; rowCount() 在mysqli中不确定

所以你需要这个逻辑

query1 =计算table1中的行

query2 =计算table2中的行

好像你说两个表中是否存在它应该隐藏它所以你将使用if-or statement

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,使连接更容易

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().']!');
        }       
    }
}

现在行计数:已更新&amp;&amp;&amp;和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)Connectdatabase

2)为query

制作table1

3)Count来自entries的{​​{1}}(records}

4)为table1

制作query

5)table2来自Count的{​​{1}}(entries}

6)records如果table2等于Check

7)如果one等于0 one0entries)),则显示按钮