php - 更新查询只使数据库表字段为空

标签 php mysql oop sql-update

我已经使用 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 数据库中的表字段为空和 null..

因此,如果您知道如何解决这个问题,请告诉我,我真的很感激?

最佳答案

这是因为在第二次迭代中,当用户选择是或否时,表单没有 $_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> 

关于php - 更新查询只使数据库表字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45663794/

相关文章:

php - 如何在 Laravel 中检索 FormData

mysql - GROUP BY 多个表 MYSQL

c# - 如何使用反射和抽象对象动态加载类?

oop - 状态模式和策略模式的区别

PHPExcel 删除单元格(列)removeColumn() 不起作用

php - 关于通过 mysqli 使用更新

Java : error in SQL syntax

oop - 在哪里放置处理两个类之间关系的方法?

php - 使用 PHP 将 JSON 数据插入 MySQL

mysql - 使用带有 'multi=True' 参数的execute() 函数创建时出现“表不存在”