简体   繁体   中英

Update database from user input php

I have a database with a table called users, one of the columns of this table is called depot_location. A user is presented with this if they want to change the location.

<?php include("includes/header.php") ?>

    <div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3">
        <div class="alert-placeholder">

            <?php display_message(); ?>

            <?php update_depot(); ?>

        </div>
    </div>
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-login">
                <div class="panel-heading">
                    <div class="row">

                        <div class="col-xs-12">
                            <h3>Update Depot Location</h3>
                        </div>
                    </div>
                    <hr>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-12">
                            <form id="register-form" method="post" role="form" >
                                <div class="form-group">
                                    <select name="depot_location" id="depot_location" tabindex="1" class="form-control" value="" required>
                                        <option value="" disabled="disabled" selected="selected">Please select a drop-off location</option>
                                        <option value="Cork">Cork</option>
                                        <option value="Waterford">Waterford</option>
                                        <option value="Limerick">Limerick</option>
                                        <option value="Dublin">Dublin</option>
                                        <option value="Galway">Galway</option>
                                    </select>
                                </div>
                                    <div class="row">
                                        <div class="col-sm-6 col-sm-offset-3">
                                            <input type="submit" name="reset-password-submit" id="reset-password-submit" tabindex="4" class="form-control btn btn-register" value="Reset Password">
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

So they select the new location from the drop down menu and the idea is it updates the database. This is my php code but it doesnt work.

/****************************Update Depot Location Function********************************/

function update_depot() {

 if(isset($_GET['email'])) {

     if($_POST['depot_location']) {

            $updated_depot_location = ($_POST['depot_location']);
            $sql = "UPDATE users SET depot_location = '".escape($updated_depot_location)."', WHERE email = '".escape($_GET['email'])."'";
            query($sql);

            set_message("<p class='bg-success text-center'>Your depot location has been updated.</p>");

            redirect("change.php");

        } else {

            echo validation_errors("Could not change depot location please try again.");

        } 

 }

}

When the submit button is clicked nothing happens it just seems to refresh the page. I have no idea why its not working.

i see a comma in your update sql

$sql = "UPDATE users SET depot_location = '".escape($updated_depot_location)."', WHERE email = '".escape($_GET['email'])."'";

remove the comma and try again

$sql = "UPDATE users SET depot_location = '".escape($updated_depot_location)."' WHERE email = '".escape($_GET['email'])."'";

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