伊塞特();不返回 false 并将空白字段插入数据库
我正在网上做一个 php 教程,一切进展顺利,直到使用表单条件中的 isset 向数据库插入一些内容为止。好吧,让我们来看看代码:
这是插入 DB 函数(它在functions.php 中)
function addCat($cName, $cDesc){
$query = mysql_query("INSERT INTO categories (Title, Description) VALUES ('$cName','$cDesc')") or die (mysql_error());
}
这是表单:
<form action="doAdd.php" method="post">
<table>
<tr>
<td><label for="CatName">Name</label></td>
<td><input type="text" name="CatName" /></td>
</tr>
<tr>
<td><label for="CatDesc">Description</label></td>
<td><input type="text" name="CatDesc" /></td>
</tr>
<tr><td colspan="2"><input type="submit" value="submit" name="submit"/></td></tr>
</table>
</form>
这是表单的操作(在 doAdd.php 中):
<?php
include('includes/functions.php');
if(isset($_POST['submit'])) {
if(isset($_POST['CatName'])){
addCat($_POST['CatName'],$_POST['CatDesc']);
header("Location: cats.php");
} else {
echo "please set a category name!";
}
} else {
header("Location: addCat.php");
}
?>
所以关于表单的事情是它插入空白字段,使得数据库内的空白记录。您认为 isset 的问题是什么?
I'm doing one of php tutorials in the web, and it was going all right till moment of inserting something to database with isset in forms conditionals. Well let's get to the code:
This is the insert into DB function (it's inside functions.php)
function addCat($cName, $cDesc){
$query = mysql_query("INSERT INTO categories (Title, Description) VALUES ('$cName','$cDesc')") or die (mysql_error());
}
Here's the form:
<form action="doAdd.php" method="post">
<table>
<tr>
<td><label for="CatName">Name</label></td>
<td><input type="text" name="CatName" /></td>
</tr>
<tr>
<td><label for="CatDesc">Description</label></td>
<td><input type="text" name="CatDesc" /></td>
</tr>
<tr><td colspan="2"><input type="submit" value="submit" name="submit"/></td></tr>
</table>
</form>
And here's form's action (in doAdd.php):
<?php
include('includes/functions.php');
if(isset($_POST['submit'])) {
if(isset($_POST['CatName'])){
addCat($_POST['CatName'],$_POST['CatDesc']);
header("Location: cats.php");
} else {
echo "please set a category name!";
}
} else {
header("Location: addCat.php");
}
?>
So the thing about the form is that it inserts blank fields, making blank records inside database. What do you think is the problem with isset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用
empty()
而不是isset()
,如下所示:empty
测试 var 是否已设置且不为空(即不是''
以及明显的其他值,如 0、false 等)。You should use
empty()
instead ofisset()
in this way:empty
test if the var is set and is not empty (that is is not''
and obvioulsy other value like 0, false and so on).isset
只是检查变量是否存在。如果你想测试它是否不为空,你应该使用!empty
来代替。isset
just checks for the existence of a variable. If you want to test if it's not blank, you should use!empty
instead.isset()
检查变量的值是否包括(False、0 或空字符串),但不包括NULL
。如果 var 存在则返回 TRUE;否则为假。另一方面,
empty()
函数检查变量是否具有空值empty string、0、NULL 或False。如果 var 具有非空且非零值,则返回 FALSE。希望这有帮助!
isset()
checks if a variable has a value including (False , 0 , or Empty string), but notNULL
. Returns TRUE if var exists; FALSE otherwise.On the other hand the
empty()
function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.Hope this helped !