来自PHP数据库(PDO)的MySQL数据库的自动填充数据(输入字段)

时间:2016-05-28 12:29:45

标签: php mysql html5 pdo

这篇文章包含:

  • 3个PHP页面
  • MySQL表格图片
  • 表单图片

正如标题所说,我需要从数据库 提取一些信息

到目前为止,我有以下内容:

  • 索引页< - 您需要使用个人代码登录此处。 (这是我从联系页面开始获得“会话用户名”的地方)

  • 配置页< - 用于数据库访问和所有

  • 联系页面< - 在这里,我想自动填写用户数据。

数据库表包含:

  • 用户名;电子邮件; realName(以及其他一些我不需要的东西)

因此,在联系页面中,我希望看到以下内容:

  • 卡号=用户名(已经有效)

  • 根据用户名自动填写:realName和电子邮件。

也许可以在登录时提取信息并将其存储在POST变量中?但是如何?

数据库:

  • 表格我需要使用:'成员'

members

用户自己提供的信息= 用户名

我需要自动填写的信息= realName 电子邮件

表格的图片。

电子邮件应该在SESSION语句中,因此我可以向该人发送确认电子邮件,并且应该从数据库输入realName。

Picture of the form

这些是当前正在运行的文件:

的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 the user class, pass in the database connection
include('classes/user.php');
include('classes/phpmailer/mail.php');
$user = new User($db);
?>

联系表格:

<?php require('includes/config.php'); 

//if not logged in redirect to 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 are 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 are 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 us with your name.<br>"; } 

if(!empty($error_msg))
{ 
//Een van de velden werd niet goed ingevuld 
echo "<b>Your message can't be send due to the following reason:</b>    <br><br>"; 
echo $error_msg; 
echo "<br>Click on <a href='javascript:history.back(1)'>Go back</a> and  provide us with your 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)">
                            Your 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 your 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 would you like?" />
                            </div>
                            <div class="6u 12u(mobilep)">
                            Next Project - Sand
                                <input type="text" name="next2" id="next" value="" placeholder="How many tickets would you 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; *****. All 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>

索引页面(他们必须登录)

<?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 your 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 for any errors
            if(isset($error)){
                foreach($error as $error){
                    echo '<p class="bg-danger">'.$error.'</p>';
                }
            }

            if(isset($_GET['action'])){

                //check the action
                switch ($_GET['action']) {
                    case 'active':
                        echo "<h2 class='bg-success'>Your account is now active you may now log in.</h2>";
                        break;
                    case 'reset':
                        echo "<h2 class='bg-success'>Please contact us for a new card.</h2>";
                        break;
                    case 'resetAccount':
                        echo "<h2 class='bg-success'>Password changed, you may now login.</h2>";
                        break;
                }

            }


            ?>

            <div class="form-group">
                <input type="text" name="username" id="username"     class="form-control input-lg" placeholder="Enter your 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 your 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>

2 个答案:

答案 0 :(得分:1)

已编辑:由于您拥有用户类(例如:$user->is_logged_in()),因此在$_POST[]方法中忽略$_POST['name2'],因为它仍然可以通过DEV工具进行更改,而是更改到$_SESSION['username'](假设这是正确的值)。这比使用HTML字段更可靠。 PHP部分(查询)也有所不同,因为你正在使用PDO,但我无法帮助解决这个问题,因为我使用的是mysqli_,而不是PDO(抱歉),但查询本身应该是类似的

"SELECT * FROM `table` WHERE `number` = '".$_POST['number']."'"

但不要忘记escape your string,否则,您的查询容易受到攻击。

答案 1 :(得分:1)

我解决了这个问题。

在user.php文件中,我已经准备好了所有内容,但我只是不明白为什么它不起作用。

原来是一个简单的错误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 FROM members WHERE username = :username AND 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;
    }
}
}
?>

我忘记将realName和电子邮件添加到此行:( idiot me:D)

$stmt = $this->_db->prepare('SELECT password, username, memberID, realName, email FROM members WHERE username = :username AND active="Yes" ');