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?
You should use
empty()instead ofisset()in this way:emptytest if the var is set and is not empty (that is is not''and obvioulsy other value like 0, false and so on).