本文档旨在指导开发者如何在使用 mysql 数据库的 php 项目中,正确实现并应用百分比保存功能。通过修改现有的 Actions.php 文件,使其与 MySQL 数据库连接并执行数据插入和更新操作,确保数据能够成功保存到数据库中。本文将提供修改后的代码示例,并解释关键步骤和注意事项。
修改 Actions.php 文件
原有的 Actions.php 文件可能存在与 MySQL 数据库交互的问题。为了解决这个问题,需要对该文件进行修改,使其能够正确地连接到 MySQL 数据库,并执行相应的 SQL 查询。
1. 继承 mysqli 类
首先,需要让 Actions 类继承 mysqli 类。这将允许 Actions 类直接使用 mysqli 类的方法来连接和操作 MySQL 数据库。
<?php class Actions extends mysqli{ public $sql; function __construct(){ $this->sql = new mysqli("localhost", "root", "", "resultgrading"); } // ... 其他代码 ... }
代码解释:
立即学习“PHP免费学习笔记(深入)”;
- Class Actions extends mysqli{: 定义了一个名为 Actions 的类,并使其继承自 mysqli 类。
- public $sql;: 声明一个公共属性 $sql,用于存储 mysqli 对象。
- function __construct(){ … }: 构造函数,在创建 Actions 对象时自动调用。
- $this->sql = new mysqli(“localhost”, “root”, “”, “resultgrading”);: 创建一个新的 mysqli 对象,并将其赋值给 $this->sql 属性。这里的参数分别是 MySQL 服务器地址、用户名、密码和数据库名。请根据你的实际情况修改这些参数。
2. 修改 save_percentage() 方法
接下来,需要修改 save_percentage() 方法,使其使用 $this->sql 对象来执行数据库查询。
function save_percentage(){ extract($_POST); $data = ""; foreach($component_id as $k => $v){ if(!empty($data)) $data .= ", "; $data .= "('$id','{$v}','{$percentage[$k]}')"; } if(!empty($data)) $this->sql->query("delete FROM `component_subject_percentage` where `subject_id` = '{$id}'"); $mb = "INSERT INTO `component_subject_percentage` (`subject_id`,`component_id`,`percentage`)VALUES {$data}"; $insert = $this->sql->query($mb); if($insert){ $resp['status'] ='success'; $resp['msg'] = "Data successfully saved"; }else{ $resp['status'] ='failed'; $resp['msg'] = "Data fails to save. Error: ". $this->sql->lastErrorMsg(); $resp['sql'] = $sql; } return JSon_encode($resp); }
代码解释:
立即学习“PHP免费学习笔记(深入)”;
- $this->sql->query(“DELETE FROM …”): 使用 $this->sql 对象执行 DELETE 查询,删除指定 subject_id 的数据。
- $insert = $this->sql->query($mb);: 使用 $this->sql 对象执行 INSERT 查询,插入新的数据。
- $this->sql->lastErrorMsg(): 如果插入失败,使用 $this->sql 对象获取 MySQL 数据库的错误信息。
3. 完整代码示例
下面是修改后的 Actions.php 文件的完整代码示例:
<?php Class Actions extends mysqli{ public $sql; function __construct(){ $this->sql = new mysqli("localhost", "root", "", "resultgrading"); } function save_percentage(){ extract($_POST); $data = ""; foreach($component_id as $k => $v){ if(!empty($data)) $data .= ", "; $data .= "('$id','{$v}','{$percentage[$k]}')"; } if(!empty($data)) $this->sql->query("DELETE FROM `component_subject_percentage` where `subject_id` = '{$id}'"); $mb = "INSERT INTO `component_subject_percentage` (`subject_id`,`component_id`,`percentage`)VALUES {$data}"; $insert = $this->sql->query($mb); if($insert){ $resp['status'] ='success'; $resp['msg'] = "Data successfully saved"; }else{ $resp['status'] ='failed'; $resp['msg'] = "Data fails to save. Error: ". $this->sql->lastErrorMsg(); $resp['sql'] = $sql; } return json_encode($resp); } } $a = isset($_GET['a']) ?$_GET['a'] : ''; $action = new Actions(); switch($a){ case 'save_percentage': echo $action->save_percentage(); break; default: // default action here break; }
注意事项
- 数据库连接信息: 确保在 __construct() 方法中使用的数据库连接信息(服务器地址、用户名、密码、数据库名)与你的 MySQL 数据库配置相匹配。
- SQL 注入: 在构建 SQL 查询时,要特别注意 SQL 注入的风险。使用 mysqli_real_escape_string() 函数或其他参数化查询方法来转义用户输入的数据,以防止恶意代码注入。在这个例子中,extract($_POST) 是一个潜在的风险点,因为它直接将 POST 数据提取为变量。应该谨慎使用,并进行适当的验证和清理。
- 错误处理: 在生产环境中,应该添加更完善的错误处理机制。例如,可以记录错误日志,以便进行调试和问题排查。
- 安全性: 避免将数据库密码硬编码在代码中。使用环境变量或配置文件来存储敏感信息。
总结
通过以上步骤,你可以修改 Actions.php 文件,使其能够正确地与 MySQL 数据库交互,并实现百分比保存功能。请务必注意代码中的注释和注意事项,以确保代码的正确性和安全性。在实际应用中,还需要根据具体的需求进行适当的调整和优化。
评论(已关闭)
评论已关闭