doukuiqian5345 2019-01-23 03:13
浏览 64

更新PHP页面并插入db

I am currently trying to set up a login and register page in PHP and MySQL. I have an external connection.php page that connects to a DB and prints a success image (which is posted at the top of every webpage, so it works). In my current code, I am not getting any output at all from the whats in the PHP tags. Once the register is pressed I don't get any error messages (if I try doing a simple echo statement, nothing gets printed either). The page just reloads and that's that. When I check my DB the data isn't inserted. I figure if I can get some sort of output I can debug the rest myself, but I am not even getting that. I tried adding "global $conn;" to see if the scope is the issue, but that's, not the case.

<?php
    include "connection.php";

    if($_POST['register']) {
        #Check to see which button was pressed
        $username = $_POST['username'];
        $password = $_POST['password'];

        echo $username . " " . $password;

        #First check to see if username is already selected
        $query = $conn->prepare("SELECT * FROM accounts WHERE username=$username");
$query->execute();
        if(mysqli_num_rows($query) >= 1) {
            echo "Account already exists. Please try again.";
        } else {
            $insert = $conn->prepare('INSERT INTO accounts(username, password) VALUES (?, ?)');
            $insert->bind_param("ss", $username, $password);
            $insert->execute() or die (mysqli_error());
            echo "Successfuly Registered";
        }
    }



 ?>

<!DOCTYPE html>
 <html>
    <head>
        <title>UND Bookstore Login</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
        <meta name="author" content="Sai Peri"></meta>
        <meta name="description" content="CSCI 457 Assignment 1"></meta>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
        <!-- Latest jQuery libraries -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                        <nav id="nav" class="navbar navbar-inverse text-center">
                            <ul class="nav navbar-nav">
                                <li><a href="index.php">Home</a></li>
                                <li class="active"><a href="login.php">Login</a></li>
                                <li><a href="search.php">Search</a></li>
                                <li><a href="#">Add/Delete/Edit</a></li>
                            </ul>
                        </nav>
                </div> <!-- End col-md-12 -->
            </div>  <!-- End row (nav)-->
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header">
                        <h3 class="text-center">Login below <small>Or register below</small></h3>
                    </div> <!-- End of page-header -->
                </div> <!-- End col-md-12 -->
            </div> <!-- End row (nav)-->
            <div class="row" id="body">
                <!-- Add text boxes and buttons -->
                <form action="login.php" method="post">
                    <div class="form-group">
                        <label>Username</label>
                        <input type="text" class="form-control" id="inputUsername" placeholder="Username" name="username">
                        <label>Password</label>
                        <input type="password" class="form-control" id="inputPassword" placeholder="Password" name="password">
                        <button type="submit" class="btn btn-primary" name="login">Login</button>
                        <button type="submit" class="btn btn-primary" name="register">Register</button>
                    </div> <!-- End form-group -->
                </form>

            </div> <!-- End of row -->
            <div class="row" id="footer">
                <div class="col-md-12 text-center">
                    <footer>Created 2019 by Sai Peri</footer>
                </div> <!-- End col-md-12 -->
            </div> <!-- End row (nav)-->
        </div> <!-- End container -->
    </body>
</html>

SOLVE: The issue was solved using the following statement isset($_POST['register']) or by doing !empty($_POST...) and adding values to the input tags.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测