我坚持用另一个表中的值更新表。可以说我有下表:
Persons table
PersonId (pk),
Name,
StateId (fk)
States table
StateId (pk),
Level
我在两个输入中显示“名称”及其对应的“级别”,让此人使用以下代码:
$sel = "SELECT Name, Level FROM Persons P JOIN States S ON P.StateID = S.StateID WHERE PersonID = :pid";
$stm = $pdo->prepare($sel);
$stm->execute(array(":pid" => $_GET['personId'])); // personId coming from calling page
$person = $stm->fetch(PDO::FETCH_ASSOC);
此人显示在以下输入中(未显示带有personId的隐藏输入)
<input type="text" name="person" value=" <?php echo($person['Name']) ?>">
<input type="text" name="level" value=" <?php echo($person['Level']) ?>">
更新时,我运行以下查询
$update = "UPDATE Persons SET Name = :name, Level = :level WHERE PersonID = :pid"; // Level = :level wont work
$stm = $pdo->prepare($update);
$stm->execute(array(':pid' => $_POST['personId'],
':name' => $_POST['name'],
':level' => $_POST['level'])); // again, not correct
Level = :level
显然是错误的,我试图用SELECT ...WHERE Level = :level
的结果替换它,但没有成功。如何根据输入中具有name =“ level”的值更新Persons表的外键?
答案 0 :(得分:0)
我找到了一个解决方案,原来是SQL问题:
我更改了$update
字符串以选择StateID
,其中Level = <input type="text" name="level"...>
标签包含的内容。
$update = "UPDATE Persons SET Name = :name, StateID = (SELECT StateID FROM States WHERE Level = :level) WHERE PersonID = :pid";