我使用php显示来自mysql的数据。 您可以看到当前余额的值为0 但在编辑模式下,它无法显示0
执行更新声明之前
我从相应的$ _POST变量
声明其变量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(编辑模式)
<input name="CurrentBalance" type="text" placeholder="CurrentBalance" value="<?php echo !empty($CurrentBalance)?$CurrentBalance:'';?>" required />
其他字段遵循相同的逻辑
答案 0 :(得分:1)
问题是:
<?php echo !empty($CurrentBalance)?$CurrentBalance:'';?>
对于空函数0被视为空 - 只需查看http://au2.php.net/manual/en/function.empty.php
您可以将其更改为:
<?php echo isset($CurrentBalance)?$CurrentBalance:'';?>
答案 1 :(得分:0)
如果你查看empty()函数的文档,你会看到它会为0返回true。因此你只需在这里显示一个空字符串:
<input name="CurrentBalance" type="text" placeholder="CurrentBalance" value="<?php echo !empty($CurrentBalance)?$CurrentBalance:'';?>" required />
简单的解决方法是将空的搅拌变为0。