2014-06-10 179 views
0

我越來越沒有錯誤就這一點,但SQL表不會得到任何數據...INSERT INTO不會工作

<?php 

error_reporting(E_ALL); 

ini_set('display_errors', '1'); 

header('Content-type: text/plain; charset=utf-8'); 

$con = mysqli_connect("127.0.0.1","root","123456","bikeshop"); 

if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
session_start(); 
$name=$_SESSION['username']; 
$totalprice = $_POST['totalprice2']; 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$email = $_POST['email']; 
$adress = $_POST['adress']; 
$mobilephone = $_POST['mobilephone']; 
$postalcode = $_POST['postalcode']; 
$city = $_POST['city']; 
$homephone = $_POST['homephone']; 
$deliveryinfo = $_POST['deliveryinfo']; 
$status = 'pending'; 

mysqli_query($con,"INSERT INTO orderbank (firstname, lastname, email, adress, mobilephone, postalcode, city, homephone, deliveryinfo, cost, status) VALUES ('$firstname','$lastname','$email','$adress',''$mobilephone','$postalcode',city,'$homephone','$deliveryinfo','$totalprice','$status')"); 

?> 
+1

你是不是檢查錯誤... – siride

+2

你有',城市,'不帶引號或印記。這肯定會給你一個錯誤。另外,請不要使用插入的字符串,也不要使用無用戶輸入。這太可怕了。 – siride

+0

@ user0000000:如果我可能會問,如何提供幫助? –

回答

0

質量的代碼裝飾幫忙看看錯誤更快。 嘗試:

INSERT INTO `orderbank` (
    `firstname`, 
    `lastname`, 
    `email`, 
    `adress`, 
    `mobilephone`, 
    `postalcode`, 
    `city`, 
    `homephone`, 
    `deliveryinfo`, 
    `cost`, 
    `status`) 
VALUES (
    '$firstname', 
    '$lastname', 
    '$email', 
    '$adress', 
    '$mobilephone', 
    '$postalcode', 
    '$city', 
    '$homephone', 
    '$deliveryinfo', 
    '$totalprice', 
    '$status')" 
+1

我猜想像firstname這樣的字段需要在SQL中引用,所以這可能會失敗。 – siride

+0

爲真。我通常使用那些無處不在。 –

+1

我的意思是圍繞'VALUES'子句中的變量。當像「Anna Sue」這樣的字符串放在'$ firstname'中時,你認爲會發生什麼? – siride

4

看看你VALUES線路查詢:

VALUES ('$firstname','$lastname','$email','$adress',''$mobilephone','$postalcode',city,'$homephone','$deliveryinfo','$totalprice','$status') 

不宜,city,,'$city',?爲什麼會有兩個單引號'$mobilephone

前嘗試改變的是這樣的:

VALUES ('$firstname','$lastname','$email','$adress','$mobilephone','$postalcode','$city','$homephone','$deliveryinfo','$totalprice','$status') 

這就是說,你也沒有做任何錯誤捕獲的查詢。這裏有很多問題。

這就是說,這裏是你的代碼的重構,使這一切都更清潔&更穩定。請注意,使用mysqli_error報告查詢錯誤以及連接的mysqli_connect_error。還使用$_POST值的數組來允許更輕鬆地處理$_POST值。這也允許對isset()!empty()的輸入進行一些基本驗證。還使用mysqli_stmt_bind_param在查詢中設置變量。最後使用mysqli_free_result & mysqli_close乾淨地獲得空閒內存&關閉連接後,一切都說&完成。

// Sundry items set by the original poster in the original code. 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
header('Content-type: text/plain; charset=utf-8'); 
session_start(); 

// Credentials. 
$host = "127.0.0.1"; 
$user = "root"; 
$password = "123456"; 
$database = "bikeshop"; 

// Connecting, selecting database 
$con = mysqli_connect($host, $user, $password, $database) or die("Failed to connect to MySQL: " . mysqli_connect_error()); 

// Set a '$_POST' array and roll through each value. 
$post_array = array('totalprice2', 'firstname', 'lastname', 'email', 'adress', 'mobilephone', 'postalcode', 'city', 'homephone', 'deliveryinfo'); 
foreach ($post_array as $post_key => $post_value) { 
    $$post_value = isset($_POST[$post_value]) && !empty($_POST[$post_value]) ? $_POST[$post_value] : null; 
} 

// Set the other variables. 
$name = $_SESSION['username']; 
$status = 'pending'; 

// Set the query. 
$query = "INSERT INTO `orderbank` (`firstname`, `lastname`, `email`, `adress`, `mobilephone`, `postalcode`, `city`, `homephone`, `deliveryinfo`, `cost`, `status`)" 
     . " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 
     ; 

// Bind the params. 
mysqli_stmt_bind_param($insertsql, 'sssssssssss', $firstname, $lastname, $email, $adress, $mobilephone, $postalcode, $city, $homephone, $deliveryinfo, $totalprice2, $status); 

// Run the query. 
$result = mysqli_query($con, $insertsql) or die(mysqli_error()); 

// Free the result set. 
mysqli_free_result($result); 

// Close the connection. 
mysqli_close($con); 
+0

靠近手機',''$ mobilephone''也是額外的單引號 – Saqueib

+0

@Saqueib沒有額外的逗號。額外的單引號。 – JakeGould

+3

兩點。 1.'或die(mysqli_connect_errno());'只會在輸出任何連接錯誤時輸出。您需要'mysqli_error($ con)'來代替。 2.該查詢仍對[SQL注入](http://bobby-tables.com)攻擊廣泛開放。你可能想在你的答案中提到這一點。 –

0

更好的解決方案是使用預處理語句和綁定參數。

爲什麼?這裏有一些原因:

  • SQL注入不會發生。

  • 不會有任何奇怪的SQL錯誤,如INSERT中的「列不允許在這裏」。

  • 如果您使用準備好的語句,性能會更好。

  • 代碼將會更清晰。

下面是示例代碼:

<?php 
    $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world'); 

    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 

    $stmt = $mysqli->prepare("INSERT INTO Country (ID, Name) VALUES (?, ?)"); 
    $stmt->bind_param('is', $id, $name); 

    $id = 1; 
    $name = "USA"; 

    $stmt->execute(); 

    printf("%d Row inserted.\n", $stmt->affected_rows); 

    $stmt->close(); 

    $mysqli->close(); 
?> 
+0

實際代碼示例如何? – Phil

+0

*這不是一個答案*你只是避免了這個問題,沒有解決它。準備好的聲明在某些情況下性能更差。 – Raptor

+1

沒有語法錯誤是'怪異的SQL錯誤',它的我們誰不明白如何正確地使用語言 –