0

I want update my database from an array except one value from $_POST

$_POST['value'] = 'b';
$array = array('a','ab','b','bb','c','cc');
//insert 'a','ab','bb','c','cc' except $_POST['value'] 'b'
mysql_query("UPDATE core SET list='".$newarray."' WHERE id='".$_POST['id']."' ");

How do I do? use preg_replace remove the $_POST['value'] --> difficulty judge the comma ','

or advanced mysql query skill?

2
  • Set $_POST['value'] after update? Commented Sep 30, 2011 at 10:49
  • $_POST['value'] = 'b'; $array = array('a','ab','b','bb','c','cc'); I don't understand this part. Commented Sep 30, 2011 at 10:50

3 Answers 3

2

If I understood the question correctly...

$newarray = implode( ',', array_diff( $array, array( $_POST[ 'value' ] ) ) );

// $newarray == "a,ab,bb,c,cc"
Sign up to request clarification or add additional context in comments.

1 Comment

Mind that this answer does no escaping (mysql_real_escape) of the array elements, so $array may only include database-safe values ...
1

Try this if you need to filter by array value

$filtered = array_filter($array, function($value) {
  // you need to return a boolean value which tells
  // whether to include this element or not
  return $value != 'b';
});
$fields = implode(', ', $filtered);
mysql_query("UPDATE core SET list='".$fields."' WHERE id='".$_POST['id']."' ");

Or if you need to filter by array keys

$keys = array('...'); // list here allowed keys
$filtered = array_merge(array_combine($keys, $keys), $_POST);
$fields = implode(', ', $filtered);
mysql_query("UPDATE core SET list='".$fields."' WHERE id='".$_POST['id']."' ");

Comments

0
$newarray = $array;
$key = array_search($_POST['value'], $newarray);

if ($key !== false)
{
   unset($newarray[$key]);
}

1 Comment

Note: This only removes the first matched item.

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.