2016-03-29 182 views
0

我已經創建了一個表單,但是php post頁面沒有發佈表單到數據庫。PHP表單不提交到數據庫

我不知道什麼是錯的代碼..

<?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); 
} 
?> 

創建的形式是如下:

<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> 

任何人都可以引導我進入修復此?提交空白頁面時我沒有任何錯誤。

+0

問題是此行'如果(mysqli_query){'。使用它作爲'if(mysqli_query($ conn,「INSERT INTO claim_info(.......)){echo」success!「;}' – Saty

+0

'mysqli_query($ link,$ query)或者die(mysqli_error($鏈接))'會給你提供更好的信息是否有錯誤 – mitkosoft

回答

0

更改行:

mysqli_query($conn,"[...]"); 

到:

$result = mysqli_query($conn,"[...]"); 

,然後檢查:

if ($result) { 
    echo "success!"; 
} 

因爲:

返回值

失敗時返回FALSE。對於成功的SELECT,SHOW,DESCRIBE或 EXPLAIN查詢,mysqli_query()將返回一個mysqli_result對象。對於 其他成功的查詢mysqli_query()將返回TRUE。 Source

1

首先,這不起作用,因爲這個查詢是空的,因此「成功!」將不會被迴應。

if (mysqli_query) { 
    echo "success!"; 
} 

要檢查它是否成功,你應該把你的實際查詢放在if語句中。

此外,你應該附上數據庫和表名稱反向滴答`以防止MySQL語法錯誤。 (貸@Refilon您指出)

// 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!"; 

其次,你應該使用mysqli_real_escape_string()防止MySQL的注入:

$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')"); 
+1

我建議數據庫名稱,表名和字段名應該用''包圍,因爲當你不這樣做時,數據庫名是一個sql函數,它會失敗。 – Refilon

+0

@Refilon Yup,更新後,謝謝指出:) – Panda

0

你應該直接之前綁定參數將它們放入數據庫中。否則,你可能會得到一個SQL注入。例如:

$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"); 
$stmt->bind_param('sssd', $code, $language, $official, $percent); 

一個完整的例子來看看這個網站: http://www.mustbebuilt.co.uk/php/insert-update-and-delete-with-mysqli/