【问题标题】:Update query only make the database table field empty更新查询只使数据库表字段为空
【发布时间】:2017-08-13 18:50:00
【问题描述】:

我使用 PHP OOP 进行了更新查询。基本上这里是itemedit.php 页面,用户可以在其中更新数据库表字段:

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

如果您想了解这背后的理论,请参阅我的另一个问题 here...

然后我在这里编写了代码以更新该自定义字段:

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();
            }
        }

以下是该格式文件的操作:

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>
    ";

它工作正常,但问题是它只使 MySQL 数据库中的表字段为空且由于某些原因为空..

所以如果你知道如何解决这个问题,请告诉我我真的很感激?

【问题讨论】:

  • 您如何/何时调用此函数EditMenuItem
  • 抱歉忘记写了,再看问题

标签: php mysql oop sql-update


【解决方案1】:

这是因为在第二次迭代中,当用户选择是或否时,表单没有 $_POST['e'] 的值。

改一下

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

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

【讨论】:

    猜你喜欢
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多