php
  • pdo
  • 2017-07-14 113 views -1 likes 
    -1

    我正在嘗試使用PDO更新我的postgres數據庫中的一行。PDO更新不返回變量

    表單不會將變量發送到處理程序文件。

    我不確定問題出在哪裏,我一直在爲此奮鬥了幾天。

    形式

    //$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

    在處理程序,您使用線條狀頂部...

    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使用同樣的方式。

    +0

    對不起,應該提到,$ maxContent已經設置。 未發佈的變量是photograph_date,photograph_number和primary_shot。 –

    +0

    只需重新讀取Update語句,將包含$ maxContext作爲搜索條件之一,然後將該值設置爲$ maxContext。當然,這將無濟於事?! –

    +0

    啊是的,很好的發現,我編輯的代碼是$ context_number –

    相關問題