PHP初学者求助:Base64编解码器无法存储哈希值到数据库
嘿,恭喜你搞定Base64编解码器的核心功能!咱们来解决把用户操作记录存入数据库的问题,顺便提个关键提醒:你用的mysql_*函数早就被PHP官方废弃了(PHP 7及以上版本甚至直接移除了这些函数),安全性和兼容性都没保障,我给你换成现在推荐的mysqli扩展来实现,顺便把存储逻辑加上。
第一步:先创建数据库表
首先你得在base64数据库里创建一个用来存储记录的表,执行下面的SQL语句:
CREATE TABLE IF NOT EXISTS base64_records ( id INT AUTO_INCREMENT PRIMARY KEY, original_text TEXT NOT NULL, encoded_text TEXT, decoded_text TEXT, operation_type ENUM('encode', 'decode') NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
这个表的字段很清晰:
id:自增主键,用来唯一标识每条记录original_text:用户输入的原始内容encoded_text:编码后的结果(解码操作时这个字段为空)decoded_text:解码后的结果(编码操作时这个字段为空)operation_type:记录是编码还是解码操作created_at:自动记录操作时间
第二步:修改你的index.php代码
把原来的代码替换成下面的完整代码,我已经把数据库连接、编解码逻辑和存储记录的功能整合好了:
<?php // 数据库连接信息 $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'base64'; // 使用mysqli连接数据库(替代废弃的mysql_*函数) $dbconn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); // 检查连接是否成功 if (!$dbconn) { die('数据库连接失败: ' . mysqli_connect_error()); } // 处理表单提交 $originalText = ''; $result = ''; $operation = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $originalText = trim($_POST['input_text']); $operation = $_POST['operation']; // 执行Base64编解码 if ($operation === 'encode') { $result = base64_encode($originalText); } elseif ($operation === 'decode') { $result = base64_decode($originalText); } // 将操作记录存入数据库(确保内容不为空时才存储) if (!empty($originalText) && !empty($operation)) { // 使用预处理语句防止SQL注入,这是新手一定要注意的安全点! $stmt = mysqli_prepare($dbconn, "INSERT INTO base64_records (original_text, encoded_text, decoded_text, operation_type) VALUES (?, ?, ?, ?)"); // 绑定参数:'ssss'表示四个字符串类型的参数 $encodedVal = ($operation === 'encode') ? $result : null; $decodedVal = ($operation === 'decode') ? $result : null; mysqli_stmt_bind_param($stmt, 'ssss', $originalText, $encodedVal, $decodedVal, $operation); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); } } ?> <!DOCTYPE html> <html> <head> <title>Base64编解码器</title> <style> .container { max-width: 600px; margin: 2rem auto; padding: 0 1rem; } textarea { width: 100%; height: 150px; margin: 1rem 0; padding: 0.5rem; border: 1px solid #ddd; border-radius: 4px; } .buttons { margin-bottom: 1rem; } button { padding: 0.6rem 1.2rem; margin-right: 0.8rem; border: none; border-radius: 4px; background: #007bff; color: white; cursor: pointer; } button:hover { background: #0056b3; } .result-area { background: #f8f9fa; } </style> </head> <body> <div class="container"> <h1>Base64编解码器</h1> <form method="POST"> <textarea name="input_text" placeholder="请输入要编解码的内容"><?php echo htmlspecialchars($originalText); ?></textarea> <div class="buttons"> <button type="submit" name="operation" value="encode">编码</button> <button type="submit" name="operation" value="decode">解码</button> </div> </form> <?php if (!empty($result)): ?> <h2>操作结果:</h2> <textarea class="result-area" readonly><?php echo htmlspecialchars($result); ?></textarea> <?php endif; ?> </div> </body> </html>
几个重要的细节说明
- 替换废弃的mysql扩展:用
mysqli替代了原来的mysql_*函数,保证代码能在PHP 7+版本正常运行; - 防止SQL注入:用了预处理语句
mysqli_prepare和参数绑定,避免用户输入的恶意内容破坏数据库; - XSS防护:用
htmlspecialchars输出用户输入和结果,防止恶意脚本注入页面; - 逻辑完整性:只有当用户输入内容且选择了操作类型时,才会把记录存入数据库,避免空记录。
你现在只需要先执行SQL创建表,然后替换代码,测试几次编解码操作,再去数据库的base64_records表看看,就能看到所有用户的操作记录啦!如果原来你有自己的HTML样式,也可以把样式部分换成你自己的,核心逻辑不变。
内容的提问来源于stack exchange,提问作者Lavish Sardana
相关产品推荐
相关产品推荐

