我编写了一个在线调查的代码,其中有一些文本(表单)字段,用户可以在其中输入数据。我的困难在于,在需要数字的表单字段中,如果用户未能输入数字,则默认的“0”会一直出现在MySQL数据库的列中。我怎样才能防止这种情况发生?我更喜欢的是,如果用户忘记或只是不想输入他的年龄或高度,MySQL 中的年龄和高度列将保持为空。我可以使用以下代码(我从 YouTube 获得并简单更改字段名称)来实现这个愿望吗?:
<?php
mysqli_connect('localhost','root','pswd','database');
if (isset($_POST['age']) && isset($_POST['heightininches'])) {
$age = 'age';
$heightininches = 'heightininches';
if(!empty($age) && !empty($heightininches)) {
$age = 'age';
$heightininches = 'heightininches';
} else {
$age = ''; // will this line ensure that column will not display "0"
$heightininches = '';
}
}
<form action = "pagoda.php" method = "POST">
Age: <input type = "text" name = "age">
<input type = "text" name = "heightininches">
<input type = "text" value = "Submit">
</form>
我还有两个问题:鉴于上面的代码有“&&”,这是否意味着如果我不在年龄字段或 heightininches 字段中输入任何数据,那么数据库中的这些列都不会被人口稠密? '&&' 是否将两个字段连接在一起?或者他们仍然分开吗? 我的另一个问题是:'isset'函数有一个看起来像'$_POST['age']'的参数,但是'empty'函数有一个看起来像'$age'的参数。为什么?为什么 'empty' 没有像 '$_POST['age']' 这样的参数?
最佳答案
在数据库中,配置字段age接受null,默认值为null,就是这样的。
关于php - 如果用户输入数据失败,如何确保MySQL中的列不显示默认的 "0"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27552228/