简体   繁体   中英

I need to update a table joining two different tables. Is this query the appropriate one?

I've got 3 tables:

  answers         questions      users

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

I would like to update Qstatus from 0 to 1 when I answer a question (in table answers.) I'm looking to do this, by joining the two tables, but when I answer a question, Qstatus should change to 1 (Answered) but it doesn't change.

Here's some of the 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();
}

Any ideas? All help is much appreciated! Thank you!

Change your last 3 SQL lines with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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