Jump to content

short hand variable loading


Q695
Go to solution Solved by Jacques1,

Recommended Posts

Someone showed me a while ago this trick for loading variables, but it creates an error when you go to the page initially:

$fname = $_POST['fname'] ? $_POST['fname'] : null;

How do I write the short hand to run correctly?

Link to comment
Share on other sites

  • Solution

You need to check if the parameter even exists:

$fname = isset($_POST['fname']) ? $_POST['fname'] : null;

However, this whole practice of putting external input into separate variables doesn't make a lot of sense. You already have a variable with the value: $_POST['fname']. What's the point of creating this extra $fname variable? Only to save 10 characters when writing it down? That's not really a valid reason in the times of auto-completion.

Link to comment
Share on other sites

Someone points their form without data required for the PDO to the page the form is on.  When it's not set, it automatically sets the value to null.

 

Sorry about the redundant posting, it was a slow server that I'll blame ;) ;)

Link to comment
Share on other sites

Someone points their form without data required for the PDO to the page the form is on.  When it's not set, it automatically sets the value to null.

 

This doesn't require an extra variable. Just set the value to null when needed, e. g.,

$stmt = $database->prepare('
    ... some query ...
');
$stmt->execute(array(
    'fname' => isset($_POST['fname']) ? $_POST['fname'] : null
));
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.