How to force my variable in php?

Asked

Viewed 261 times

0

if ($stmtMoreInformations->execute()) {

    $stmtUpdateMoreInformations = $conn->prepare("UPDATE menu SET only_delivery = :delivery, card_on_delivery = :delivery,
                                                 wifi = :wifi, live_music = :music, open_holiday = :holiday, 
                                                 acessible = :acessible
                                                 WHERE menu_id = :menu");

    $stmtUpdateMoreInformations->bindValue(":menu", $menu);
}

I have this update and my variables are returning 1 or 2, I want to force them to return true or false, can I use the cast? How could I use it?

$menu = $_POST['menu'];
    $delivery = null;
    if (isset($_POST['delivery'])) {
        $delivery = $_POST['delivery'];
    }

    $cards = null;
    if (isset($_POST['cards'])) {
        $cards = $_POST['cards'];
    }
    $wifi = null;
    if (isset($_POST['wifi'])) {
        $wifi = $_POST['wifi'];
    }
    $music = null;
    if (isset($_POST['music'])) {
        $music = $_POST['music'];
    }
    $holiday = null;
    if (isset($_POST['holiday'])) {
        $holiday = $_POST['holiday'];
    }
    $acessible = null;
    if (isset($_POST['acessible'])) {
        $acessible = $_POST['acessible'];
    }







$stmtMoreInformations = $conn->prepare("SELECT only_delivery, card_on_delivery, wifi, live_music, 
                                        open_holiday, acessible FROM public.menu
                                        WHERE menu_id = :menu");

$stmtMoreInformations->bindValue(":menu", $menu);



if ($stmtMoreInformations->execute()) {

    $stmtUpdateMoreInformations = $conn->prepare("UPDATE menu SET only_delivery = :delivery, card_on_delivery = :card,
                                                 wifi = :wifi, live_music = :music, open_holiday = :holiday, 
                                                 acessible = :acessible
                                                 WHERE menu_id = :menu");

    $stmtUpdateMoreInformations->bindValue(':menu', $menu);
    $stmtUpdateMoreInformations->bindValue(':delivery', $delivery);
    $stmtUpdateMoreInformations->bindValue(':card', $cards);
    $stmtUpdateMoreInformations->bindValue(':wifi', $wifi);
    $stmtUpdateMoreInformations->bindValue(':music', $music);
    $stmtUpdateMoreInformations->bindValue(':holiday', $holiday);
    $stmtUpdateMoreInformations->bindValue(':acessible', $acessible);
    $stmtUpdateMoreInformations->execute();

    echo'delivery: '.$delivery;
    echo'card: '.$cards;
    echo'wifi: '.$wifi;
    echo'music: '.$music;
    echo'd: '.$holiday;
}
  • If you put a snippet of html with radio, it can be easier to adjust the answer to the specific case (and help people who can post new answers or alternatives).

  • 1

    If you want to adapt to your question on the same subject (which you deleted a little), just use if( isset($_POST['cards'] ) && ($_POST['cards'] =='1') ) { etc to use white or empty for false, 1 and for true.

  • Thanks @Bacco but I haven’t solved my real problem yet.

  • If you post a question with HTML code and more PHP we can try to help. I would suggest you stop insisting on the cast/bool because this is definitely not the problem. PHP doesn’t have this type of typing problem. When asking new question, put the two parts, explain what is happening that we try to help.

  • This problem in question I’ve already solved, I’m making another comparison now. Between what’s coming from the bank and what’s coming from my radio input. But thank you.

  • 1

    Okay, but the tip is for any post you post. Always put the snippet of updated code, SQL in the database part, etc. - And on recovering radios from DB, if you want to use the same option. if( $valordodb = 'seu_verdadeiro') $s1=' selected' else $s2=' selected', then you put in html <option value ..... <?=$s1?>> at the end of the true and $s2 at the false (Selected is for option, checked for checkbox) but the logic is the same.

  • Okay, thank you so much for the tip!

Show 2 more comments

1 answer

2


On the conversion to bool:

There are several ways, the ideal is to cast at the time of use and not return.

Some possibilities:

$boolMenu = ( $menu == 2 ); // só é true quando for 2

$boolMenu = (boolean) ( $menu - 1 );  // alterna entre zero (false) e 1 (true)

Applying to your POST, you can make it even simpler:

$cards     = isset($_POST['cards']    ) && ($_POST['cards']    =='2');
$wifi      = isset($_POST['wifi']     ) && ($_POST['wifi']     =='2');
$music     = isset($_POST['music']    ) && ($_POST['music']    =='2');
$holidays  = isset($_POST['holydays'] ) && ($_POST['holydays'] =='2');
$acessible = isset($_POST['acessible']) && ($_POST['acessible']=='2');

These lines above override all your question code. No longer need initialization nor cast, or anything.

With 'Yes' or 'No', it’s the same logic:

$acessible = isset($_POST['acessible']) && ($_POST['acessible']=='Yes');

Always using the value true in the ==, carefully case in the same way as they were typed, as YES and Yes are completely different things for comparison.

If you want to exaggerate, you can also add a && is_string( ... ) shortly after the isset to sanitize arrays, but this usually doesn’t happen in a normal form. Remember that the quotes in '2' are because GET and POST are always passed as string

  • I edited my question, in case I need to force the variables to return in the right way. And not the menu. With the menu variable everything is ok.

  • 1

    @Anacarolinaribeiro so it is important to ask the full question already. Qq way, updated the answer to your case, it is also super simple

  • But for me to make her Boolean??

  • Is still giving invalid!!

  • 1

    Then it has nothing to do with your question, it’s a mistake on some line that you didn’t post in the question, and you’re using it with wrong parameters. Maybe a Binding of DB with some :variavel left over or missing, or some function with something more or less. The part of Boolean that was what you asked I answered. If you have any more questions specifically about the Boolean part, I can try to update and help. If it is about another part of the code, it would be the case of a separate question. Search about this error, already have answer on the site.

  • I have updated all my code there. It gives this error here: $stmtUpdateMoreInformations->execute()!!!

  • 1

    That’s exactly what I said, you have one bind only, and several variables. You have to have one bindvalue for each :variable. It’s no use just doing :menu

  • Really, thank you very much!

  • I corrected it, but it’s still wrong! I’m actually using input radio pro yes and no, it’s always picking up 1 on my echo even when it’s on yes and no.

Show 4 more comments

Browser other questions tagged

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