Jump to content

filter empty fields in $_POST array


Raz3rt
Go to solution Solved by Raz3rt,

Recommended Posts

Hi, i'm new here and hope i've finally found a forum where i can find the perfect solution for my questions.

 

Well since a year now i started to work with PHP and still now and then there are some things where i seize on. My question is on of those situations.

 

I have the following code:

if (isset($_POST)){
    
    foreach ($_POST as $key => $value){

    if (!empty($value)){
    $update_rma_detail_stmt = $dbh->prepare("UPDATE rma_detail SET $key = ? WHERE rd_rma_nr = ?");
    $update_rma_detail_stmt->bindParam(1, $value);
    $update_rma_detail_stmt->bindParam(2, $getadmrmaid);
    $update_rma_detail_stmt->execute();

    }
  } 
}

All i want is when there is an empty (not filled in) input field because it is not mandatory the foreach still continuous to the end.

When i use

if (!empty($value)){

It still outputs these error:

 

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ' '

 

However, when i dont use i got an other error: 

 

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: ''

 

It is however so that the datetime is the very first empty field it come across when looping. I think my if (!empty($value)){ is doing the right thing but still gives error's on integers. Can it be? I allready tried to use trim() or array_filter() but because i previously never used it i dont know if i am doing it the right way so any help is welkom!

 

Thanks!

Edited by Raz3rt
Link to comment
Share on other sites

So the value of key is going to be an integer value, right? I believe for PDO you should be specifying the data type, in this instance PDO::PARAM_INT:

 

$update_rma_detail_stmt->bindParam(1, $value, PDO::PARAM_INT);

 

Does this make any difference? The check for empty is returning FALSE so the variable must be a non-null value?

Link to comment
Share on other sites

  • Solution

So the value of key is going to be an integer value, right? I believe for PDO you should be specifying the data type, in this instance PDO::PARAM_INT:

 

$update_rma_detail_stmt->bindParam(1, $value, PDO::PARAM_INT);

 

Does this make any difference? The check for empty is returning FALSE so the variable must be a non-null value?

 

 

Thanks IanA for you reply!

 

As i remember it is not nessesary to specify the date type, i never did it before though. After a long search i've manage to find my "stupid" fold i have made. My problem only appears when the foreach wanted to loop through a select from my form & my first rule in the select was: <option value="$nbsp;" selected></option>. As you may notice the value is a space so the person cannot make a wrong entry. Changing the   to 0 fixed the problem. Sorry i had to bother for such a small thing.

Edited by Raz3rt
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.