如何使用php和mysql在URL参数中填充字段

时间:2016-05-13 07:47:55

标签: php mysql pdo

我有一个URL链接,点击链接后,必须在页面打开时填充字段,下面是URL链接,表格必须填充少量字段,如名称等字段,idocode和电子邮件必须填充,其余由用户和提交按钮无效,我做错了什么,

网址:'http://www.domain.com/register/registration.php?token= $ token& stud_id = stud_id'

<?php
    error_reporting(1);
    session_start();

    include 'includes/connect.php';


    $student_id = $_GET["id"];
    $_SESSION['student_id'] = $student_id;

    $token = $_GET["tokenk"];
    $_SESSION['token'] = $token;

    if (isset($_GET["id"]) && isset($_GET["tk"])) {

        $sql = "SELECT * FROM student WHERE student_id=" . $student_id . " and token='" . $token . "'";

        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!empty($result)) {

            $sql = "UPDATE student SET first_name=:first_name, middle_name=:middle_name, last_name=:last_name WHERE student_id=:student_id";
            $stmt->bindValue(':first_name', $first_name);
            $stmt->bindValue(':middle_name', $middle_name);
            $stmt->bindValue(':last_name', $last_name);
            $stmt->bindValue(':student_id', $student_id);
            $result = $stmt->execute();

        } else {

        }

    }


    if ($_POST["Submit"] == "Submit") {
        //echo '<pre>';print_r($_POST["name"]);exit;
        $ccode = $_POST["idode"];
        $first_name = $_POST['first_name'];
        $middle_name = $_POST['middle_name'];
        $last_name = $_POST['last_name'];
        $course = $_POST["course"];
        $email = $_POST['email'];
        $password = $_POST["password"];
        $confirm_password = $_POST["confirm_password"];


        //echo "<br/>In post"; die();
        //echo $name.'>>>>'.$email.'>>>>'.$idcode;exit;

        add_student_session($_POST);

        $validate = add_student_validator($_POST);
        //echo '<pre>';print_r($validate);exit;

        if (isset($validate) && $validate) {
            //echo "<br/>Validated: TRUE<br/>"; die();
            $sql = "SELECT COUNT(*) AS num FROM student WHERE student_id = :student_id";
            $stmt = $pdo->prepare($sql);

            $stmt->bindValue(':idcode', $idcode);
            $stmt->bindValue(':first_name', $first_name);
            $stmt->bindValue(':middle_name', $middle_name);
            $stmt->bindValue(':last_name', $last_name);
            $stmt->bindValue(':course', $course);
            $stmt->bindValue(':date_of_birth', $date_of_birth);
            $stmt->bindValue(':email', $email);
            $stmt->bindValue(':password', $password);


            $stmt->execute();

            $row = $stmt->fetch(PDO::FETCH_ASSOC);


            if ($row['num'] > 0) {
                $_SESSION["already_exist"] = TRUE;
            } else {
                //echo "<br/>Validated: FALSE<br/>"; die();

                $sql = "INSERT INTO student (course, date_of_birth, password) VALUES (:course, :date_of_birth, :password)";
                $stmt = $pdo->prepare($sql);

                //echo "<br/>CCODE: ".$idcode;exit;


                $stmt->bindValue(':course', $course);
                $stmt->bindValue(':date_of_birth', $date_of_birth);
                $stmt->bindValue(':password', $password);
                $stmt->bindValue(':confirm_password', $confirm_password);

                if ($_POST["password"] != $_POST["confirm_password"]) {
                    echo 'passwords do not match';
                }


                $result1 = $stmt->execute();
                //echo $result ;exit;


                if ($result1) {

                    echo '<div><p class="text-success">Created Successfully!</p></>';
                }
                clear_session();


            }
        }
    }

?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Register</title>
        <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
        <link rel="stylesheet" href="css/bootstrap.min.custom.css" type="text/css"/>


        <script src="js/bootstrap.min.js"></script>
        <script language="javascript" src="js/calendar.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="Absolute-Center is-Responsive">

                    <div class="col-md-10 col-md-offset-2 well">

                        <form class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
                              role="form">


                            <legend>Registration</legend>

                            <div class="form-group">
                                <label for="name" class="col-xs-2 control-label">CCODE:</label>
                                <div class="col-xs-10">
                                    <input class="form-control" type="text" name="idcode" required
                                           placeholder="CCODE/EMPLOYEE CODE/ ID CODE"
                                           value="<?php echo @$_POST['idcode']; ?>" readonly/>
                                    <label for="idcode" generated="true" class="error">
                                        <?= isset($error_hash["idcode"]) ? $error_hash["idcode"] : "" ?>
                                    </label>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="birthday" class="col-xs-2 control-label">Name:</label>
                                <div class="col-xs-10">
                                    <div class="form-inline">
                                        <div class="form-group">
                                            <input class="form-control" type="text" name="first_name" required
                                                   placeholder="First Name"
                                                   value="<?php echo @$_POST['first_name']; ?>"/>
                                            <label for="first_name" generated="true" class="error">
                                                <?= isset($error_hash["first_name"]) ? $error_hash["first_name"] : "" ?>
                                            </label>
                                        </div>
                                        <div class="form-group">
                                            <input class="form-control" type="text" name="middle_name" required
                                                   placeholder="Middle Name"
                                                   value="<?php echo @$_POST['middle_name']; ?>"/>
                                            <label for="middle_name" generated="true" class="error">
                                                <?= isset($error_hash["middle_name"]) ? $error_hash["middle_name"] : "" ?>
                                            </label>
                                        </div>
                                        <div class="form-group">
                                            <input class="form-control" type="text" name="last_name" required
                                                   placeholder="Last Name" value="<?php echo @$_POST['last_name']; ?>"/>
                                            <label for="middle_name" generated="true" class="error">
                                                <?= isset($error_hash["last_name"]) ? $error_hash["last_name"] : "" ?>
                                            </label>
                                        </div>

                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="name" class="col-xs-2 control-label">Designation:</label>
                                <div class="col-xs-10">
                                    <input class="form-control" type="text" name="course" required
                                           placeholder="Designation"
                                           value="<?= isset($_SESSION["course"]) ? $_SESSION["course"] : ""; ?>"/>
                                    <label for="course" generated="true" class="error">
                                        <?= isset($error_hash["course"]) ? $error_hash["course"] : "" ?>
                                    </label>
                                </div>
                            </div>


                            <div class="form-group">
                                <label for="name" class="col-xs-2 control-label">Date-Of-Birth:</label>
                                <div class="col-xs-8">


                                    <select name="dd">
                                        <option value="">Date</option>
                                        <?php
                                            for ($i = 1; $i <= 31; $i++) {
                                                echo "<option value='$i'>" . $i . "</option>";
                                            }
                                        ?>
                                    </select>
                                    <select name="mm">
                                        <option value="">Month</option>
                                        <?php
                                            for ($i = 1; $i <= 12; $i++) {
                                                echo "<option value='$i'>" . $i . "</option>";
                                            }
                                        ?>
                                    </select>

                                    <select name="yy">
                                        <option value="">Year</option>
                                        <?php
                                            for ($i = 1960; $i <= 2020; $i++) {
                                                echo "<option value='$i'>" . $i . "</option>";
                                            }
                                        ?>
                                    </select>

                                </div>
                            </div>

                            <div class="form-group">
                                <label for="name" class="col-xs-2 control-label">Email:</label>
                                <div class="col-xs-10">
                                    <input class="form-control" type="email" name="email" required placeholder="Email"
                                           value="<?php echo @$_POST['email']; ?>" readonly/>
                                    <label for="email" generated="true" class="error">
                                        <?= isset($error_hash["email"]) ? $error_hash["email"] : "" ?>
                                    </label>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="name" class="col-xs-2 control-label">Password:</label>
                                <div class="col-xs-10">
                                    <input class="form-control" type="password" name="password" required
                                           placeholder="Password"
                                           value="<?= isset($_SESSION["password"]) ? $_SESSION["password"] : ""; ?>"/>
                                    <label for="name" generated="true" class="error">
                                        <?= isset($error_hash["password"]) ? $error_hash["password"] : "" ?>
                                    </label>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="name" class="col-xs-2 control-label">Confirm Password:</label>
                                <div class="col-xs-10">
                                    <input class="form-control" type="password" name="confirm_password" required
                                           placeholder="Confirm Password"
                                           value="<?= isset($_SESSION["confirm_password"]) ? $_SESSION["confirm_password"] : ""; ?>"/>
                                    <label for="name" generated="true" class="error">
                                        <?= isset($error_hash["confirm_password"]) ? $error_hash["confirm_password"] : "" ?>
                                    </label>
                                </div>
                            </div>
                        </form>


                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">

                                <div class="form-actions">
                                    <input type="submit" name="Submit" value="Submit" class="btn btn-primary"/>

                                </div>

                                <br>

                            </div>
                        </div>


                        </form>
                    </div>
                </div>
            </div>
        </div>
</html>

0 个答案:

没有答案