0

Okay, I'm completely lost...

What I am trying to do is update one row in my database.

$sizes = array($_POST['size_0'],
               $_POST['size_1'],
               $_POST['size_2'],
               $_POST['size_3'],
               $_POST['size_4']);

$sizes_upd = implode(", ", $sizes);

mysqli_query($con, "UPDATE beds 
                    SET `Available Sizes` = '$sizes_upd' " .
                    "WHERE ID = '$prod_id' ");

My Problem is that not all of my $_POST[] contain data. So let us say for instance $sizes[0] && $sizes[1] contain data, the following string is returned:

value1, value2, , , ,

How do I prevent this so that only the $_POST[] with data returns a value?

1
  • 1
    You should build the query while looping array checking if data is set, also your code is vulnerable to mysql injection Commented Mar 4, 2014 at 12:01

1 Answer 1

6

Use array_filter to clean up your array,

$sizes_upd = implode(", ", array_filter($sizes));
Sign up to request clarification or add additional context in comments.

5 Comments

+ bobby-tables, oh for the love of the spaghetti monster... sanitize database inserts
Could you PLS stop stealing all of my answers before i press this damn "Post your Answer" Button? xD Still +1 :P
@YUNOWORK: May be he is reading your mind in advance you post it.
I i think so. Im scared now. Time that i start to wear one of these aluminum hats so im protected from mental attacks.
Awesome! Simple Solution. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.