简体   繁体   English

sqlsrv_query 执行检查数据功能后没有返回任何结果

[英]The sqlsrv_query does not return any result after the checking data function is perform

I am working as a company project task related to prevent duplication of data entered for user registration.我正在担任与防止重复输入用户注册数据相关的公司项目任务。 The data i wish to prevent them from duplicate is email(user cant signup using same email for the application).我希望防止它们重复的数据是电子邮件(用户不能使用相同的电子邮件注册应用程序)。 There are two page involve in the checking process (signup.php and insert.php).检查过程中涉及两个页面(signup.php 和insert.php)。 Signup.php is where user key in informations on all 6 pages for register as a company user. Signup.php 是用户在所有 6 个页面上输入信息以注册为公司用户的地方。 Insert.php is where inserting data into sql server and i have implemented code for checking before inserting data into sql server. Insert.php 是将数据插入 sql server 的地方,我已经实现了在将数据插入 sql server 之前进行检查的代码。

    //email validation
    $emailvalid="SELECT Email FROM EcomLogin WHERE Email  = '$_POST[email]'";
    $stmt3=sqlsrv_query($conn,$emailvalid);
    if($stmt3){
        header("location:signup.php?status=error&message=Error when register with email!");
 exit();
        die(print_r(sqlsrv_errors(), true));
    }

Meanwhile in signup.php(registration form).同时在signup.php(注册表单)中。 on top of the form i have include a php code which block website for directing user to second registration page if first page information is duplicated with database record in this case is email.在表单顶部,我包含一个 php 代码,如果第一页信息与数据库记录重复,则该代码会阻止网站将用户引导至第二个注册页面,在这种情况下是电子邮件。

<?php 
if(isset($_GET['status']) && $_GET['status'] == 'error'){
echo '<script>toastr.error("Email has already been registered!");history.replaceState(null, "", location.href.split("&")[0]);</script>';}
?>

In the head section i have also include script below for toast function在 head 部分,我还包含了下面用于 toast 功能的脚本

 <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js" integrity="sha512-VEd+nq25CkR676O+pLBnDW09R7VQX9Mdiij052gVCp5yVH3jGtH70Ho/UUv4mJDsEdTvqRCFZg0NKGiojGnUCw==" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.css" integrity="sha512-3pIirOrwegjM6erE5gPSwkUzO+3cTjpnV9lexlNZqvupR64iZBnOOTiiLPb9M36zpMScbmUNIcHUqKD47M719g==" crossorigin="anonymous" />
    <script src="https://cdn.jsdelivr.net/npm/underscore@1.13.1/underscore-umd-min.js"></script>

注册第一页

在 sql server 数据库表中使用重复的电子邮件

After i input same email address for registration., it lead me to second page of registration page which is not allow to be do so.在我输入相同的电子邮件地址进行注册后,它会将我带到不允许这样做的注册页面的第二页。 it expect to be a toastr message appear on first page if duplicated email is used on first page and blocking them to the second page of registration page.如果在第一页上使用重复的电子邮件并将其阻止到注册页面的第二页,则它希望在第一页上出现一条 toastr 消息。

signup.php 的第二页,在第一页使用相同的电子邮件地址之后

Do any part of my coding is wrong or i am missing any kind of coding to perform the blocking function in php file?我的编码的任何部分是错误的还是我缺少任何类型的编码来执行 php 文件中的阻止功能? if the coding is correct do i need to create another function from preventing website direct user to second page?如果编码正确,我是否需要创建另一个功能来防止网站将用户直接转到第二页? The signup.php will look through another js file which is validating input field first then only execute insert.php. signup.php 将查看另一个 js 文件,该文件首先验证输入字段,然后仅执行 insert.php。

There are at least these two issues with your code:您的代码至少存在以下两个问题:

  • You need to understand what exactly sqlsrv_query() returns.您需要了解sqlsrv_query()究竟返回什么。 As is explained in the documentation , the result is a statement resource, or if the statement cannot be created and/or executed, false is returned.正如文档中所解释的,结果是一个语句资源,或者如果无法创建和/或执行该语句,则返回false So, the check if($stmt3) {...} means, that the statement is executed correctly, not that there is a row with the specified e-mail.因此,检查if($stmt3) {...}意味着该语句被正确执行,而不是有指定电子邮件的行。
  • Always use parameterized statements to prevent possible SQL injection issues.始终使用参数化语句来防止可能的 SQL 注入问题。

If I understand your issue correctly and you want to check if an e-mail already exists, the following approaches are possible solutions:如果我正确理解您的问题并且您想检查电子邮件是否已经存在,以下方法是可能的解决方案:

  • Use your current statement and check if the result set has one or more rows using sqlsrv_has_rows() .使用您当前的语句并使用sqlsrv_has_rows()检查结果集是否有一行或多行。
  • Change the statement to get the count of the rows for the specified e-mail.更改语句以获取指定电子邮件的行数。

PHP: PHP:

<?
...
$sql    = "SELECT Email FROM EcomLogin WHERE Email = ?";
$params = array($_POST[email]);
$stmt   = sqlsrv_query($conn, $sql, $params);
if ($stmt === false) {
    header("location:signup.php?status=error&message=Error when register with email!");
    die(print_r(sqlsrv_errors(), true));
}   
if (sqlsrv_has_rows($stmt)) {
    header("location:signup.php?status=error&message=Error when register with email!");
    die(print_r(sqlsrv_errors(), true));
}
...

...
$sql    = "SELECT COUNT(*) AS EmailCount FROM EcomLogin WHERE Email = ?";
$params = array($_POST[email]);
$stmt   = sqlsrv_query($conn, $sql, $params);
if ($stmt === false) {
    header("location:signup.php?status=error&message=Error when register with email!");
    die(print_r(sqlsrv_errors(), true));
}   
$count = 0;
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
    $count = $row["EmailCount"];
}
if ($count >= 1) {
    header("location:signup.php?status=error&message=Error when register with email!");
    die(print_r(sqlsrv_errors(), true));
}
...
?>

Keep in mind that请记住

 $stmt3=sqlsrv_query($conn,$emailvalid);
    if($stmt3){

might return TRUE even if there is no such email, because the sql query is legal but does not produce a result nor an error.即使没有这样的电子邮件,也可能返回 TRUE,因为 sql 查询是合法的但不会产生结果也不会产生错误。

You could count for emails as here:您可以在这里计算电子邮件:

$emailvalid="SELECT count(Email) as found FROM EcomLogin WHERE Email  = '$_POST[email]'";

Fetch the record an look at the value of found获取记录看看found的值

As a very last guard against dublicate emails you can create a UNIQUE INDEX above Email.作为防止重复电子邮件的最后一道防线,您可以在电子邮件上方创建一个唯一索引 With this the DB itself rejects duplictes有了这个数据库本身拒绝重复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM