Jump to content

How to check the db and hide/show the button?


kyme

Recommended Posts

Hi,

 

I was just playing php and try to check if the database is empty.

I want to check the DB if it's empty. And if it's not, the button will hide for "Insertion btn". 

I made some basic understanding of the structure of the code. 

I don't have a clue of how it will hides/shows the button once the database was checked if it's empty or not.

Some comment says that I should use JS to hide and show the button. But I don't have a clue if how to intergrate the btn hide() and show().  

What statement should I add to enable to check the db and hide/show the btn? 

<!DOCTYPE html>
<html>
<body>

<form action="mypractice.php" method="post" enctype="multipart/form-data">
Message: <textarea name='message' rows='3' cols='10'> </textarea><br><br>
<button id="insert" type="submit" value="insert sample" name="submit">insert sample</button>
</form>


<?php
//My DB connection
require_once('config.php');

//Checks if DB is empty
if(empty($conn))
{
shows the button if db is empty
}
else {
hides the button if db is not empty
}
?>

</body>
</html>
Link to comment
Share on other sites

 

Hi,

 

I was just playing php and try to check if the database is empty.

I want to check the DB if it's empty. And if it's not, the button will hide for "Insertion btn". 

I made some basic understanding of the structure of the code. 

I don't have a clue of how it will hides/shows the button once the database was checked if it's empty or not.

Some comment says that I should use JS to hide and show the button. But I don't have a clue if how to intergrate the btn hide() and show().  

What statement should I add to enable to check the db and hide/show the btn? 

<!DOCTYPE html>
<html>
<body>

<form action="mypractice.php" method="post" enctype="multipart/form-data">
Message: <textarea name='message' rows='3' cols='10'> </textarea><br><br>
<button id="insert" type="submit" value="insert sample" name="submit">insert sample</button>
</form>


<?php
//My DB connection
require_once('config.php');

//Checks if DB is empty
if(empty($conn))
{
shows the button if db is empty
}
else {
hides the button if db is not empty
}
?>

</body>
</html>

 

Hey,

 

What do you mean if the db is empty? You mean empty of tables? Or do you mean if a specific table is empty?

 

The code if(empty($conn)) - I am assuming $conn is a resource of a database connection right? Checking if that variable is empty isn't going to tell you if the database is empty of tables or anything like that, you would need to run a mysql query to check for tables. 

 

A query like this would do the trick:

SELECT COUNT(*) AS TableCount FROM information_schema.tables WHERE table_schema = 'dbo' and TABLE_TYPE='BASE TABLE'

So you would run this query, fetch the row and check TableCount > 0 

Link to comment
Share on other sites

Hey,

 

What do you mean if the db is empty? You mean empty of tables? Or do you mean if a specific table is empty?

 

The code if(empty($conn)) - I am assuming $conn is a resource of a database connection right? Checking if that variable is empty isn't going to tell you if the database is empty of tables or anything like that, you would need to run a mysql query to check for tables. 

 

A query like this would do the trick:

SELECT COUNT(*) AS TableCount FROM information_schema.tables WHERE table_schema = 'dbo' and TABLE_TYPE='BASE TABLE'

So you would run this query, fetch the row and check TableCount > 0 

 

 

When the table is empty. It will check the table and if it's empty the btn for insertion is visible.

Link to comment
Share on other sites

When the table is empty. It will check the table and if it's empty the btn for insertion is visible.

 

Ok, so if you need to check a specific table is empty you can just run  simple mysql query, for example:

SELECT COUNT(*) AS TotalRows FROM `table`

It's difficult to say exactly how to modify your code as i can't see the full extent of it, but i would do this using PDO like this:

$query = $dbOBJ->prepare("SELECT COUNT(*) AS TotalRows FROM `table`");
$query->execute();

if($query->fetch(PDO::FETCH_OBJ)->TotalRows >0){

}
Link to comment
Share on other sites

 

Ok, so if you need to check a specific table is empty you can just run  simple mysql query, for example:

SELECT COUNT(*) AS TotalRows FROM `table`

It's difficult to say exactly how to modify your code as i can't see the full extent of it, but i would do this using PDO like this:

$query = $dbOBJ->prepare("SELECT COUNT(*) AS TotalRows FROM `table`");
$query->execute();

if($query->fetch(PDO::FETCH_OBJ)->TotalRows >0){

}

 

 

So I did this but this is my structure.

$query = $conn->prepare("SELECT COUNT(*) AS TotalRows FROM `myTableName`");
$query->execute();

    if($query->fetch(PDO::FETCH_OBJ)->TotalRows >0){
      echo Btn insert shows
    }
      else{
        echo btn insert hides  
      }

So basically what I want to do is to check the table if it's empty or not.

If empty, it shows the button to insert.

And if it's not empty, the button(insert) is not visible.

Link to comment
Share on other sites

And why would you want this?

 

Only allowing a single row is a rather strange goal. It's also unclear how exactly you want to enforce that. If there are multiple users or even just one user with multiple tabs/windows/browsers, you'll quickly run into a scenario where the page is initially loaded with no rows present (i. e. the button is visible), but then a row does get inserted outside of the current page. With your code, the insertion button is still visible and probably functional as well.

 

If there is no meaning behind this all, and you're just practicing, then you might want to choose a more realistic task.

Link to comment
Share on other sites

So I did this but this is my structure.

$query = $conn->prepare("SELECT COUNT(*) AS TotalRows FROM `myTableName`");
$query->execute();

    if($query->fetch(PDO::FETCH_OBJ)->TotalRows >0){
      echo Btn insert shows
    }
      else{
        echo btn insert hides  
      }

So basically what I want to do is to check the table if it's empty or not.

If empty, it shows the button to insert.

And if it's not empty, the button(insert) is not visible.

 

This depends on what you're using to connect to the database, is $conn a PDO object or a mysql connection resource? The code I used was just an example i would use with PDO

Link to comment
Share on other sites

And why would you want this?

 

@Jaques1 and I both asked the obvious question, why, yet the OP hasn't answered. I believe there is no real and logical reason to do this. Unless the OP shows otherwise, I would file this as an XY problem or worse. It just makes no sense.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.