简体   繁体   中英

Everytime I use a $_ I have to use an @

ex.

$fn = htmlentities(mysqli_real_escape_string($connection, @$_POST['fname']));
$ln = htmlentities(mysqli_real_escape_string($connection, @$_POST['lname']));
$em = htmlentities(mysqli_real_escape_string($connection, @$_POST['email']));
$em2 = htmlentities(mysqli_real_escape_string($connection, @$_POST['email2']));
$pd = htmlentities(mysqli_real_escape_string($connection, @$_POST['password']));
$pd2 = htmlentities(mysqli_real_escape_string($connection, @$_POST['password2']));


Same thing happens with $_SESSION

I get an undefined index error if I take the "@" off...
Any idea why? I don't... all the codes work if I keep them on.

That probably means that you're accessing the array's values without checking they're checked or not...
Either use isset or if ($_POST['fname']) .

Whatever you do, don't lower your error reporting mode (you're probably at E_STRICT | E_ALL ). Supressing errors using the @ is, in my book, bad practice, BTW. The notice is raised internally, so you're slowing down your code, which then looks messy. You're breeding bad habits (turning to the evil @ sign, instead of fixing the problem)...

make sure you check for the existence of a value in the array element using isset() before attempting to access it. The reason you're getting an undefined index is because one of those values from your POST data does not exist.

if (isset($_POST['fname']))
{
    $fn = htmlentities(mysqli_real_escape_string($connection, $_POST['fname']));
}
else
{
    $fn = '';
}

I'm assuming you know that the @ is. If not, its the error suppression operator. Which turns off error reporting for every instance its used and afterward, it turns error reporting back on. You'll only want to use this when you absolutely have to, as it prevents you from seeing things you might want to see.

You can use something like this

foreach (array('fname', 'lname', 'email') as $key) {
   $$key = isset($_POST[$key]) && is_string($_POST[$key]) ? htmlentities(mysqli_real_escape_string($_POST[$key])) : '';
}

to have variables $fname, $lname, $email and so on. Don't just copy and paste.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM