【问题标题】:I need to update a table joining two different tables. Is this query the appropriate one?我需要更新一个连接两个不同表的表。这个查询合适吗?
【发布时间】:2016-01-03 21:40:29
【问题描述】:

我有 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();
}

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

【问题讨论】:

  • 更新查询有一些问题。 questionid 与 userid 如何相等?
  • 您好 devpro。我知道查询有一些问题,但我不知道它是什么。这就是我在这里寻求帮助的原因。我尝试更改 qid=?但没有用。也许我没有清楚地解释自己。无论如何,谢谢!

标签: php jquery html mysql database


【解决方案1】:

将最后 3 行 SQL 更改为:

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

【讨论】:

  • 我试过了,但它没有将 qStatus 更改为 1。无论如何,谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多