php - 这个数据库查询有什么问题?

标签 php mysql sql post

这是一小段 PHP 代码。

<?php include_once("includes/connection.php"); ?>
    <?php include_once("includes/functions.php"); ?>
    <?php 
        if(!isset($_POST['menu_name']) or !isset($_POST['position']) or !isset($_POST['visible'])){
            echo "You are supposed to fill all entries<br/><br/><a href='new_subject.php'>go back</a>";
            exit;
        }
            echo "<pre>";
            var_dump($_POST); 
            echo "</pre>":
        $menu_name = $_POST['menu_name'];
        $position = (int)$_POST['position'];
        $visible = $_POST['visible'];
        $menu_name = mysql_real_escape_string($menu_name);
        $subject_count = mysql_num_rows(get_all_subjects());
        //$subject_set_desc = mysql_query("SELECT position FROM subjects ORDER by position DESC",$connection);
        for($change = $position;$change<=$subject_count;$change++){
            //get row with position $change
            //increase it's position by 1
            //continue till all rows end OR loop ends
            /*UPDATE subjects SET position = $change+1 WHERE position = $change*/
            $new_position = $change+1;
            echo "{$change} ".gettype($change)."  {$new_position} ".gettype($new_position)."<br/> ";
            mysql_query("UPDATE subjects SET position = {$new_position} WHERE position = {$change}",$connection);
            echo "UPDATE subjects SET position = {$new_position} WHERE position = {$change}"."<br/>";
            //confirm_query($cheack);
            
        }
        $query = "INSERT INTO subjects (menu_name,position,visible) VALUES ('{$menu_name}',{$position},{$visible})";
        if(mysql_query($query,$connection)){
            //header("location:content.php");
            exit;
        } else{
            echo "<b>failed to insert new subject</b> <br/>".mysql_error();
        }
    ?>
    <?php mysql_close($connection); ?>

我希望您只关注 for循环以及该循环中的数据库查询.. 我通过post发送以下数据到此页面

$_POST['subject'] = 'new_subject'; //string type <br/>
$_POST['position'] = 1 //int type <br/>
$_POST['visible'] = 1 //tinyint type <br/>
$_POST['submit'] = 'Add Subject' // string type just submisssion <br/>

正如您在我的表单处理文件中看到的

enter image description here

进行这些查询之前我的数据库

enter image description here

进行查询后我的数据库(在代码的 for 循环中)。

enter image description here

我认为表中所有行的位置应该增加 1,但全部更改为 5.. 这里我想为那些位置大于或等于 $_POST['position']..<br/> 的位置增加 1 我尝试回显我的查询,所有查询都显示查询是正确的,但执行后对表的更改并不像我想象的那样(我想将每个位置增加 1)..

最佳答案

你应该向下运行;首先将位置 1 更新为位置 2。现在有 2 个项目的位置为 2。然后将其更新为 3。在位置 3 上留下三件元素。

相反,从最高值开始,将 4 更新为 5。然后删除一个,将所有 3 增加到 4 等。

或者更好的是,对所有需要递增的位置使用一个查询,而不需要任何循环:

Update subjects SET position = position +1

关于php - 这个数据库查询有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19401215/

相关文章:

php - 租车 - 如何检查两个日期之间是否有车可用?

mysql - 从选择结果更新我的数据库

mysql - 查询 - 向用户发送公共(public)和私有(private)消息

php - 我怎样才能更好地显示我的数据。我有问题

PHP socket_listen() 不工作

php - 为什么 session 不能被取消设置和销毁?

sql - 每分钟计算两列的行数

javascript - EXTJS CSRF 保护

mysql - 无法连接到数据库

php - CakePHP 问题 : Unexpected call to database