php - 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>";
}
?>
解决方案
您应该检查查询匹配的行数,因此您的条件应该是
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_result
not false
,即使没有匹配的行。
推荐阅读
- javascript - 我收到此错误:未捕获的 ReferenceError: $ is not defined at HTMLInputElement.onkeyup
- java - 在 Android 中的图像上添加圆圈
- ruby - 如何在 Ruby 中将两个 int 变量相乘
- python-3.x - Privnote 套接字服务器不理解请求
- python - 使用请求将图像发布到 Firebase
- java - 依赖于某些条件/属性名称等的 JsonView 序列化
- javascript - 数据表 - 加载时显示最后一页
- java - 没有足够的堆内存来运行 weka 3.8.4
- javascript - 当我尝试在端口 5500 上启动 localhost 时,Google Chrome 无法连接
- ruby-on-rails - Rails 订单搜索结果