Is it possible to verify that two variables are defined in an easier way?

Asked

Viewed 109 times

-3

I receive by the form via POST the fields sd and video, need to check if both are set. I currently do the following:

<?php

if (isset($_POST['sd'])) {

    $sd = $_POST['sd'];

    if (isset($_POST['video'])) {

        // ...

    }

}

You can do this check more simply?

  • Welcome to the Stackoverflow in Portuguese, to make good use of the site, please do the [tour], to learn more about it, go to [help].

1 answer

2


The isset accepts several parameters, thus:

if(isset($_POST['sd'], $_POST['video'])){

    $sd = $_POST['sd'];

    $vid = $_POST['video'];


    $sql = "UPDATE `player` SET `sd` = '$sd', `video` = '$vid' WHERE `id` = 3";
    $ssl = mysqli_query($conn, $sql);

    $_SESSION['post'] = 'Configuração salva com sucesso!';
    header("Location: edit.php");

    exit();

}

For the record, I recommend you use mysqli_real_escape_string to avoid Sqlinjection, thus:

if(isset($_POST['sd'], $_POST['video'])){

    $sd = mysqli_real_escape_string($_POST['sd']);

    $vid = mysqli_real_escape_string($_POST['video']);


    $sql = "UPDATE `player` SET `sd` = '$sd', `video` = '$vid' WHERE `id` = 3";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.