为什么我的查询不会将数据插入数据库?

时间:2017-01-18 10:58:19

标签: php sql sql-server

编辑:似乎是数据库的东西。我们无法弄清楚它是什么。

我在存储放入表单中的数据时遇到问题。我在MS SQL中测试了查询(我们必须将它用于学校),但是一旦我输入变量,它似乎无法工作。所以我猜测问题来自变量。但是我不确定,因为当我回显$ _POST变量时,它输出的字符串就像我想要的那样。但是当我把它放在查询中时它就不会将rit存储在我的数据库中。如果有人能帮我解决这个问题会很棒。

HTML code:

<form action="registerSystem.php" method="post">
Email:
<input type="email" name="emailAdres" required> <br>
Naam:
<input type="text" name="naamGebruiker" required> <br>
Wachtwoord:
<input type="password" name="wachtwoordGebruiker" required> <br>
Herhaal wachtwoord:
<input type="password" name="bevestigWachtwoord" required> <br>
<input type="submit" value="Registreer">
</form>

Php代码:     

require "connect.php";

session_start();

GLOBAL $conn;

function createAccount(){

$email = $_POST['emailAdres'];
$username = $_POST['naamGebruiker'];
$wachtwoord = $_POST['wachtwoordGebruiker'];
GLOBAL $conn;

$hashed_pass = md5($wachtwoord);
$paypal = $email;
$subscription_start = date("Y:m:d");
$land = 'Nederland';

$query = $conn->prepare("INSERT INTO Customer (customer_mail_adress, name,  paypal_account, subscription_start, subscription_end, password, country_name) "
                                               ."VALUES (:customer_mail_adres, :naam, :paypal, :subscription_start, null, :password, :country_name)");

$query->bindParam(':customer_mail_adres', $email);
$query->bindParam(':naam', $username);
$query->bindParam(':paypal', $paypal);
$query->bindParam(':subscription_start', $subscription_start);
$query->bindParam(':password', $hashed_pass);
$query->bindParam(':country_name', $land);
$conn->query($query);

}

if($_SERVER['REQUEST_METHOD'] === 'POST'){
        //password check
        if ($_POST['wachtwoordGebruiker'] == $_POST['bevestigWachtwoord']) {

            createAccount();
            header("location: loginSystem.php");

        } else {

            echo "De opgegeven wachtwoorden komen niet overeen!";


    }
}?>

2 个答案:

答案 0 :(得分:0)

$query = $conn->prepare("INSERT INTO Customer (customer_mail_adress, name,  paypal_account, subscription_start, subscription_end, password, country_name)"
                                               ." VALUES (:customer_mail_adres, :naam, :paypal, :subscription_start, null, :password, :country_name)");

$query->bindParam(':customer_mail_adres', $email);
$query->bindParam(':naam', $username);
$query->bindParam(':paypal', $paypal);
$query->bindParam(':subscription_start', $subscription_start);
$query->bindParam(':password', $hashed_pass);
$query->bindParam(':country_name', $land);
$query->execute();

我在此处所做的更改是$conn->query($query);$query->execute()。因为您正在使用预准备语句,所以需要调用预准备语句execute的对象实例的$query方法。

$conn->query($sql)通常仅在使用SELECT查询检索结果时使用,该查询不包含从用户输入接收数据的过滤条件。

为了您的信息,作为最佳做法,请使用try catch块来包装代码,以帮助您处理错误。

try {
  $query = $conn->prepare("INSERT INTO Customer (customer_mail_adress, name,  paypal_account, subscription_start, subscription_end, password, country_name)"
                                                   ." VALUES (:customer_mail_adres, :naam, :paypal, :subscription_start, null, :password, :country_name)");

  $query->bindParam(':customer_mail_adres', $email);
  $query->bindParam(':naam', $username);
  $query->bindParam(':paypal', $paypal);
  $query->bindParam(':subscription_start', $subscription_start);
  $query->bindParam(':password', $hashed_pass);
  $query->bindParam(':country_name', $land);
  $query->execute();
} catch (PDOException $ex) {
    echo $ex->getMessage(); // or die($ex->getMessage());
}

在使用try catch块之前,请将PDO的错误报告设置为异常:

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

创建PDO对象实例后立即设置此属性。

您还可以在对象实例化期间通过以下构造函数设置此属性:

$conn = new PDO('mysql:host=localhost;dbname=demo', 'root', 'password', array(
   PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));

希望它有所帮助!

答案 1 :(得分:0)

我找到了你的功能问题。

问题在于:VALUES (:customer_mail_adres, :naam, :paypal, :subscription_start, null, :password, :country_name)");

在问题:subscription_start之后为null,而是将一个占位符放在适当位置,然后有一个字符串,您将其赋值为null。那么你的查询应该有效。

我不确定subscription_end的数据类型是什么,但我想它应该是日期。并且还使用try catch块,以便您可以在sql查询中看到错误。在至少运行查询之后也不要急于重新加载下一页但是在标题()上有一些延迟,这样你就可以打印成功消息并查看它是否显示然后加载下一页

这就是我更新你的功能的方式。

<?php

require 'connect.php';



session_start();

GLOBAL $conn;

function createAccount()
{

    $email      = $_POST['emailAdres'];
    $username   = $_POST['naamGebruiker'];
    $wachtwoord = $_POST['wachtwoordGebruiker'];
    GLOBAL $conn;

    $hashed_pass        = md5($wachtwoord);
    $paypal             = $email;
    $subscription_start = date("Y:m:d");
    $land               = 'Nederland';
    $enddate               = 'null';


    try {

        $query = $conn->prepare("INSERT INTO Customer (customer_mail_adress, name,  paypal_account, subscription_start, subscription_end, password, country_name) " . "VALUES (:customer_mail_adres, :naam, :paypal, :subscription_start, :enddate, :password, :country_name)");

        $query->bindParam(':customer_mail_adres', $email);
        $query->bindParam(':naam', $username);
        $query->bindParam(':paypal', $paypal);
        $query->bindParam(':subscription_start', $subscription_start);
        $query->bindParam(':password', $hashed_pass);
        $query->bindParam(':country_name', $land);
        $query->bindParam(':enddate', $enddate);

        if ($query->execute()) {

            echo  "Done";
        }


    }
    catch (PDOException $e) {



        echo "error". $e->getMessage();
    }



}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    //password check
    if ($_POST['wachtwoordGebruiker'] == $_POST['bevestigWachtwoord']) {

        createAccount();
         header("refresh:5;url=loginSystem.php");

    } else {

        echo "De opgegeven wachtwoorden komen niet overeen!";


    }
}
?>

希望这有帮助。

  

注意:不要使用md5();加密你的密码不再安全,   而是使用PHP函数password_hash()password_verify()   它们可以在php.net上找到,供您阅读和理解。