1

I'm updating customer details by fetching the data from the html form. The PHP query for the database is getting executed but changed data is not reflected in my Database table.

PHP code:

Code to fetch data from the form

$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 code:

>    <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>
  • I guess this is the problem: `WHERE customer_id='c_id'` – Dhruv Saxena Apr 01 '17 at 20:38
  • I'm fetching 'c_id' from session variable, this variable is correct I have verified it. –  Apr 01 '17 at 20:43
  • Looking at your comment to the answer below, think you probably mean `$c_id`. As the query stands right now, `c_id` is not a valid PHP variable. Plus, you should consider looking at [MySQLi](http://php.net/manual/en/mysqli.prepare.php) and [PDO](http://php.net/manual/en/pdo.prepared-statements.php) _Prepared Statements_ to prevent your code from being an easy target for [SQL Injection Attacks](http://stackoverflow.com/q/60174/2298301) – Dhruv Saxena Apr 01 '17 at 20:53

1 Answers1

0

Try changing your update query like this:

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

Make sure by debugging all variable values you are getting properly

Mahesh Singh Chouhan
  • 2,558
  • 1
  • 16
  • 26