title: How do I update a row of data in mysql using PHP?
body: <p>I'm attempting to create a form where the user can update their profile details but it just doesn't seem to work.</p>
  
  <p>I'm quite the beginner in server side programming so I'm piecing together code from different tutorials viz. from <a href="http://www.codingcage.com/2015/04/php-login-and-registration-script-with.html" rel="nofollow noreferrer">http://www.codingcage.com/2015/04/php-login-and-registration-script-with.html</a></p>
  
  <p>The <em>class.user.php</em> file, which originally only had the code for login, and signup. I copied the signup function and changed some stuff to update instead:</p>
  
  <pre><code>public function update($id,$uname,$umob,$uaddr,$uacc,$upass) {
      try {
          $upass = password_hash($upass, PASSWORD_DEFAULT);
  
          $stmt = $this-&gt;conn-&gt;prepare(
              "UPDATE users
                  SET
                      id       = :id, 
                      name     = :uname, 
                      mobile   = :umob,
                      address  = :uaddr,
                      accNo    = :uacc, 
                      password = :upass
                WHERE id = :id"
          );
  
          $stmt-&gt;bindParam(":id", $id);
          $stmt-&gt;bindParam(":upass", $upass);
          $stmt-&gt;bindParam(":uacc", $uacc);                                         
          $stmt-&gt;bindParam(":uname", $uname);
          $stmt-&gt;bindParam(":uaddr", $uaddr); 
          $stmt-&gt;bindParam(":umob", $umob);
  
          $stmt-&gt;execute();   
  
          return $stmt;   
      }
      catch(PDOException $e) {
          echo $e-&gt;getMessage();
      }               
  }
  </code></pre>
  
  <p>and in <em>view_account.php</em>: (edit 3, whole file including code corrections by @e_i_pi):</p>
  
  <pre><code>&lt;?php
  ini_set("error_log", "/path/to/error.log");
      require_once("session.php");
  
      require_once("class.user.php");
  
      $auth_user = new USER();
  
      $stmt = $auth_user-&gt;runQuery("SELECT * FROM users WHERE consumer-no=:cno");
  
      $userRow = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
  
      if(!$session-&gt;is_loggedin()){
          // session no set redirects to login page
          $session-&gt;redirect('index.php');
      }
  
      if(isset($_POST['submit']) &amp;&amp; $_POST['submit'] === 'save') {
          $uname = strip_tags($_POST['full-name']);
          $umob = strip_tags($_POST['mobile']);
          $uaddr = strip_tags($_POST['addr']);
          $uacc = strip_tags($_POST['bank-acc']);
          $id = strip_tags($_POST['id']);
          $upass = strip_tags($_POST['password']);
  
  
          if($uname=="") {
              $signuperror[] = "Please Enter Your Full Name!";    
          }
          else if($umob=="")  {
              $signuperror[] = "Please Enter Your Mobile No.!";   
          }
          else if($uaddr=="") {
              $signuperror[] = 'Please Enter Your Address!';
          }
          else if($upass=="") {
              $signuperror[] = "Please Enter a Password!";
          }
          else if(strlen($upass) &lt; 6) {
              $signuperror[] = "Password must be atleast 6 characters";   
          }
          else {
              try {
  // I commented out these for some weird reason I can't even rememebr
  //                $stmt = $auth_user-&gt;runQuery("SELECT id FROM users WHERE id=:id");
  //                $stmt-&gt;execute(array(':id'=&gt;$id));
  //                $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
                  $auth_user-&gt;update($id,$uname,$umob,$uaddr,$uacc,$upass);
              }
              catch(PDOException $e) {
                  echo $e-&gt;getMessage();
              }
          }   
      }
  
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Gas Booking&lt;/title&gt;
      &lt;link rel="stylesheet" href="style.css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;header&gt;
          &lt;h1&gt;gas booking&lt;/h1&gt;
          &lt;nav&gt;
              &lt;ul&gt;
                  &lt;li&gt;&lt;a href="index.php"&gt;home&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="booking.php"&gt;booking&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="payment.php"&gt;payment&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="ticket.php"&gt;ticket&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="view_account.php"&gt;view account&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="user-bank.php"&gt;bank&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="logout.php?logout=true"&gt;logout&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/nav&gt;
      &lt;/header&gt;
      &lt;div class="content"&gt;
          &lt;h2&gt;Edit Your Profile Details&lt;/h2&gt;
  
          &lt;form method="post" action="view_account.php"&gt;
              &lt;input type="hidden" id="id" name="id" value="&lt;?php echo $_SESSION['id']; ?&gt;"&gt;
              &lt;label for="full-name" class="input-info"&gt;
                  &lt;div class="label"&gt;full name&lt;/div&gt;
                  &lt;input type="text" id="full-name" name="full-name" value="&lt;?php echo $_SESSION['name']; ?&gt;"&gt;
              &lt;/label&gt;
              &lt;label for="mobile" class="input-info"&gt;
                  &lt;div class="label"&gt;mobile number&lt;/div&gt;
                  &lt;input type="text" id="mobile" name="mobile" value="&lt;?php echo $_SESSION['mob']; ?&gt;"&gt;
              &lt;/label&gt;
              &lt;label for="addr" class="input-info"&gt;
                  &lt;div class="label"&gt;address&lt;/div&gt;
                  &lt;input id="addr" name="addr" value="&lt;?php echo $_SESSION['addr']; ?&gt;"&gt;
              &lt;/label&gt;
              &lt;label for="bank-acc" class="input-info"&gt;
                  &lt;div class="label"&gt;bank account number&lt;/div&gt;
                  &lt;input type="text" id="bank-acc" name="bank-acc" value="&lt;?php echo $_SESSION['accNo']; ?&gt;"&gt;
              &lt;/label&gt;
              &lt;hr&gt;
              &lt;label for="password" class="input-info"&gt;
                  &lt;div class="label"&gt;password&lt;/div&gt;
                  &lt;input type="password" id="password" name="password"&gt;
              &lt;/label&gt;
              &lt;button type="submit" name="submit" value="save"&gt;
                  Save Changes
              &lt;/button&gt;
          &lt;/form&gt;     
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and my table is as follows:</p>
  
  <pre><code>--
  -- Table structure for table `users`
  --
  
  CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `consumerNo` varchar(15) NOT NULL,
    `password` varchar(255) NOT NULL,
    `accNo` varchar(255) NOT NULL,
    `name` varchar(255) NOT NULL,
    `address` varchar(255) NOT NULL,
    `mobile` bigint(10) NOT NULL,
    `balance` bigint(10) NOT NULL,
    `joining_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  </code></pre>
  
  <p>I'm sure I've done something stupid. I'd really appreciate pointing me in the right direction, I sat with it till 5:00am and am feeling frustrated with myself.</p>
  
  <p>The connection with the db is working, classes are properly included. Let me know if you need more information. Thank you!</p>
  
  <p>The project can be downloaded here: <a href="https://www.dropbox.com/s/9v69m18l82n1t46/gas.zip?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/9v69m18l82n1t46/gas.zip?dl=0</a>. Warning the code's kind of a mess.</p>