简体   繁体   English

使用 php 以 html 形式将数据插入 mysql 数据库

[英]Inserting data into mysql database using php in an html form

Please help out.请帮忙。 I have an html form with meant to be submitted to mysql database.我有一个 html 表单,打算提交给 mysql 数据库。 I have written the html and the php script for inserting data but it does not insert data into mysql and it throws an error "something went wrong" as I have programmed it show when data is not inserted.我已经编写了用于插入数据的 html 和 php 脚本,但它不会将数据插入到 mysql 中,并且它会抛出一个错误“出现问题”,因为我已经编程它显示何时未插入数据。 My problem now is how do I find out the error message or how do I re-code it so it can be submitted successfully.我现在的问题是如何找出错误消息或如何重新编码以便成功提交。 Many thanks as I await your responses.非常感谢,因为我正在等待您的回复。

<?php

session_start();

if (!isset($_SESSION['SESSION_EMAIL'])) {
    header("Location: index.php");
    die();
}



//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;


//Load Composer's autoloader
require 'vendor/autoload.php';

include 'includes/config.php';
include 'includes/db.php';
include 'includes/functions.php';
$msg = "";

if (isset($_POST['save'])) {
    $fname = mysqli_real_escape_string($db, $_POST['fname']);
    $mname = mysqli_real_escape_string($db, $_POST['mname']);
    $lname = mysqli_real_escape_string($db, $_POST['lname']);
    $gender = mysqli_real_escape_string($db, $_POST['gender']);
    $phone = mysqli_real_escape_string($db, $_POST['phone']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $birthdate = mysqli_real_escape_string($db, $_POST['birthdate']);
    $community = mysqli_real_escape_string($db, $_POST['community']);
    $lga = mysqli_real_escape_string($db, $_POST['lga']);
    $caddress = mysqli_real_escape_string($db, $_POST['caddress']);
    $haddress = mysqli_real_escape_string($db, $_POST['haddress']);
    $flgac = mysqli_real_escape_string($db, $_POST['flgac']);
    $mlgac = mysqli_real_escape_string($db, $_POST['mlgac']);
    $nok = mysqli_real_escape_string($db, $_POST['nok']);
    $nokphone = mysqli_real_escape_string($db, $_POST['nokphone']);
    $nokemail = mysqli_real_escape_string($db, $_POST['nokemail']);

    if (mysqli_num_rows(mysqli_query($db, "SELECT * FROM personalinfo WHERE Email='{$email}'")) > 0) {
        echo $msg = "<div class='alert alert-danger'>{$email} - This email address already exists!.</div>";
    } else {
        $sql = "INSERT INTO personalinfo (Firstname, Middlename, Lastname, Gender, Phone, Email, DOB, Community, LGA, ContactAddress, HomeAddress, FatherLGACommunity, MotherLGACommunity, NOK, NOKPhone, NOKEmail) VALUES ('{$fname}', '{$mname}', '{$lname}', '{$gender}', '{$phone}', '{$email}', '{$birthdate}', '{$community}', '{$lga}', '{$caddress}', '{$haddress}', '{$flgac}', '{$mlgac}', '{$nok}', '{$nokphone}', '{$nokemail}')";
    }
    $result = mysqli_query($db, $sql);
    if ($result) {
        echo $msg = "<div class='alert alert-info'>Personal Info Saved Successfully!.</div>";
    } else {
        echo $msg = "<div class='alert alert-danger'>Something went wrong.</div>";

    }
}

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <title>HYPREP - Educational Support For Ogonis</title>
        <!-- Favicon-->
        <link rel="icon" type="image/x-icon" href="images/ORMLogo.jpg" />
        <!-- Core theme CSS (includes Bootstrap)-->
        <link href="css/styles.css" rel="stylesheet" />
        <link href="css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="d-flex" id="wrapper">
            <!-- Sidebar-->
            <div class="border-end bg-white" id="sidebar-wrapper">
                <div class="sidebar-heading border-bottom bg-light">ORM Consult & Ltd</div>
                <img src="images/ormconsult.jpg" alt="orm_logo" width="52" height="52" style="align:center">
                <div class="list-group list-group-flush">
                    <a class="list-group-item list-group-item-action list-group-item-light p-3" href="personalinfo.php">Personal Info</a>
                    <a class="list-group-item list-group-item-action list-group-item-light p-3" href="editpersonalinfo.php">Edit Personal Info</a>
                    <a class="list-group-item list-group-item-action list-group-item-light p-3" href="educationalinfo.php">Educational Info</a>
                    <a class="list-group-item list-group-item-action list-group-item-light p-3" href="editeducationalinfo.php">Edit Educational Info</a>
                    <a class="list-group-item list-group-item-action list-group-item-light p-3" href="bankinfo.php">Bank Info</a>
                    <a class="list-group-item list-group-item-action list-group-item-light p-3" href="editbankinfo.php">Edit Bank Info</a>
                    <a class="list-group-item list-group-item-action list-group-item-light p-3" href="documentinfo.php">Document Upload</a>
                    <a class="list-group-item list-group-item-action list-group-item-light p-3" href="editdocumentinfo.php">Edit Document</a>
                </div>
            </div>
            <!-- Page content wrapper-->
            <div id="page-content-wrapper">
                <!-- Top navigation-->
                <nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom">
                    <div class="container-fluid">
                        <button class="btn btn-primary" id="sidebarToggle">Toggle Menu</button>
                        <h6>Welcome <?php $query = mysqli_query($db, "SELECT * FROM users WHERE email='{$_SESSION['SESSION_EMAIL']}'"); if (mysqli_num_rows($query) > 0) {$row = mysqli_fetch_assoc($query); echo $row['name'];}?></h6>
                        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
                        <div class="collapse navbar-collapse" id="navbarSupportedContent">
                            <ul class="navbar-nav ms-auto mt-2 mt-lg-0">
                            <li class="nav-item active"><a class="nav-link" href="hyprep/index.php">Home</a></li>
                                <li class="nav-item"><a class="nav-link" href="changepassword.php">Change Password</a></li>
                                <li class="nav-item"><a class="nav-link" href="logout.php">Logout</a></li>
                             
                                <!--<li class="nav-item dropdown">
                                    <a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
                                    <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                                        <a class="dropdown-item" href="#!">Action</a>
                                        <a class="dropdown-item" href="#!">Another action</a>
                                        <div class="dropdown-divider"></div>
                                        <a class="dropdown-item" href="#!">Something else here</a>
                                    </div>
                                </li>-->
                            </ul>
                        </div>
                    </div>
                </nav>
                <!-- Page content-->
<div class="container-fluid">
    <img src="images/Hyprep.jfif" alt="orm_logo" width="120" height="82" style="align:center">
    <h2 class="mt-4">PERSONAL INFO</h2>
    <br>
    <?php echo $msg; ?>
    <br>
<form method="post" action="personalinfo.php">
<div class="row">
    <div class="form-group col-md-4">
        <input type="text" class="form-control" id = "fname" name="fname" placeholder="First Name" required>
    </div>

    <div class="form-group col-md-4">
        <input type="text" class="form-control" id="mname" name="mname" placeholder="Middle Name" required>
    </div>

    <div class="form-group col-md-4">
        <input type="text" class="form-control" id = "lname" name="lname" placeholder="Last Name" required>
    </div>
</div>
<!-- End of A Row-->
<br>
<div class="row">
    <div class="form-group col-md-4">
    <select id="gender" name="gender" class="form-control">
        <option value="Select gender" selected>Select Gender</option>
        <option value="M">Male</option>
        <option value="F">Female</option>
      </select>
    </div>

    <div class="form-group col-md-4">
        <input type="text" class="form-control" id="phone" name="phone" maxlength="11" placeholder="Phone" required>
    </div>

    <div class="form-group col-md-4">
        <input type="email" class="form-control" id ="email" name="email" placeholder="Email" value ="<?php $query = mysqli_query($db, "SELECT * FROM users WHERE email='{$_SESSION['SESSION_EMAIL']}'");
    if (mysqli_num_rows($query) > 0) {
        $row = mysqli_fetch_assoc($query);
        echo $row['email'];
    } ?>" required readonly>
    </div>
</div>
<!-- End of A Row-->
<br>
<div class="row">
    <div class="form-group col-md-4">
    <input type="date" class="form-control" id="birthdate" name="birthdate" placeholder="Birth Date" required>
    </div>

    <div class="form-group col-md-4">
        <input type="text" class="form-control" id= "community" name="community" placeholder="Community" required>
    </div>

    <div class="form-group col-md-4">
    <select id="lga" name="lga" class="form-control">
        <option value="Select LGA" selected>Select LGA</option>
        <option value="E">Eleme</option>
        <option value="G">Gokana</option>
        <option value="K">Khana</option>
        <option value="T">Tai</option>
      </select>
    </div>
</div>
<br>
<!-- End of A Row-->
<div class="row">
    <div class="form-group col-md-4">
        <input type="text" class="form-control" id = "caddress" name="caddress" placeholder="Contact Address" required>
    </div>

    <div class="form-group col-md-4">
        <input type="text" class="form-control" id="haddress" name="haddress" placeholder="Home Address" required>
    </div>

    <div class="form-group col-md-4">
        <input type="text" class="form-control" id="flgac" name="flgac" placeholder="Father LGA/Community" required>
    </div>
</div>
<!-- End of A Row-->
<br>
<div class="row">
    <div class="form-group col-md-3">
        <input type="text" class="form-control" id = "mlgac" name="mlgac" placeholder="Mother LGA/Community" required>
    </div>

    <div class="form-group col-md-3">
        <input type="text" class="form-control" id ="nok" name="nok" placeholder="Next of Kin Name" required>
    </div>

    <div class="form-group col-md-3">
        <input type="text" class="form-control" id="nokphone" name="nokphone" maxlength="11" placeholder="Next of Kin Phone" required>
    </div>

    <div class="form-group col-md-3">
        <input type="text" class="form-control" id = "nokemail" name="nokemail" placeholder="Next of Kin Email" required>
    </div>
</div>
<!-- End of A Row-->
<br>
<?php echo $msg;?>
    <br>
<button type="submit" id = "save" name="save" class="btn btn-primary">SAVE</button>

</form>

</div>
        <!-- Bootstrap core JS-->
        <!-- Core theme JS-->
        <script src="js/scripts.js"></script>
    </body>
</html>

Asuming there is an issue with the query check the error log using mysqli_error()假设查询有问题,请使用mysqli_error()检查错误日志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM