简体   繁体   English

PHP:仅当数据库值等于 X 时才显示表单

[英]PHP: Display a form only if database value equals X

I have added a form into an admin panel with a yes (1) and no (0) option.我在管理面板中添加了一个带有是 (1) 和否 (0) 选项的表单。

I have changed the Db so that on submit, the value gets added to the Db with the value "seller" 1 or 0, which works fine.我已经更改了 Db,以便在提交时,该值被添加到 Db 中,值为“seller”1 或 0,这可以正常工作。

I have a form on my site.我的网站上有一个表格。 I only want it to display to a logged in person if they have a value in the Db of "1" for seller.如果卖家的 Db 值为“1”,我只希望它显示给登录的人。 If they have a value of 0 I want to print a message.如果它们的值为 0,我想打印一条消息。

I am guessing I need to query the database, then construct a function and apply it in the page where the form code is to display it if the condition of "1" is met?我猜我需要查询数据库,然后构造一个函数并将其应用到表单代码所在的页面中,如果满足“1”的条件,则显示它?

I have tried searching but cannot grasp how I can construct it.我尝试过搜索,但无法掌握如何构建它。

<?php
// insert your DB Data
$user = "";
$pass = "";
$host = "";
$dbdb = "";

$connect = mysqli_connect($host, $user, $pass, $dbdb);
if(!$connect)
{
    trigger_error('Error connection to database: '.mysqli_connect_error());
}

mysqli_set_charset($connect, 'utf8'); 

$query = mysqli_query($connect, "SELECT `seller` FROM `nameTable` WHERE `user` = 'userName'");
$record = mysqli_fetch_array($query);

if($record['seller'] == 1){
    echo "Seller is 1";
}else{
    echo "Seller is 0";
}
?>

I am using if else condition for my solution here:我在这里使用if else 条件作为我的解决方案:

 <?php

    $userLoggedIn = $_SESSION['userName'];//session created while login is stored in variable

    //Search table `tbl` for field `SELLER` where field `user` matches with loggedin user

    $searchSeller = mysql_query("SELECT `SELLER` from `tbl` WHERE `user`='$userLoggedIn'");
    $searchSellerArray = mysql_fetch_array($searchSeller);

    //Setting variable to be used in css depending on value in table

    if($searchSellerArray['seller']==1){

    $displayForm="block";//displays form
    $displayMessage="none";//hides message

    }
    elseif ($searchSellerArray['seller']==0){

    $displayForm="none";//hides form
    $displayMessage="block";//displays message

    }
    ?>

    <form style="display:<?php echo $displayForm;?>;">
    //Form content goes here...
    </form>
    <p style="display:<?php echo $displayMessage;?>;">
    //Message content goes here
    </p>

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

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