There are a few things that you should do with this code.
First, since you're trying to use every field in your form, you should add the required
attribute to each input field.
Second, since you're submitting a post request, I would suggest that you check in your PHP script if the request is a POST and check if the values you're attempting to get from the POST exist. This will prevent someone from simply navigating to your script and trying to execute it improperly:
<?php
if (
$_SERVER['REQUEST_METHOD'] === 'POST' &&
array_key_exists('name', $_POST) &&
array_key_exists('email', $_POST) &&
array_key_exists('va', $_POST) &&
array_key_exists('va-iata', $_POST) &&
array_key_exists('pricing', $_POST) &&
array_key_exists('vam-vms', $_POST) &&
array_key_exists('add-info', $_POST)
) {
/* ... */
} else {
http_response_code(401);
}
?>
Third, don't use mysqli
. The PDO class has been out since PHP 5.1.0 and is the preferred method of connecting to databases. In this case, use a Try/Case statement to attempt to establish a PDO connection:
try {
/* database configuration */
$servername = "host";
$username = "username";
$password = "password";
$dbname = "dbname";
/* establish a PDO connection */
$dsn = "mysql:dbname=$dbname;host=$servername;charset=utf8mb4";
$db = new PDO($dsn, $username, $password);
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
/* run SQL query */
} catch(PDOException $ex) {
/* handle database error */
}
Fourth, you'll need to tweak your SQL statement a bit, I think that this is where it is causing some errors. It is generally best practice to escape the column name using back-ticks, but in this case since it looks like your column names have spaces in them, it is actually necessary (plus name
might be a keyword too). It is also best practice to use parameters as to prevent for SQL injection. The idea is that you pass a user-defined name in the SQL query with a :
prefix and that name corresponds to a value defined in an array that gets passed:
/* Insert all the values in the request_site */
$stmt = $db->prepare('
INSERT INTO `request_site` (
`Name`,
`Email Address`,
`Virtual Airline`,
`Virtual Airline IATA`,
`Pricing`,
`VAM/PHPVMS`,
`Additional info`
) VALUES (
:name,
:email,
:va,
:vaiata,
:pricing,
:vamvms,
:additionalinfo
);
');
$stmt->execute(array(
':name' => $name,
':email' => $email,
':va' => $va,
':vaiata' => $vaiata,
':pricing' => $pricing,
':vamvms' => $vamvms,
':additionalinfo' => $additionalinfo
));
Put it all together and you get:
<?php
if (
$_SERVER['REQUEST_METHOD'] === 'POST' &&
array_key_exists('name', $_POST) &&
array_key_exists('email', $_POST) &&
array_key_exists('va', $_POST) &&
array_key_exists('va-iata', $_POST) &&
array_key_exists('pricing', $_POST) &&
array_key_exists('vam-vms', $_POST) &&
array_key_exists('add-info', $_POST)
) {
/* put the $_POST values in variables */
$name = $_POST['name'];
$email = $_POST['email'];
$va = $_POST['va'];
$vaiata = $_POST['va-iata'];
$pricing = $_POST['pricing'];
$vamvms = $_POST['vam-vms'];
$additionalinfo = $_POST['add-info'];
try {
/* database configuration */
$servername = "host";
$username = "username";
$password = "password";
$dbname = "dbname";
/* establish a PDO connection */
$dsn = "mysql:dbname=$dbname;host=$servername;charset=utf8mb4";
$db = new PDO($dsn, $username, $password);
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
/* Insert all the values in the request_site */
$stmt = $db->prepare('
INSERT INTO `request_site` (
`Name`,
`Email Address`,
`Virtual Airline`,
`Virtual Airline IATA`,
`Pricing`,
`VAM/PHPVMS`,
`Additional info`
) VALUES (
:name,
:email,
:va,
:vaiata,
:pricing,
:vamvms,
:additionalinfo
);
');
$stmt->execute(array(
':name' => $name,
':email' => $email,
':va' => $va,
':vaiata' => $vaiata,
':pricing' => $pricing,
':vamvms' => $vamvms,
':additionalinfo' => $additionalinfo
));
echo 'New record created successfully';
} catch(PDOException $ex) {
/* handle database error */
echo 'Connection failed: ', $ex->getMessage();
}
} else {
http_response_code(401);
}
?>