我正在尝试使用PDO更新postgres数据库中的一行。
表单不是将变量发送到处理程序文件。
我不确定问题出在哪里,我已经和它斗争了几天。
表格
//$maxContent is set and available
//$context_number is set and available
echo"<form method='post' action='updateSpatialPhoto_handler.php'>";
$query3 = $conn->prepare("SELECT * FROM excavation.contexts_spatial_photographs
WHERE contexts_spatial.area_easting = {$_SESSION['area_easting']}
AND contexts_spatial.area_northing = {$_SESSION['area_northing']}
AND contexts_spatial.context_number = {$_SESSION['context_number']}");
//contexts_spatial_photographs
$query3->execute();
while($r = $query3->fetch(PDO::FETCH_OBJ))
{
// for each needed
echo"<input type='hidden' name='photograph_date' value='".$r->photograph_date."'>";
echo"<input type='hidden' name='photograph_number' value='".$r->photograph_number."'>";
echo"<input type='hidden' name='primary_shot' value='".$r->primary_shot."'>";
echo"<input type='hidden' name='maxContext' value='", $maxContext,"'>";
};
echo"<input type='submit' value='Update Spatial Photo'>";
echo "</form>";
处理程序
<?php
session_start();
//
include 'connect/connect.php';
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (!isset($_SESSION['photograph_date'])) {$_SESSION['photograph_date'] = $_POST['photograph_date'];}
if (!isset($_SESSION['photograph_number'])) {$_SESSION['photograph_number'] = $_POST['photograph_number'];}
if (!isset($_SESSION['primary_shot'])) {$_SESSION['primary_shot'] = $_POST['primary_shot'];}
if (!isset($_SESSION['maxContext'])) {$_SESSION['maxContext'] = $_POST['maxContext'];}
if (isset($_SESSION['photograph_date'])) {$_SESSION['photograph_date'] = $_POST['photograph_date'];}
if (isset($_SESSION['photograph_number'])) {$_SESSION['photograph_number'] = $_POST['photograph_number'];}
if (isset($_SESSION['primary_shot'])) {$_SESSION['primary_shot'] = $_POST['primary_shot'];}
if (isset($_SESSION['maxContext'])) {$_SESSION['maxContext'] = $_POST['maxContext'];}
//echo "Photograph Date: "; echo $_SESSION['photograph_date']; echo "<br />";
//echo "Photograph Number: "; echo $_SESSION['photograph_number']; echo "<br />";
//echo "Primary Shot: "; echo $_SESSION['primary_shot']; echo "<br />";
try {
$sql3 = "UPDATE excavation.contexts_spatial_photographs SET
context_number = :context_number
WHERE contexts_spatial_photographs.area_easting = $area_easting
AND contexts_spatial_photographs.area_northing = $area_northing
AND contexts_spatial_photographs.context_number = $context_number";
$stmt2 = $conn->prepare($sql3);
// prepare sql and bind parameters
$stmt2->bindParam(':context_number', $maxContext, PDO::PARAM_INT);
$stmt2->execute();
echo "Record updated successfully in contexts spatial photographs<br />";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
?>
答案 0 :(得分:0)
在处理程序的顶部,您使用的行如...
if (isset($_SESSION['maxContext'])) {$_SESSION['maxContext'] = $_POST['maxContext'];}
这些是在会话变量中设置值,这很好。 但是在你的SQL中 -
AND contexts_spatial_photographs.context_number = $maxContext";
$ maxContext似乎没有设置在任何地方。这可能与会话变量相同,因此您需要
$maxContext = $_SESSION['maxContext'];
或
AND contexts_spatial_photographs.context_number = $_SESSION['maxContext']";
虽然使用bindParam会更好,但与:context_number
一样使用它。