problems with bindValue

Asked

Viewed 54 times

0

    $query = "INSERT INTO
                " . $this->table_name . "
            (data, sexo)
            VALUES
            (:data,:sexo)";

    $stmt->bindValue(':data', $_REQUEST['data']);
    $stmt->bindValue(':sexo', $_REQUEST['sexo']);

is making a mistake: Undefined index: sexo. the field sexo is a radio button and I didn’t select it before saving it in the bank. if I select the field sexo, data is saved normally in the database, why? wanted to save even if the field is selected or not...

  • Well, the error is clear and you seemed to understand the reason. You’re trying to get information that doesn’t exist, so Undefined. Try to check if the field exists with a if(isset($REQUEST_['campo'])) before inserting, if it does not exist, you make a decision, such as saving the field as null or 0, depending on how the column is in the database. Read more about isset()

1 answer

0

They are saved because when you select the radio field and send the POST when you redeem for $_REQUEST this index will exist. When you do not select it does not send this index: The reason for the error. if you want to save even without selecting put a value as default to save to the bank. Example:

debug to hurt the index received by the form print_r($_REQUEST);

avoid missing index error if (!isset($_REQUEST['sex'])) { $sex = 'its value'; }

Browser other questions tagged

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