user8454964
user8454964

Reputation:

Update query only make the database table field empty

I have made an Update query with PHP OOP. Basically here is the itemedit.php page where users can update the database table fields:

    <form action='itemedit.php?i=$i' method='POST'>
                                    <td>$i</td>
                                    <td><input type='text' name='e' value='".$menuSet->$func()."'/></td>
                                    <td>
                                        <input type='submit' value='Edit'/>&nbsp; 
                                        <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a>
                                    </td>
                                    </tr>
                                    </form>

If you wanna know about the theory behind this see my other question here...

And then here I have coded this for updating that custom field:

public function EditMenuItem($i,$e)
        {
            if(!empty($i))
            {
                $adm = $this->db->prepare("UPDATE menu_nav SET menu_link_$i = ?");
                $adm->bindParam(1, $e, PDO::PARAM_STR);
                $adm->execute();
            }
            else
            {
                header("Location: php/includes/errors/012.php");
                exit();
            }
        }

And here is the action of file of that form:

if (isset($_GET['i'])){
    $i = $_GET['i'];
    $e = $_POST['e'];
    $editItem = new Menus();

    if(isset($_POST['yes'])){
        $editItem->EditMenuItem($i,$e);
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=menus.php">';   
        exit; 
    }
    if(isset($_POST['no'])){
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=menus.php">';    
        exit; 
    }

    echo "
    <div class='content-wrapper'>
        <section class='content-header'>
            <h1>
                Editing Menu Item
                <small></small>
            </h1>
            <ol class='breadcrumb'>
                <li class='active'>itemedit.php</li>
            </ol>
        </section>
        <section class='content' style='text-align:center;'>
            <form action='' method='POST'>
                <h5><strong> 
                    Are you sure you want to change the selected item ?</br></br>
                </strong></h5>   
                <p> 
                    <button name='yes' type='submit' class='btn btn-primary'>Yes</button>
                    <button name='no' type='submit' class='btn'>No</button>
                </p> 
            </form> 
        </section>
    </div>
    ";

And it works fine but the problem is it ONLY make the table field in MySQL database empty and null for some reasons..

So if you know how to solve this, please let me know I really appreciate it ?

Upvotes: 1

Views: 105

Answers (1)

Amit Joshi
Amit Joshi

Reputation: 1354

This is because on the 2nd iteration when users select yes or no, the form doesnt have values for the $_POST['e'].

Change this

<p>
<button name='yes' type='submit' class='btn btn-primary'>Yes</button>
<button name='no' type='submit' class='btn'>No</button>
</p> 

to

<p>
<input type='hidden' name='e' value='<?php echo $e;?>'>
<button name='yes' type='submit' class='btn btn-primary'>Yes</button>
<button name='no' type='submit' class='btn'>No</button>
</p> 

Upvotes: 1

Related Questions