What you have:
foreach($_POST as $key) {
$keys[] = $key;
}
$query = "INSERT INTO $table (" . implode(",", $keys) . ")";
$query .= " VALUES('" . implode("','", $_POST) . "')";
Part of the problem is you're imploding all of $_POST which normally is ok, but here you need to exclude some values (the X and Y).
There's several ways to do this...
$column = '';
$data = '';
foreach($_POST as $key => $value)
{
if(strtolower($key) != 'x' && strtolower($key) != 'y')
{
if($column != '')
$column .= ', ';
$column .= $key;
if($data != '')
$data .= ', ';
$data .= "'" . $value . "'";
} // end if(strtolower($key) != 'x' && strtolower($key) != 'y')
} // end foreach($_POST as $key => $value)
$query = "INSERT INTO $table (" . $column . ") VALUES (" . $data . ")";
That's one way. Not overly graceful, but should work. Another way might be to just do a foreach and instead of building the SQL clauses like I did above, just have it store the form data in new variables (kind of what you did with $key). Then use implode. But swap out $_POST that you have with a variable you populate in the foreach() group. Make sense?
Doesn't look like PHP has a built in way to delete specific elements from an array (it has pop, but it deletes only from one end of the array). So in the end, you have to build your own...