简体   繁体   English

如何基于发布的值和结果运行SQL查询,重定向到其他页面或执行下一组代码?

[英]How do I run a SQL query based off of a posted value and based off the results, redirect to different page or execute the next set of code?

I am working on a site and there's a point at which you can choose to Sign in or Sign out, but I need to be able to run a query to determine if the previous clocking was an In or and Out so that the user can't double clock in or out. 我在一个网站上工作,可以选择登录或注销,但是我需要能够运行查询以确定先前的时钟是输入还是输出,以便用户可以t双重时钟输入或输出。 I am using PHP to accomplish this. 我正在使用PHP完成此任务。

Here is where I am now but I'm not sure if this is correct, and I'm not sure how to assign the InOrOut to a value to compare it to whether it = 'In' or 'Out' 这是我现在的位置,但是我不确定这是否正确,也不确定如何将InOrOut分配给值以将其与=“ In”或“ Out”进行比较

$query = $pdo->prepare("SELECT TOP 1 InOrOut FROM visitor WHERE email = ? ORDER BY clocking DESC");
$email = $_POST['email'];
$query->execute(array($email,'InOrOut'));

This is for the Sign In page. 这用于“登录”页面。 If the previous clocking was an 'In', then I want to redirect to the Sign out page. 如果以前的时钟是“输入”,那么我想重定向到“注销”页面。

If the previous clocking was not an 'In', then continue with the remainder of the query which is below. 如果先前的时钟不是“ In”,则继续下面的查询其余部分。

//SQL query to be run
$query = $pdo->prepare("SELECT email,firstname,lastname,coname FROM visitor WHERE email = ?");
//Assign variable $email to the POSTed value from inmail.php
$email = $_POST['email'];
$email = filter_var ($email, FILTER_SANITIZE_EMAIL);
//Executes and stores the results of the query to be fetched in in.php
$query->execute(array($email,'firstname'));

UPDATE: 更新:

I tried the following and did not get any errors, but it didn't do anything either. 我尝试了以下操作,但未收到任何错误,但也没有执行任何操作。 Worked like it normally does instead of redirecting. 像平常一样工作,而不是重定向。 I put the following in it's own php file and made it a required asset. 我将以下内容放入自己的php文件中,并使其成为必需的资产。 Still not sure if I'm pulling the result in correctly. 仍然不确定我是否正确提取结果。

<?php
//TESTING  possibly create new PHP and require it on in.php
$query = $pdo->prepare("SELECT TOP 1 InOrOut FROM visitor WHERE email = ? ORDER BY clocking DESC");
$email = $_POST['email'];
$query->execute();
$result= $query->fetchAll(PDO::FETCH_ASSOC);
if($result == 'In')
{
    echo '<script type="text/javascript">alert("It works.");</script>';
    header("Location: ../outmail.html",TRUE,302);
}


?>

Once a user hits sign in using their email, the sign in page posts to another page which has the following assets. 用户点击使用电子邮件的登录名后,登录页面便会发布到另一个具有以下资产的页面。

<!-- Accesses the connection file and assigns the input 'email to $email. Runs the query and returns the matching results' -->
<?php
require 'assets/dbinfo.php';
require 'assets/clockin.php';
require 'assets/indb.php';
?>

Finally got it working with the following code. 终于让它与以下代码一起使用。 I create two new php files, one for clock in and one for clock out and added it as an asset to my main page. 我创建了两个新的php文件,一个用于时钟输入,一个用于时钟输出,并将其作为资产添加到我的主页中。 Thanks for taking a look! 谢谢参观!

$query = $pdo->prepare("SELECT TOP 1 * FROM visitor WHERE email = ? ORDER BY clocking DESC");
$email = $_POST['email'];
$inout = 'InOrOut';
$query->execute(array($email,'firstname'));
$result = $query->fetchColumn(7);
if($result == 'Out')
{
    $message = "You have already signed out, redirecting you to sign in page.";
echo "<script type='text/javascript'>alert('$message');</script>";
echo "<script>setTimeout(\"location.href = '../inmail.php';\",0);</script>";
}
?>

Use Single Query TO execute 使用单一查询执行

$query = $pdo->prepare("SELECT * FROM visitor WHERE email = ? ORDER BY clocking DESC");
$email = $_POST['email'];
$query->execute();
$result= $query->fetchAll(PDO::FETCH_ASSOC);

if(count($result) > 0)
{
// Previous clocking Not an In
}
else
{
// Previous clocking an In
}

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

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