【问题标题】:PHP query for Database update executing successfully but values are not getting updated on database table数据库更新的PHP查询成功执行,但数据库表上的值未更新
【发布时间】:2017-08-26 23:13:30
【问题描述】:

我正在通过从 html 表单中获取数据来更新客户详细信息。正在执行数据库的 PHP 查询,但更改的数据未反映在我的数据库表中。

PHP 代码:

从表单中获取数据的代码

$con = mysqli_connect("localhost","root","","ecommerce");
    if(isset($_POST['savec']))
    {
        global $con;

        $name=$_POST['name'];
        $phone=$_POST['phone'];
        $email=$_POST['email'];
        $passm=$_POST['password'];
        $pass=md5($passm);
        $cust_query="UPDATE er_customer SET customer_name='$name',customer_phone='$phone',customer_email='$email',customer_password='$pass' WHERE customer_id='c_id'";
        $excecute=mysqli_query($con,$cust_query);

        if($excecute)
        {
            echo "<script> alert('Account changes is successfull.') </script>";
        }
    }

表格代码:

>    <form method="post" class="registration-form"
> action="account_settings.php" id="form_login"
> enctype="multipart/form-data">
>                       
>                   <div class="form-group">
>                         <label>Name</label>
>                            <input class="form-control"  name="name" type="text" value="<?php echo"$c_name"; ?>" required>                 
> 
>                     </div>         
>                   
>                   <div class="form-group">
>                         <label>Phone</label>
>                            <input class="form-control" name="phone" type="text" value="<?php echo"$c_phone"; ?>" required>                
> 
>                     </div>
>                   
>                   
>                   <div class="form-group">
>                         <label>Email</label>
>                            <input class="form-control" name="email" type="text" value="<?php echo"$c_email"; ?>" required>                
> 
>                     </div>
>               
>                   
>                   <div class="form-group">
>                         <label>Password</label>
>                            <input class="form-control" name="password" type="text" value="" required>                       
>                     </div>
>               
>                   </br>
>                   
>                   <div class="form-group">
>                        
>                            <input type="submit" class="col-sm-3 btn btn-success" name="savec" value="Save changes" align="center" />      
> 
>                     </div>
>               
>               
>               </form>

【问题讨论】:

  • 我猜这是问题所在:WHERE customer_id='c_id'
  • 我从会话变量中获取'c_id',这个变量是正确的,我已经验证过了。
  • 查看您对以下答案的评论,认为您的意思可能是$c_id。就目前的查询而言,c_id 不是有效的 PHP 变量。另外,您应该考虑查看MySQLiPDO Prepared Statements,以防止您的代码成为SQL Injection Attacks 的简单目标

标签: php mysql


【解决方案1】:

尝试像这样更改您的更新查询:

$cust_query="UPDATE er_customer SET customer_name=$name,customer_phone=$phone,customer_email=$email,customer_password=$pass WHERE customer_id=$c_id";

通过调试所有变量值来确保你得到正确

【讨论】:

  • 您可以将此答案标记为有用,如果它对您有用,以便它帮助其他面临同样问题的人。谢谢
猜你喜欢
  • 1970-01-01
  • 2023-01-14
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多