Monday 30 September 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...