Reputation: 33
I have created a form but the php post page is not posting the form to the database.
I am not sure what is wrong with the code..
<?php
// Create connection
$conn = mysqli_connect("localhost", "username", "password", "database");
if(isset($_POST['submit']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
move_uploaded_file($file_loc,$folder.$file);
mysqli_query($conn,"INSERT INTO claim_info (
claimant_name,
claimant_surname,
file,
size,
type,
claimant_title,
claimant_position,
claimant_dob,
claimant_ni
) VALUES (
'$_POST[claimant_name]',
'$_POST[claimant_surname]',
'$file',
'$file_size',
'$file_type',
'$_POST[claimant_title]',
'$_POST[claimant_position]',
'$_POST[claimant_dob]')");
if (mysqli_query) {
echo "success!";
}
mysqli_close($conn);
}
?>
The form created is below:
<head>
<meta charset="UTF-8">
<title>Claim Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<title>Accident Claim - Form</title>
<style>
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form method="post" action="test.php" enctype="multipart/form-data">
<div id="stage1" class="form-group">
<h1>Claiment Detials</h1>
<label for="claimant_title">Title:</label>
<select class="form-control" id="claimant_title" name="claimant_title">
<option value=""></option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
</select><br />
<label for="claimant_position">Position: </label>
<select class="form-control" id="claimant_position" name="claimant_position">
<option value=""></option>
<option value="Driver">Driver</option>
<option value="Passenger">Passenger</option>
</select><br />
<label for="claimant_name">Name:</label>
<input class="form-control" id="claimant_name" name="claimant_name" /><br />
<label for="claimant_surname">Surname:</label>
<input class="form-control" id="claimant_surname" name="claimant_surname" /><br />
<label for="claimant_dob">DOB:</label>
<input class="form-control" id="claimant_dob" name="claimant_dob" placeholder="DD/MM/YYYY" /><br />
<label for="claimant_ni">N.I.:</label>
<input class="form-control" id="claimant_ni" name="claimant_ni" /><br />
<label for="claimant_address">Address:</label>
<input class="form-control" id="claimant_address" name="claimant_address" /><br />
<label for="claimant_postcode">Post Code:</label> <input class="form-control" id="claimant_postcode" name="claimant_postcode" /><br />
<label for="phone">Phone:</label>
<input class="form-control" id="claimant_phone" name="pclaimant_hone" /><br />
<label for="email">Email:</label>
<input class="form-control" id="claimant_email" name="claimant_email" /><br />
<label for="file">File:</label>
<input class="form-control" id="file" name="file" type="file"><br />
<span id="error-message1"></span><br />
<button class="btn btn-default" type="submit">Submit</button>
</div>
</form>
</div>
</body>
</html>
Can anyone guide me into fixing this? I don't get any errors when submitting just a blank page.
Upvotes: 0
Views: 861
Reputation: 6896
Firstly, this won't work as this query is empty, thus "success!" will not be echoed.
if (mysqli_query) {
echo "success!";
}
To check if it's success, you should put your actual query in an if statement.
Also, you should enclose database and table names with back ticks ` to prevent MySQL syntax errors. (Credits to @Refilon for pointing out)
// shortened for ease to view
$query = mysqli_query($conn,"INSERT INTO `claim_info` (`claimant_name`, `claimant_surname`) VALUES ('$_POST[claimant_name]', '$_POST[claimant_surname]')");
if ($query) echo "Query Successful!";
Secondly, you should prevent MySQL Injection using mysqli_real_escape_string()
:
$claimant_name = mysqli_real_escape_string($conn, $_POST[claimant_name]);
$claimant_surname = mysqli_real_escape_string($conn, $_POST[claimant_surname]);
$query = mysqli_query($conn,"INSERT INTO `claim_info` (`claimant_name`, `claimant_surname`) VALUES ('$claimant_name', '$claimant_surname')");
Upvotes: 1
Reputation: 16
You should bind the parameters before directly putting them into the database. Otherwise you could get an sql injection. Ex:
$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);
See this website for a complete example: http://www.mustbebuilt.co.uk/php/insert-update-and-delete-with-mysqli/
Upvotes: 0
Reputation: 5152
Change line:
mysqli_query($conn,"[...]");
into:
$result = mysqli_query($conn,"[...]");
and then check:
if ($result) {
echo "success!";
}
Because:
Return Values
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE. Source
Upvotes: 0