session - isset and empty functions are executing code when they shouldn't be - PHP -
i have come across problem isset
, empty
in php
textarea
box. first test see if $_post
array contains text. if does, stores text in $_session
array index comments
. i've tried hitting submit empty textarea , instead of bypassing conditional statement, executing , putting blank data in database.
heres html:
<p class="center"><textarea placeholder="450 characters max!" rows="10" cols="50" name="message" maxlength="450"></textarea></p>
this executes in script related form:
if (isset($_post['message']) || !empty($_post['message'])) { $_session['comments'] = $_post['message']; }
this part of function in script:
if (isset($_session['comments']) || !empty($_session['comments'])) { $comment = $_session['comments']; // comment_id, user_id, comments $sql = "insert comments (user_id, comments) values ('{$user_id}', '{$comment}')"; $db -> query($sql); }
in theory there shouldn't in $_post
, $_session
when leave textarea
blank, , therefore should not write db
later on in process. have quite few of these statements in code of going behave same way not good. when check db
comments
field empty, created row comments_id
, user_id
.
just check empty. handle isset well:
if (!empty($_post['message'])) { // process }
from php's website: no warning generated if variable not exist. means empty() concise equivalent !isset($var) || $var == false.
http://php.net/manual/en/function.empty.php
thanks,
andrew
Comments
Post a Comment