doushi1960 2018-12-03 12:49
浏览 37

Php SQL更新查询生成NULL

Hopefully you can help me with this. I have been working on a very simple database and have hit a brick wall.

I have 2 PHP files, 1 containing functions (bintype.php) and then a file called update_bintype.php.

See below function code:

function update(){

    $query = "UPDATE
                " . $this->table_name . "
            SET
                    BinTypeName=:bintypename, BinTypeDescription=:bintypedescription, CleanPricePerBin=:costperbin
            WHERE
                BinTypeID = :id";

    $stmt = $this->conn->prepare($query);

        // posted values
        $this->BinTypeName=htmlspecialchars(strip_tags($this->BinTypeName));
        $this->BinTypeDescription=htmlspecialchars(strip_tags($this->BinTypeDescription));
        $this->CleanPricePerBin=htmlspecialchars(strip_tags($this->CleanPricePerBin));
        $this->BinTypeID=htmlspecialchars(strip_tags($this->BinTypeID));

        // bind values
        $stmt->bindParam(":bintypename", $this->BinTypeName);
        $stmt->bindParam(":bintypedescription", $this->BinTypeDescription);
        $stmt->bindParam(":costperbin", $this->CleanPricePerBin);
        $stmt->bindParam(":id", $this->BinTypeID);

    // execute the query
    if($stmt->execute()){
        return true;
    }

    return false;

}

Update_bintype.php contains the following code, a very simple form which calls saved values from the database and supposedly updates any amendments to the database when Save is pressed.

<?php

// get ID of the product to be edited
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: missing ID.');

// include database and object files
include_once 'config/database.php';
include_once 'objects/bintype.php';

// get database connection
$database = new Database();
$db = $database->getConnection();

// prepare objects
$BinType = new BinType($db);

// set ID property of product to be edited
$BinType->BinTypeID = $id;

// read the details of product to be edited
$BinType->readOne();

// set page header
$page_title = "Edit Bin Type";
include_once "layout_header.php";

echo "<div class='right-button-margin'>";
    echo "<a href='view_bintypes.php' class='btn btn-default pull-right'>View Bin Types</a>";
echo "</div>";

?>

<?php
// if the form was submitted
if($_POST){

    // set bin type property values
    $BinType->BinTypeName = $_POST['bintypename'];
    $BinType->BinTypeDescription = $_POST['bintypedescription'];
    $BinType->CleanPricePerBin = $_POST['cleanpricerperbin'];

    // update the bin type
    if($BinType->update()){
        echo "<div class='alert alert-success alert-dismissable'>";
            echo "Bin Type was updated.";
        echo "</div>";
    }

    // if unable to update the bin type, tell the user
    else{
        echo "<div class='alert alert-danger alert-dismissable'>";
            echo "Unable to update Bin Type.";
        echo "</div>";
    }
}
?>


<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
    <table class='table table-hover table-responsive table-bordered'>

                <tr>
            <td>Bin Type Name</td>
            <td><input type='text' name='bintypename' value='<?php echo $BinType->BinTypeName; ?>' class='form-control' /></td>
        </tr>

        <tr>
            <td>Description</td>
            <td><input type='text' name='bintypedescription' value='<?php echo $BinType->BinTypeDescription; ?>' class='form-control' /></td>
        </tr>

        <tr>
            <td>Cost Per Bin</td>
            <td><input type='text' name='costperbin' value='<?php echo $BinType->CleanPricePerBin; ?>' class='form-control' /></td>
        </tr>

        <tr>
            <td></td>
            <td>
                <button type="submit" class="btn btn-primary">Save</button>
            </td>
        </tr>

    </table>
</form>

<?php

// set page footer
include_once "layout_footer.php";
?>

All fields update perfectly except the Cost Per Bin field, which when I submit overwrites the current value with NULL and saves NULL to the database and I can't figure it out for the life of me - PLEASE HELP BEFORE I GO CRAZZZZYYYY!

Thanks so much, Ross

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示