为什么PHP中执行MySQL更新操作无法正常完成?
问题描述
我尝试更新数据库中的某张数据表,以表单先行输入的邮箱作为检索条件定位待更新行,再填写其余待更新数据。但操作执行后抛出错误,我暂未找到问题根源。当前站点使用的组件如下:
- 数据采集页面
- PHP数据库连接脚本
- 更新操作处理PHP脚本
- MySQL数据库
接下来展示完整流程和对应代码片段:
数据录入页面界面

表单代码
<label>Localidad</label> <input type="text" name="labelLocalidad1" required/> </div> <div class="form-element"> <label>Direccion</label> <input type="text" name="labelDireccion1" required/> </div> <div class="form-element"> <label>Codigo Postal</label> <input type="text" name="labelPostal1" pattern="[a-zA-Z0-9]+" required/> </div> <div class="form-element"> <label>Providencia</label> <input type="text" name="labelProvidencia1" pattern="[a-zA-Z0-9]+" required/> </div> <div class="form-element"> <label>Numero de telefono</label> <input type="number" name="labeltelefono1" pattern="[a-zA-Z0-9]+" required/> </div> <button type="submit" name="PAGO" value="PAGO">Registrar direccion</button> </form>
数据库连接代码
<?php $host = "localhost"; $user = "root"; $clave = ""; $bd = "usuarios"; $conectar = mysqli_connect($host,$user,$clave,$bd); ?>
更新处理PHP代码
<?php require 'conexion.php'; $idusuario= session_id(); $localidad = $_POST['labelLocalidad1']; $direccion = $_POST['labelDireccion1']; $postal = $_POST['labelPostal1']; $providencia = $_POST['labelProvidencia1']; $telefono = $_POST['labeltelefono1']; $correoentrega = $_POST['labelcorreo1']; $actualizar =("UPDATE datosentrega set localidad='$localidad',direccion='$direccion',postal=$postal,providencia='$providencia',telefono='$telefono' WHERE correoentrega='$correoentrega'"); $query = mysqli_query($conectar,$actualizar); if($query){ echo "<script> alert('Datos registrados'); </script>"; }else{ echo "<script> alert('Error favor de revisar el codigo XD'); </script>"; }
问题排查及修复方案
1. 表单缺失邮箱输入项
更新SQL中使用$correoentrega = $_POST['labelcorreo1']作为检索条件,但给出的表单代码里没有对应name="labelcorreo1"的输入框,导致$correoentrega为空,WHERE条件匹配不到任何行,或者直接触发SQL语法错误。
修复方法:在表单中补充邮箱输入项:
<div class="form-element"> <label>Correo de entrega</label> <input type="email" name="labelcorreo1" required/> </div>
2. postal字段SQL语法错误
SQL语句中postal=$postal没有加单引号,而表单中postal的类型是text,允许包含字母,不带引号会直接触发语法报错。如果postal字段在数据库中是字符串类型,修改为:
// 原代码:postal=$postal // 修改为:postal='$postal' $actualizar =("UPDATE datosentrega set localidad='$localidad',direccion='$direccion',postal='$postal',providencia='$providencia',telefono='$telefono' WHERE correoentrega='$correoentrega'");
3. 未开启session导致session_id()无效
代码中调用了session_id()但没有提前执行session_start(),如果后续有依赖session的逻辑会失效,修复方法:在php代码开头加session_start();。
4. 高危SQL注入风险
当前直接拼接用户输入到SQL语句,存在严重的注入漏洞,建议替换为预处理语句:
$stmt = mysqli_prepare($conectar, "UPDATE datosentrega set localidad=?,direccion=?,postal=?,providencia=?,telefono=? WHERE correoentrega=?"); mysqli_stmt_bind_param($stmt, "ssssss", $localidad, $direccion, $postal, $providencia, $telefono, $correoentrega); $query = mysqli_stmt_execute($stmt);
5. 调试建议
执行出错时可以打印具体报错信息定位问题,修改错误分支的代码:
else{ echo "<script> alert('错误:".mysqli_error($conectar)."'); </script>"; }
内容的提问来源于stack exchange,提问作者Irving Orona
相关产品推荐
相关产品推荐

