首页 > 解决方案 > How to disable button based on sql query?

问题描述

I need to disable or hide, button if existsusername in the table, and == logged in user

For example: username John exists in table paym we should disable button to John

table: paym

 ID        username        column1      column2  
+-------+-------------+-------------+-----------+
|   1   |  John       |  Value      |    Value  |
+-------+-------------+-------------+-----------+
|   2   |  Alex       |  Null       |    Null   |
+-------+-------------+-------------+-----------+

May be some syntax error or error in Query??

<?php
  $sql = "SELECT * FROM paym WHERE username = '" . $_SESSION['username'] . "'and column1 IS NOT NULL ";
  $result = mysqli_query($connection, $sql);
       if($result === true) {
        echo "<button class='btn btn-primary' disabled='disabled' name='add_box'>Add Box</button>";
    } else {
        echo "<button class='btn btn-primary' type='submit' name='add_box'>Add Box</button>";
    } 
  ?>

标签: phphtmlmysql

解决方案


您应该检查查询匹配的行数,因此您的条件应该是

if($result !== false && $result->num_rows > 0)

因为$result只有false在出现错误时。

如果您的查询是

SELECT * FROM paym WHERE username = 'not existing user' and column1 IS NOT NULL

mysqli_query将返回mysqli_resultnot false,即使没有匹配的行。


推荐阅读