Guest User

MySQL Syntax error

a guest
Jul 18th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.79 KB | None | 0 0
  1. <?php
  2.  
  3.     // First we execute our common code to connection to the database and start the session
  4.     require("commonlogin.php");
  5.      
  6.     // At the top of the page we check to see whether the user is logged in or not
  7.     if(empty($_SESSION['user']))
  8.     {
  9.         // If they are not, we redirect them to the login page.
  10.         header("Location: login.php");
  11.          
  12.         // Remember that this die statement is absolutely critical.  Without it,
  13.         // people can view your members-only content without logging in.
  14.         die("Redirecting to login.php");
  15.     }
  16.      
  17.     // Everything below this point in the file is secured by the login system
  18.      
  19.     // We can display the user's username to them by reading it from the session array.  Remember that because
  20.     // a username is user submitted content we must use htmlentities on it before displaying it to the user.
  21. // This if statement checks to determine whether the edit form has been submitted
  22.     // If it has, then the account updating code is run, otherwise the form is displayed
  23.     if(!empty($_POST))
  24.     {
  25.     //START WEIRD EMAIL CHECKING
  26.         // Make sure the user entered a valid E-Mail address
  27.         if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
  28.         {
  29.             die("Invalid E-Mail Address");
  30.         }
  31.          
  32.         // If the user is changing their E-Mail address, we need to make sure that
  33.         // the new value does not conflict with a value that is already in the system.
  34.         // If the user is not changing their E-Mail address this check is not needed.
  35.         if($_POST['email'] != $_SESSION['user']['email'])
  36.         {
  37.             // Define our SQL query
  38.             $query = "
  39.                SELECT
  40.                    1
  41.                FROM lfs
  42.                WHERE
  43.                    email = :email
  44.            ";
  45.              
  46.             // Define our query parameter values
  47.             $query_params = array(
  48.                 ':email' => $_POST['email']
  49.             );
  50.              
  51.             try
  52.             {
  53.                 // Execute the query
  54.                 $stmt = $db->prepare($query);
  55.                 $result = $stmt->execute($query_params);
  56.             }
  57.             catch(PDOException $ex)
  58.             {
  59.                 // Note: On a production website, you should not output $ex->getMessage().
  60.                 // It may provide an attacker with helpful information about your code.  
  61.                 die("Failed to run query: " . $ex->getMessage());
  62.             }
  63.              
  64.             // Retrieve results (if any)
  65.             $row = $stmt->fetch();
  66.             if($row)
  67.             {
  68.                 die("This E-Mail address is already in use");
  69.             }
  70.         }
  71.          
  72.          //END WEIRD EMAIL CHECKING
  73.          
  74.          
  75.         // If the user entered a new password, we need to hash it and generate a fresh salt
  76.         // for good measure.
  77.         if(!empty($_POST['password']))
  78.         {
  79.             $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
  80.             $password = hash('sha256', $_POST['password'] . $salt);
  81.             for($round = 0; $round < 65536; $round++)
  82.             {
  83.                 $password = hash('sha256', $password . $salt);
  84.             }
  85.         }
  86.         else
  87.         {
  88.             // If the user did not enter a new password we will not update their old one.
  89.             $password = null;
  90.             $salt = null;
  91.         }
  92.          
  93.         // Initial query parameter values
  94.         $query_params = array(
  95.         ':user_id' => $_SESSION['user']['id'],
  96.             ':email' => $_POST['email'],           
  97.            
  98.         );
  99.          if($_POST['scrim'] !== null)
  100.         {
  101.             $query_params[':scrim'] = $_POST['scrim'];
  102.         }
  103.         // If the user is changing their password, then we need parameter values
  104.         // for the new password hash and salt too.
  105.         if($password !== null)
  106.         {
  107.             $query_params[':password'] = $password;
  108.             $query_params[':salt'] = $salt;
  109.         }
  110.          
  111.         // Note how this is only first half of the necessary update query.  We will dynamically
  112.         // construct the rest of it depending on whether or not the user is changing
  113.         // their password.
  114.         $query = "
  115.            UPDATE lfs
  116.            SET
  117.                email = :email ,
  118.                
  119.        ";
  120.          if($_POST['scrim'] !== null)
  121.         {
  122.             $query .= "
  123.                scrim = :scrim ,
  124.            ";
  125.         }
  126.         // If the user is changing their password, then we extend the SQL query
  127.         // to include the password and salt columns and parameter tokens too.
  128.         if($password !== null)
  129.         {
  130.             $query .= "
  131.                password = :password ,
  132.                 salt = :salt ,
  133.            ";
  134.         }
  135.          
  136.         // Finally we finish the update query by specifying that we only wish
  137.         // to update the one record with for the current user.
  138.         $query .= "
  139.            WHERE
  140.                id = :user_id ;
  141.        ";
  142.          
  143.         try
  144.         {
  145.             // Execute the query
  146.             $stmt = $db->prepare($query);
  147.             $result = $stmt->execute($query_params);
  148.         }
  149.         catch(PDOException $ex)
  150.         {
  151.             // Note: On a production website, you should not output $ex->getMessage().
  152.             // It may provide an attacker with helpful information about your code.  
  153.             die("Failed to run query: " . $ex->getMessage());
  154.         }
  155.          
  156.         // Now that the user's E-Mail address has changed, the data stored in the $_SESSION
  157.         // array is stale; we need to update it so that it is accurate.
  158.         $_SESSION['user']['email'] = $_POST['email'];
  159.          
  160.         // This redirects the user back to the members-only page after they register
  161.         header("Location: private.php");
  162.          
  163.         // Calling die or exit after performing a redirect using the header function
  164.         // is critical.  The rest of your PHP script will continue to execute and
  165.         // will be sent to the user if you do not die or exit.
  166.         die("Redirecting to private.php");
  167.     }
  168.      
  169. ?>  
  170. <!DOCTYPE html>
  171. <html lang="en">
  172.  
  173. <head>
  174.     <meta charset="utf-8">
  175.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  176.     <meta name="description" content="">
  177.     <meta name="author" content="">
  178.  
  179.     <title>ScrimNow - Account</title>
  180.  
  181.     <!-- Bootstrap core CSS -->
  182.     <link href="css/bootstrap.css" rel="stylesheet">
  183.     <link href="css/stylesheet.css" rel="stylesheet">
  184.  
  185.     <!-- Add custom CSS here -->
  186.     <style>
  187.     body {
  188.         margin-top: 60px;
  189.     }
  190.     </style>
  191.  
  192. </head>
  193.  
  194. <body>
  195.  
  196.     <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
  197.         <div class="container">
  198.             <div class="navbar-header">
  199.                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
  200.                     <span class="sr-only">Toggle navigation</span>
  201.                     <span class="icon-bar"></span>
  202.                     <span class="icon-bar"></span>
  203.                     <span class="icon-bar"></span>
  204.                 </button>
  205.                 <a class="navbar-brand" href="index.php">ScrimNow</a>
  206.             </div>
  207.  
  208.             <!-- Collect the nav links, forms, and other content for toggling -->
  209.             <div class="collapse navbar-collapse navbar-ex1-collapse">
  210.                 <ul class="nav navbar-nav">
  211.                 <li><a href="index.php">Home</a>
  212.                     </li>
  213.                     <li><a href="lfs.php">LFS</a>
  214.                     </li>
  215.                     </ul>
  216.                     <ul class="nav navbar-nav navbar-right">
  217.                     <li class="dropdown">
  218.           <a href="#" class="dropdown-toggle" data-toggle="dropdown"><?
  219.           if(empty($_SESSION['user']))
  220.     {
  221.         // If they are not, we redirect them to the login page.
  222.         echo"Account";
  223.          
  224.         // Remember that this die statement is absolutely critical.  Without it,
  225.         // people can view your members-only content without logging in.
  226.         die("Account");
  227.     }else{
  228.     echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
  229.     } ?> <span class="caret"></span></a>
  230.           <ul class="dropdown-menu" role="menu">
  231.             <?
  232.           if(empty($_SESSION['user']))
  233.     {
  234.         // If they are not, we redirect them to the login page.
  235.         echo"<li><a href='login.php'>Login</a></li>";
  236.          
  237.         // Remember that this die statement is absolutely critical.  Without it,
  238.         // people can view your members-only content without logging in.
  239.         die("<li><a href='login.php'>Login</a></li>");
  240.     }else{
  241.     echo "<li><a href='#'>Edit Account</a></li>";
  242.     } ?>
  243.             <?
  244.           if(empty($_SESSION['user']))
  245.     {
  246.         // If they are not, we redirect them to the login page.
  247.         echo"<li><a href='register.php'>Register</a></li>";
  248.          
  249.         // Remember that this die statement is absolutely critical.  Without it,
  250.         // people can view your members-only content without logging in.
  251.         die("<li><a href='register.php'>Register</a></li>");
  252.     }else{
  253.     echo "<li><a href='logout.php'>Logout</a></li>";
  254.     } ?>
  255.           </ul>
  256.         </li>
  257.         </ul>
  258.             </div>
  259.             <!-- /.navbar-collapse -->
  260.         </div>
  261.         <!-- /.container -->
  262.     </nav>
  263.  
  264.     <div class="container">
  265.  
  266.         <div class="row">
  267.             <div class="col-lg-12">
  268.                 <div class="jumbotron">
  269.   <h1>Edit Account</h1>
  270.   <p>Change your account details here!</p>
  271.   <form action="private.php" method="post" role="form">
  272.   <div class="form-group">
  273.     <label for="exampleInputEmail1">Email address</label>
  274.     <input type="email" name="email" value="<?php echo htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8'); ?>" class="form-control" id="exampleInputEmail1" placeholder="Email Address">
  275.   </div>
  276.   <div class="form-group">
  277.     <label for="exampleInputPassword1">Password</label>
  278.     <input name="password" type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  279.  <small>*Leave blank if you don't want to change it</SMALL>
  280.  </div>
  281. <div class="form-group">
  282.     <label for="exampleInputPassword1">Scrim</label>
  283.     <input name="scrim" class="form-control" id="exampleInputPassword1" placeholder="0/1">
  284.   </div>
  285.   <button type="submit" class="btn btn-default">Update Account</button>
  286.    
  287.   </form>
  288. </div>
  289.             </div>
  290.         </div>
  291.  
  292.     </div>
  293.     <!-- /.container -->
  294.  
  295.     <!-- JavaScript -->
  296.     <script src="js/jquery-2.1.1.min.js"></script>
  297.     <script src="js/bootstrap.js"></script>
  298.  
  299. </body>
  300.  
  301. </html>
Advertisement
Add Comment
Please, Sign In to add comment