Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我的查询不将数据插入数据库?_Php_Sql_Sql Server - Fatal编程技术网

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

Php 为什么我的查询不将数据插入数据库?,php,sql,sql-server,Php,Sql,Sql Server,编辑:似乎与数据库有关。我们弄不清是什么。 我在存储表单中的数据时遇到了问题。我在MS SQL中测试了这个查询(我们必须在学校使用它),但一旦我输入变量,它似乎就不起作用了。所以我猜问题来自变量。但是我不确定这一点,因为当我回显$\u POST变量时,它会像我希望的那样输出字符串。但是当我把它放在查询中时,它就不会将rit存储在我的数据库中。如果有人能帮我解决这个问题就太好了 HTML代码: <form action="registerSystem.php" method="post"&g

编辑:似乎与数据库有关。我们弄不清是什么。

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

HTML代码:

<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>
我在这里更改的是
$conn->query($query)
$query->execute()
。因为您使用的是预处理语句,所以需要调用预处理语句的对象实例的
execute
方法
$query

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

请参考,作为最佳实践,请使用有助于处理错误的
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_异常)

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

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

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

希望有帮助

我已经找到了你函数的问题所在

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


    }
}
?>
问题在这里:
值(:customer\u mail\u address,:naam,:paypal,:subscription\u start,null,:password,:country\u name);

问题在于
:subscription\u start
之后的null,而不是放置一个占位符,然后使用一个字符串将其值赋给null。然后您的查询应该可以工作

我不确定订阅的数据类型是什么,但我想应该是date。还可以使用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\u hash()
password\u verify()
它们在php.net上提供,供您阅读和理解


会话_开始()应该在页面的顶部我是否将其放在顶部并不重要,因为这关系到不将数据插入数据库。会话在登录后可以正常工作。无论如何,感谢您查看:)@coder
session\u start
不一定要在页面的顶部,除非
需要“connect.php”
打印任何内容。但最佳做法是在包含它的任何页面的最顶部设置
session\u start
。@Perumal93 conn变量仅用于执行查询。在登录部分可以正常工作,但在注册部分不起作用。@MatthieuKuivenhoven当变量
$conn
处于全局状态时,无需显式设置该变量已在全局上下文中定义。仍不起作用:(似乎是导致查询混乱的变量。但是,当我们使用普通字符串而不是变量执行查询时,它会插入查询。您的意思是不使用
bindParam
?当我们使用普通变量而不使用bindParam时,它会很好地响应。没有使用bindParam测试它。我们尝试用bindParam解决方案,但效果不太理想。
<?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!";


    }
}
?>