Auto fill-in data (input field) from MySQL Database in PHP (PDO) -


this post contains:

  • 3 php pages
  • mysql table picture
  • picture of form

as title says, need extract information from database.

so far have following:

  • index page <-- need login here personal code. (this 'session username' works @ contact page)

  • config page <-- used database access , all

  • contact page <-- in here auto fill-in users data.

database table consists of:

  • username; email; realname (and other stuff don't need here)

so in contact page see following:

  • card number = username (this works)

  • auto fill-in: realname , email according username.


maybe possible extract info when logging in , storing these in post variable already? how?


database:

  • table need use: 'members'

members


the info given user himself/herself = username

the info need auto fill-in = realname , email


picture of form.

the email should in session statement, can send confirmation email person, , realname should entered database.

picture of form


these files running:

config.php:

<?php ob_start(); session_start();  //set timezone date_default_timezone_set('europe/brussels');  //database credentials define('dbhost','*****'); define('dbuser','*****'); define('dbpass','*****'); define('dbname','*****');  //application address define('dir','http://*****/'); define('siteemail','noreply@*****');  try {  //create pdo connection $db = new pdo("mysql:host=".dbhost.";dbname=".dbname, dbuser, dbpass); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception);  } catch(pdoexception $e) { //show error echo '<p class="bg-danger">'.$e->getmessage().'</p>'; exit; }  //include user class, pass in database connection include('classes/user.php'); include('classes/phpmailer/mail.php'); $user = new user($db); ?> 

contact form:

<?php require('includes/config.php');   //if not logged in redirect login page if(!$user->is_logged_in()){ header('location: index.php'); }  ?> <html> <head>     <title>*****</title>     <meta charset="utf-8" />     <meta name="viewport" content="width=device-width, initial-scale=1"        />     <!--[if lte ie 8]><script  src="siteassets/assets/js/ie/shiv.js"></script><![endif]-->     <link rel="stylesheet" href="siteassets/assets/css/main.css" />     <!--[if lte ie 8]><link rel="stylesheet" href="siteassets/assets/css/ie8.css" /><![endif]-->     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>  <script> $(document).ready(function(){ $('#next').keyup(calculate); $('#nextprice').keyup(calculate); $('#current').keyup(calculate); $('#currentprice').keyup(calculate); });  function calculate(e) { $('#total').val($('#next').val() * $('#nextprice').val() + $('#current').val() * $('#currentprice').val()); } </script>  </head> <body class="landing">     <div id="page-wrapper">          <!-- header -->             <header id="header" class="alt">                  <nav id="nav">                     <ul>                         <li>                             <a href="#" class="icon fa-angle-down">menu</a>                             <ul>                                 <li><a href="home.php">current project</a></li>                                 <li><a href="newproj.php">next projects</a></li>                                 <li><a href="prev.php">previous projects</a></li>                                 <li><a href="who.php">who we?</a></li>                             </ul>                         </li>                         <li><a href="contact.php" class="button">contact</a></li>                         <li><a href='logout.php'>logout</a></li>                     </ul>                 </nav>             </header>          <!-- banner -->             <section id="banner">                 <h2>*****</h2>                 <p>slogan.</p>                 <ul class="actions">                     <li><a href="home.php" class="button">current project</a></li>                     <li><a href="newproj.php" class="button">next projects</a></li>                     <li><a href="prev.php" class="button">previous projects</a></li>                     <li><a href="who.php" class="button">who we?</a></li>                     <li><a href="contact.php" class="button special">contact</a></li>                 </ul>             </section>          <!-- main -->                 <section id="main" class="container 75%">                 <div class="box"> <?  if($_server['request_method']=="post") {  if(strlen($_post['name2']) == 0) { $error_msg ="- please, provide name.<br>"; }   if(!empty($error_msg)) {  //een van de velden werd niet goed ingevuld  echo "<b>your message can't send due following reason:</b>    <br><br>";  echo $error_msg;  echo "<br>click on <a href='javascript:history.back(1)'>go back</a> ,  provide name.<br><br>";  } else  {  $recipient = "*****";  $subject = "******";  $header = "from: " . $_post['uwemail'] . "\n";  $mail_body = "contact script werd op " . date("d-m-y") . " om " . date("h:i") . " uur uitgevoerd.\n";  $mail_body .= "*****:\n\n";  $mail_body .= "naam: " . $_post['name2'] . "\n";  $mail_body .= "met als kaartnummer: " . $_post['card2'] . "\n";  $mail_body .= "******: " .  $_post['current2'] . "\n";  $mail_body .= "******: " .  $_post['next2'] . "\n";  $mail_body .= "voor een totaal van: " . $_post['total2'] . " eur.";    $mail_body .= "\n\n -- ****** --";  mail($recipient, $subject, $mail_body, $header);  print "<b>important!</b>"; print "<br><br>*****."; print "<br><br>***** "; print $_post['total2']; print " *****"; print "<br><br>******."; print "<br><br>******"; } }  else  {  ?>               <form action="<? echo $_server['php_self']; ?>" method="post" name="contact">                         <div class="row uniform 50%">                             <div class="6u 12u(mobilep)">                             personal card number                                 <input type="text" name="card2" id="card" value="<?php echo $_session['username']; ?>" placeholder="card number" readonly/>                             </div>                             <div class="6u 12u(mobilep)">                             please enter name. (mandatory)                                 <input type="text" name="name2"  id="name" value="" placeholder="your name" />                             </div>                         </div>                         <div class="row uniform 50%">                             <div class="6u 12u(mobilep)">                             current project - smile                                 <input type="text" name="current2" id="current" value="" placeholder="how many tickets like?" />                             </div>                             <div class="6u 12u(mobilep)">                             next project - sand                                 <input type="text" name="next2" id="next" value="" placeholder="how many tickets like?" />                             </div>                         </div>                         <div class="row uniform 50%">                             <div class="6u 12u(mobilep)">                                 <input type="hidden" id="currentprice" value="10" />                             </div>                             <div class="6u 12u(mobilep)">                                 <input type="hidden" id="nextprice" value="10" placeholder="" />                             </div>                         </div>                                         <div class="6u 12u(mobile)">                                             <input name="uwemail" placeholder="email" type="hidden" value="******"/>                                         </div>                         <div class="6u 12u(mobilep)">                         total price.(in eur)                                 <input type="text" name="total2" id="total" value=""  readonly/>                             </div>                         </div>                          <div class="row uniform">                             <div class="12u">                                 <ul class="actions align-center">                                     <li><input type="submit" name="submit"value="place order"/></li>                                 </ul>                             </div>                         </div>                     </form>   <?php  }  ?>                   </div>             </section>           <!-- footer -->             <footer id="footer">                  <ul class="copyright">                     <li>&copy; *****. rights reserved.</li>                 </ul>             </footer>      </div>      <!-- scripts -->         <script src="siteassets/assets/js/jquery.min.js"></script>         <script src="siteassets/assets/js/jquery.dropotron.min.js"></script>         <script src="siteassets/assets/js/jquery.scrollgress.min.js"></script>         <script src="siteassets/assets/js/skel.min.js"></script>         <script src="siteassets/assets/js/util.js"></script>         <!--[if lte ie 8]><script src="siteassets/assets/js/ie/respond.min.js"></script><![endif]-->         <script src="siteassets/assets/js/main.js"></script>     </body> </html> 

index page (where have login)

<?php //include config require_once('includes/config.php');   //process login form if submitted if(isset($_post['submit'])){  $username = $_post['username']; $password = $_post['password'];  if($user->login($username,$password)){      $_session['username'] = $username;     header('location: home.php');     exit;  } else {     $error[] = 'wrong username or password or account has not been activated.'; }  }//end if submit   ?>  <html> <head>        <title>******</title>     <meta charset="utf-8" />     <meta name="viewport" content="width=device-width, initial-scale=1"     />     <!--[if lte ie 8]><script src="assets/js/iv.js"></script><!    [endif]-->     <link rel="stylesheet" href="assets/css/main.css" />     <!--[if lte ie 9]><link rel="stylesheet" href="assets/css/ie9.css"     /><![endif]-->     <!--[if lte ie 8]><link rel="stylesheet" href="assets/css/ie8.css"     /><![endif]-->     <noscript><link rel="stylesheet" href="assets/css/noscript.css" />        </noscript> </head> <body class="is-loading">      <!-- wrapper -->         <div id="wrapper">              <!-- main -->                 <section id="main">                     <header>                          <h1>*****</h1>                         <h1><a style="color:#ffffff" href=''>coming soon.</a></h1>    <div class="container">  <div class="row">      <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">         <form role="form" method="post" action="" autocomplete="off">             <h2>please login</h2>              <hr>              <?php             //check errors             if(isset($error)){                 foreach($error $error){                     echo '<p class="bg-danger">'.$error.'</p>';                 }             }              if(isset($_get['action'])){                  //check action                 switch ($_get['action']) {                     case 'active':                         echo "<h2 class='bg-success'>your account active may log in.</h2>";                         break;                     case 'reset':                         echo "<h2 class='bg-success'>please contact new card.</h2>";                         break;                     case 'resetaccount':                         echo "<h2 class='bg-success'>password changed, may login.</h2>";                         break;                 }              }               ?>              <div class="form-group">                 <input type="text" name="username" id="username"     class="form-control input-lg" placeholder="enter card number" value="    <?php if(isset($error)){ echo $_post['username']; } ?>" tabindex="1">             </div>              <div class="form-group">                 <input type="hidden" name="password" id="password" class="form-control input-lg" value="*****" tabindex="3" >             </div>  <br>              <div class="row">                 <div class="col-xs-6 col-md-6"><input type="submit"    name="submit" value="login" class="btn btn-primary btn-block btn-lg"     tabindex="5"></div>             </div> <hr>                 <div class="row">                 <div class="col-xs-9 col-sm-9 col-md-9">                      <a style="color:#ffffff" href='*********'>lost card? contact us.</a>                 </div>             </div>          </form>     </div> </div>    </div>                         </header>                  </section>              <!-- footer -->                 <footer id="footer">                     <ul class="copyright">                         <li>&copy; ******</li>                     </ul>                 </footer>          </div>      <!-- scripts -->         <!--[if lte ie 8]><script src="assets/js/respond.min.js"> </script><![endif]-->         <script>             if ('addeventlistener' in window) {                 window.addeventlistener('load', function() {  document.body.classname = document.body.classname.replace(/\bis-loading\b/,  ''); });                 document.body.classname +=  (navigator.useragent.match(/(msie|rv:11\.0)/) ? ' is-ie' : '');             }         </script>  </body> </html> 

i fixed problem.

in user.php file had prepared already, didn't understand why didn't work.

turned out simple error xd

<?php include('password.php'); class user extends password{  private $_db; function __construct($db){     parent::__construct();     $this->_db = $db; } private function get_user_hash($username){     try {         $stmt = $this->_db->prepare('select password, username, memberid, realname, email members username = :username , active="yes" ');         $stmt->execute(array('username' => $username));         return $stmt->fetch();     } catch(pdoexception $e) {         echo '<p class="bg-danger">'.$e->getmessage().'</p>';     } } public function login($username,$password){     $row = $this->get_user_hash($username);     if($this->password_verify($password,$row['password']) == 1){         $_session['loggedin'] = true;         $_session['username'] = $row['username'];         $_session['memberid'] = $row['memberid'];         $_session['realname'] = $row['realname'];          $_session['email'] = $row['email'];           return true;     } }  public function logout(){     session_destroy(); }  public function is_logged_in(){     if(isset($_session['loggedin']) && $_session['loggedin'] == true)    {         return true;     } } } ?> 

i forgot add realname , email line: (idiot me :d)

$stmt = $this->_db->prepare('select password, username, memberid, realname, email members username = :username , active="yes" '); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -