Give this a run. You'd still be vulnerable to possible SQL Injections but at least you'll trim your values and only try to write those that actually contain something, plus with addslashes() you'll help ensure that any characters that would possibly break SQL are escaped.
foreach ($_REQUEST as $key => $value) {
if(trim($key) == 'beer') {
if($beerid = trim($value)) {
$sql = sprintf('INSERT INTO beer (b_beer) VALUES ("%s")',addslashes($beerid));
if(mysqli_query($connection, $sql)){
echo "Records added successfully.";
} else {
printf("ERROR: Could not able to execute %s -- %s\n",$sql,mysqli_error($connection));
}
}
}
}
OR
Since you are only processing one request, and you know the name of the field as 'beer' you can simply this way:
if(isset($_REQUEST['beer']) {
if($beerid = trim($_REQUEST['beer'])) {
$sql = sprintf('INSERT INTO beer (b_beer) VALUES ("%s")',addslashes($beerid));
if(mysqli_query($connection, $sql)){
echo "Records added successfully.";
} else {
printf("ERROR: Could not able to execute %s -- %s\n",$sql,mysqli_error($connection));
}
}
}