eiistrir
eiistrir

Reputation: 27

cannot display 0 value in an html textbox

I'm displaying data from mysql using php. You can see the value is 0 for Current Balance But in edit mode it cant display 0

enter image description here

Before executing update statement

I declare its variable from its corresponding $_POST variable

if (!empty($_POST)) {

    $ID = $_POST['ID'];
    $Name = $_POST['Name'];
    $AID = $_POST['AID'];
    $CurrentBalance = $_POST['CurrentBalance'];

    $valid = true;

    if ($valid) {
        $setsu = dbSetsuzoku();
        $setsu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "UPDATE tabledb SET ID = ?, Name = ?, AID=?, CurrentBalance=? WHERE SubAgentID = ?";
        $q = $setsu->prepare($sql);
        $q->execute(array($ID,$Name,$AID,$CurrentBalance,$ID));
        $setsu = null;
    }
} 
 else {
    $setsu = dbSetsuzoku();
    $setsu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM tabledb where ID = ?";
    $q = $setsu->prepare($sql);
    $q->execute(array($ID));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    $ID = $data['ID'];
    $Name = $data['Name'];
    $AID = $data['AID'];
    $CurrentBalance = $data['CurrentBalance'];
    $setsu = null;
}

html (edit mode)

<input name="CurrentBalance" type="text" placeholder="CurrentBalance" value="<?php echo !empty($CurrentBalance)?$CurrentBalance:'';?>" required />

other fields follow the same logic

Upvotes: 0

Views: 665

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

The problem is :

<?php echo !empty($CurrentBalance)?$CurrentBalance:'';?>

For empty function 0 is considered as empty - just look at https://www.php.net/manual/en/function.empty.php

You can change it into:

<?php echo isset($CurrentBalance)?$CurrentBalance:'';?>

Upvotes: 1

Andras Toth
Andras Toth

Reputation: 628

If you check the documentation of empty() function, you'll see that it will give back true for 0. Because of this you just display an empty string here:

<input name="CurrentBalance" type="text" placeholder="CurrentBalance" value="<?php echo !empty($CurrentBalance)?$CurrentBalance:'';?>" required />

Simple solution is to change the empty stirng to 0.

Upvotes: 0

Related Questions