简体   繁体   English

此查询有效吗? 我想更新一个表,将两个不同的表连接起来,并以两种不同的形式显示数据

[英]Is this query valid? I want to update a table, joining two different tables, and to display the data to two different forms

So, what I'm trying to do is: I've got pageA.php to get the user's name, surname and question saved to the DB (With forms and Jquery). 因此,我想做的是:我已经有了pageA.php来将用户名,姓氏和问题保存到数据库中(包括表单和Jquery)。 I'm displaying those questions in another table (Used by the "Technical Team" to answer those questions. I want to as soon as I answer, not to show the question in the technical team page anymore, and to display the answer in pageA.php. I've tried several things but nothing quite right yet. 我在另一个表格中显示这些问题(“技术团队”使用它来回答这些问题。我想尽快回答,不再在技术团队页面中显示该问题,而是在pageA中显示答案) .php。我已经尝试了几种方法,但是还没有完全正确的方法。

Here's the sql query that I'm not too sure about: 这是我不太确定的sql查询:

    $Ssql = "UPDATE questions SET Qstatus=1 
    WHERE answers.customerName=users.name 
    FROM users, answers 
    INNER JOIN users 
    ON answers.userID=users.userID;";

What I'm trying to do is, change the Qstatus to 1 when the question is answered. 我想做的是,在回答问题时将Qstatus更改为1。 0 = Not answered, 1 = Answered. 0 =未回答,1 =已回答。 First Page to ask for details. 第一页询问详细信息。

表格以获取客户的详细信息

使用文本框获取客户的问题

在技​​术团队回答问题时向用户显示的Gif

可以回答的问题

Here's the code I'm using ATM to insert the data to the DB and to update if the question was answered. 这是我使用ATM将数据插入数据库并更新是否已回答问题的代码。

        if ($_POST) 
{
    $staffName = test_input($_POST['staffName']);  
    $customerName = test_input($_POST['customerName']);
    $answer = test_input($_POST['answer']);

    try 
    {
        $host = '127.0.0.1';
        $dbname = 'webapp';
        $user = 'root';
        $pass = '';
        $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    } 

    catch (PDOException $e) 
    {
        echo "An error occurred saving your details. Please, try again!";
    }

    $sql = "INSERT INTO `answers` (`staffName`, `customerName`, `answer`) VALUES (?,?,?);";
    $sth = $DBH->prepare($sql);

    $sth->bindParam(1, $staffName, PDO::PARAM_INT);
    $sth->bindParam(2, $customerName, PDO::PARAM_INT);
    $sth->bindParam(3, $answer, PDO::PARAM_INT);

    $sth->execute();

    $Ssql = "UPDATE questions SET Qstatus=1 WHERE answers.customerName=users.name FROM users, answers INNER JOIN users ON answers.userID=users.userID;";
    $Ssth = $DBH->prepare($Ssql);
    $Ssth->execute();
}

What I'm struggling with is how to change the Qstatus to 1, when the question was answered, and how to trigger to show a table in pageA.php with all answers when there's answers available. 我正在努力解决的是如何在回答问题时将Qstatus更改为1,以及如何在有答案的情况下触发在pageA.php中显示带有所有答案的表。 Hope it's understandable and all help possible is greatly appreciated! 希望它是可以理解的,所有可能的帮助将不胜感激! THANK YOU!! 谢谢!!

In your query have some issue so i modified. 在您的查询中有一些问题,所以我修改了。

 $Ssql = "UPDATE questions,users,answers          
         SET Qstatus=1 
         WHERE answers.customerName=users.name   
         AND answers.userID=users.userID;";

Here may be add one conditon also. 这里也可以添加一个条件。 which is maintain question's anwser one-to-many relation. 这就是维护问题的one-to-many关系。 I dont know your datastructure of table so i cant add it here 我不知道您的表格数据结构,所以我不能在这里添加

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

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