【问题标题】:Why is this update in MySQL not done correctly in PHP?为什么 MySQL 中的这个更新在 PHP 中没有正确完成?
【发布时间】:2021-12-20 12:01:39
【问题描述】:

我正在尝试更新我的数据库中的一张表。为了确定我要更新的行,我使用我在表单中输入的电子邮件作为搜索的基础,然后请求我输入要更新的其余数据。问题是执行操作后它会抛出一个错误,我一直无法找到我的错误。在这个网站上我使用:

我请求数据的页面。
一个 PHP 连接。
处理更新的 PHP。
数据库。
接下来我将说明过程,我将有一个代码sn -p

我请求和输入数据的屏幕。

表单代码

<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>";
}

【问题讨论】:

标签: php html mysql


【解决方案1】:

首先,变量 $correoentrega 赋值错误,$_POST 数组中没有名为“labelcorreo1”的索引,这意味着你的代码是错误的:

$correoentrega = $_POST['labelcorreo1'];

您应该通过使用正确的索引或将输入标签添加到与索引使用的名称相同的表单来更正它。

其次,为了让代码更安全,远离错误,使用prepare函数,它可以让你从数据中分离出sql语句,也可以让你的代码更容易被未来的改进或更改,重要的是准备函数将保护您的数据库免受 SQL 注入。

要了解如何使用准备功能,请参阅:using bind_param with mysqli_query

有一个使用prepare的小例子:

$ = $mysqli->prepare("SELECT * FROM myTable WHERE name = ? AND age = ?");
$stmt->bind_param("si", $_POST['name'], $_POST['age']);
$stmt->execute();
$stmt->close();

这是怎么用的,你只是一个问号而不是直接对接值,然后通过使用bind_param()函数来添加值,但是值的顺序与sql语句中的问号相同,第一个参数是确定您将使用的值的数据类型。 要了解更多信息,请参阅: https://www.php.net/manual/en/mysqli-stmt.bind-param.php

https://www.w3schools.com/php/php_mysql_prepared_statements.asp

【讨论】:

  • @irving 有没有解决您的问题的答案!?我们可以将这些答案与其他类似问题分享吗?
【解决方案2】:

您没有提供 PHP 代码中定义的字段

$correoentrega  = $_POST['labelcorreo1'];

确保该字段在帖子表单中可用,即

<input type='text' name='labelcorreo1' value='luzma@email.com'>

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 2018-04-22
    • 2020-07-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 2011-04-16
    相关资源
    最近更新 更多