【问题标题】:MYSQL Update doesn't work consistentlyMYSQL 更新无法始终如一地工作
【发布时间】:2015-05-03 16:07:10
【问题描述】:

我在后端添加了一个表格,其中包含课程列表。当管理员点击编辑课程时,他可以编辑它们,并且在点击更新时,变量会在 mysql 数据库中更新。

问题是更新似乎适用于所有课程,其中课程信息几乎相同。它似乎只工作了一半的时间。我想知道是否有解决以下问题的方法:

<?php

//getting the text data from the fields
    if(isset($_POST['update_course'])) {
        $update_id          =   $pro_id;
        $course_provider    =   $_POST['course_provider'];
        $course_price       =   $_POST['course_price'];
        $course_desc        =   $_POST['course_desc'];
        $course_sdesc       =   $_POST['course_sdesc'];
        $course_editor      =   $_POST['course_editor'];
        $course_prereq      =   $_POST['course_prereq'];
        $course_tax         =   $_POST['course_tax'];

        $update_course      =   "update courses set course_updated='<b>$course_editor</b>', course_priceFinal=$course_tax*$course_price, course_tax='$course_tax', course_prereq='$course_prereq', course_sdesc='$course_sdesc'
        where course_id2 ='$update_id'";
        $run_course = mysqli_query($con, $update_course);

        if($run_course) {
            echo "<script>window.open('index.php?view_course','_self')</script>";
        }
        else {
            echo "<script>window.open('index.php?view_course','_self')</script>";
        }
    } ?>

非常感谢任何帮助。

【问题讨论】:

  • 您是如何获得$pro_id 的?
  • 这就是 $row_pro=mysqli_fetch_array($run_pro); $pro_id = $row_pro['course_id2'];谢谢
  • 您不应该只处理原始 POST 数据,这是一个安全问题。此外,您不能确保这些值不是空白或 NULL,因此这也可能是一个问题。
  • 你们的价格结构是怎样的?你用浮动吗?你的数据库是为它设置的吗?它接受多少位小数?输入字段是否限制了这些值?你只是截断多余的数字吗?

标签: javascript php mysql html mysqli


【解决方案1】:

尝试将您的语句更改为:

$update_course = update courses set course_updated='".$course_editor."',
course_priceFinal='".$course_tax*$course_price."', 
course_tax='".$course_tax."', course_prereq='".$course_prereq."', 
course_sdesc='".$course_sdesc."' where course_id2 ='".$update_id."'

【讨论】:

  • 感谢您的建议,我不需要在“更新课程.....”之类的更新课程周围加上引号
  • 是的..你可以说... $update_course = "更新课程设置 course_updated='".$course_editor."', course_priceFinal='".$course_tax*$course_price."', course_tax='".$course_tax."', course_prereq='".$course_prereq."', course_sdesc='".$course_sdesc."' where course_id2 ='".$update_id."'"
猜你喜欢
  • 2013-02-26
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多