Monday, September 30, 2019

php - Notice: Undefined index




I have a checkbox which can make a post password protected-







My Php tries to post-



 $password = htmlspecialchars(strip_tags($_POST['password']));


I get the undefined index error.




Now, if I try to check first if the password was set, I get the same error executing-



$sql = "INSERT INTO blog (timestamp,title,entry,password) VALUES ('$timestamp','$title','$entry','$password')";

$result = mysql_query($sql) or print("Can't insert into table blog.
" . $sql . "
" . mysql_error());


How do I fix it? Do I have to do it for every field like title text box and all?


Answer




Why are you stripping tags and escaping a boolean value? You could just do it like this:



$password = (isset($_POST['password']) && $_POST['password'] == 1) ? 1 : 0;


or:



$password = isset($_POST['password']) ? (bool) $_POST['password'] : 0;

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...