我需要更新一个连接两个不同表的表。这个查询是否合适?

时间:2016-01-03 21:40:29

标签: php jquery html mysql database

我有3张桌子:

  answers         questions      users

  userID         userID        userID
  staffName      question      name
  qID            Qstatus       surname
  answer         timeDataQ     timeData
  customerName   
  timeAnswer

当我回答问题时(表格答案中)我想将Qstatus从0更新为1.我想通过加入两个表来实现这一点,但当我回答问题时,Qstatus应该更改为1 (已回答)但它没有改变。

以下是一些PHP

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

    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`, `qID`) 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->bindParam(4, $qID, PDO::PARAM_INT);

    $sth->execute();

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

有什么想法吗?非常感谢所有帮助!谢谢!

1 个答案:

答案 0 :(得分:1)

使用以下内容更改最后3条SQL

$Ssql = "UPDATE questions SET Qstatus=1 WHERE qID = ?";
$Ssth = $DBH->prepare($Ssql);
$Ssth->bindParam(1, $qID, PDO::PARAM_INT);
$Ssth->execute();