2014-06-10 97 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错误',它的我们谁不明白如何正确地使用语言 –