PHP 不会将数据插入 Mysql 数据库 - 不会引发错误

标签 php mysql database insert

嗨,我似乎找不到有关此主题的任何帮助,因为没有抛出错误。我试图使用 mysqli_query 通过 php 将记录插入数据库,但在重定向后没有进行任何更改。我正在使用三个文件:index.php、conn.php 和 new.php。 index.php 和 new.php 位于同一文件夹中,但 conn.php 位于下一个目录。 索引.php:

    <!DOCTYPE html>
<html>
    <head>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <link rel="stylesheet" href="style.css" type="text/css" >
        <link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Khula" rel="stylesheet">

    </head>
    <script>
        $(function()
        {
            $('.error').fadeOut(10000);
        });
    </script>

    <body>

        <header>
           <img src="images/logo.png"> 
            <p>The reliable bus company</p>
        </header>

        <div class="wrapper">
            <div class="container">
                <div class="titletxt">
                    <h4>Drivers</h4>

                </div>
                <?php
        include '../conn.php';
        mysqli_query($conn, "SET NAMES utf8");
        $result = mysqli_query($conn, "SELECT * FROM tbl_employee");

        echo "
        <div class='table_content'>
        <table align='center'>
        <tr>
        <th>Employee ID</th>
        <th>Title</th>
        <th>Name</th>
        <th>Address</th>
        <th>Contact Number</th>
        <th>Job Position</th>
        <th>Gender</th>
        <th>DOB</th>
        </tr>
        ";

        while($row = mysqli_fetch_array($result))
        {
            echo "<tr>";
            echo "<td>" . $row['employeeID'] . "</td>";
            echo "<td>" . $row['title'] . "</td>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['address'] . "</td>";
            echo "<td>" . $row['contactNum'] . "</td>";
            echo "<td>" . $row['position'] . "</td>";
            echo "<td>" . $row['gender'] . "</td>";
            echo "<td>" . $row['DOB'] . "</td>";
            echo "</tr>";
        }
        echo "</table></div>";


        ?>


                <!-- Record Insert -->
                <br>
                <div class="titletxt">
                    <h4>Insert a Record</h4>
                </div>
                <h3 style="font-weight: 400; margin-left: 5px;">New Employee</h3>
                <form class="insert_form" action="new.php" method="post" name="insert_form">
                    <label>Title: </label>
                    <input type="text" name="title" required><br>
                    <span class="error"><?php echo $titleErr ?></span>
                    <br>
                    <label>Name: </label>
                    <input type="text" name="name" required> <br>
                    <span class="error"><?php echo $nameErr ?></span>
                    <br>
                    <label>Address:</label>
                    <input type="text" name="address" required><br>
                    <span class="error"><?php echo $addressErr ?></span>
                    <br>
                    <label>Contact Number</label>
                    <input type="text" name="contactNum" required><br>
                    <span class="error"><?php echo $contactErr ?></span>
                    <br>
                    <label>Job Position</label>
                    <input type="text" name="position" required><br>
                    <span class="error"><?php echo $positionErr ?></span>
                    <br>
                    <label>Gender: </label>
                    <input type="radio" name="gender" value="male" required> Male
                    <input type="radio" name="gender" value="female" required> Female<br>
                    <span class="error"><?php echo $genderErr ?></span>
                    <br>
                    <label>DOB: </label>
                    <input style="width: 60px;" type="text" name="DOB_year" required>YYYY
                    <input style="width: 30px;" type="text" name="DOB_months" required>MM
                    <input type="text" name="DOB_day" style="width: 30px" required>DD<br>
                    <span class="error"><?php echo $DOBErr ?></span>
                    <br>
                    <input type="submit" Value="Insert Entry">

                </form>


            </div>
        </div>
    </body>
</html>

conn.php:

    <?php 
   $server = "localhost";
    $user = "root";
    $password = "";
    $db = "bus_db";
    global $conn;
    $conn = mysqli_connect($server, $user, $password, $db);
    if(mysqli_connect_errno())
    {
        echo "Mysql Error has occured" . mysqli_connect_error;
    }

    else if(!mysqli_connect_errno())
    {
        echo "<connection>Connection Established</connection>";
    }


function close_connection()
{
    global $conn;
    mysqli_close($conn);
}

$title = $name = $address = $contact = $position = $gender = $DOB = "";
                      $titleErr = $nameErr = $addressErr = $contactErr = $positionErr = $genderErr = $DOBErr = ""; 
                mysqli_query($conn, "SET NAMES utf8");
                    if ($_SERVER["REQUEST_METHOD"] == "POST")
                      {
                          if (empty($_POST["title"]))
                          {
                              $titleErr = "Title is Required";
                          }else{
                              $title = input($_POST["title"]);
                          }

                          if (empty($_POST["name"]))
                          {
                              $nameErr = "Name is Required";
                          }else
                          {
                              $name = input($_POST["name"]);
                              if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                                    $nameErr = "Invalid Name"; 
                            }
                          }

                          if (empty($_POST["address"]))
                          {
                              $addressErr = "Address is Required";
                          }else{
                              $address = input($_POST["address"]);
                          }

                            if (empty($_POST["contactNum"]))
                            {
                                $contactErr = "Contact Number is required ";
                            }else{
                                $contact = input($_POST["contactNum"]);
                                $regex = "^([0-9]{10,11})$^";
                                if (!preg_match($regex, $contact)) {
                                    $contactErr = "Invalid Phone Number";
                                }
                            }

                            if(empty($_POST["position"]))
                            {
                                $positionErr = "Position is required";
                            }else{
                                $position = input($_POST["position"]);
                            }

                            if (empty($_POST["gender"]))
                            {
                                $genderErr = "Gender is Required";
                            }else{
                                $gender = input($_POST["gender"]);
                            }

                            if (empty($_POST["DOB_year"]) || empty($_POST["DOB_months"]) || empty($_POST["DOB_day"]))
                            {
                                $DOBErr = "Invalid entry for date of birth";
                            }else
                            {
                                $DOB = input($_POST["DOB_year"] + "/" + $_POST["DOB_months"] + "/" + $_POST["DOB_day"]);
                            }
                      }
                      function input($data) {
                          $data = trim($data);
                          $data = stripslashes($data);
                          $data = htmlspecialchars($data);
                          return $data;
                      }

function insert_records($p_title, $p_name, $p_address, $p_contact, $p_position, $p_gender, $p_DOB)
{
    global $conn;
    mysqli_query($conn, "INSERT INTO tbl_employee VALUES(null, '" .$p_title."', '".$p_name."', '".$p_address."', '".$p_contact."', '".$p_position."', '".$p_gender."', '".$p_DOB."')");
}
?>

新的.php:

<?php 
include '../conn.php';
insert_records($title, $name, $address, $contact, $position, $gender, $DOB);
header( 'Location:index.php');
close_connection();
?>

我将不胜感激,谢谢

最佳答案

您应该编辑 insert_records(),以便在 mysqli_query 失败时向您提供反馈。

function insert_records($p_title, $p_name, $p_address, $p_contact, $p_position, $p_gender, $p_DOB)
{
    global $conn;
    $result = mysqli_query($conn, 'some query') or die('Query failed: ' . mysqli_error($conn));
    return $result;
}

并在此处阅读有关如何防止 MySQL 注入(inject)的信息:How can I prevent SQL injection in PHP?

<小时/>

编辑:

$DOB = input($_POST["DOB_year"] + "/" + $_POST["DOB_months"] + "/" + $_POST["DOB_day"]);

在php中'+'用于进行计算。如果你想连接字符串使用'.'

$DOB = input($_POST["DOB_year"] . "/" . $_POST["DOB_months"] . "/" . $_POST["DOB_day"]);

关于PHP 不会将数据插入 Mysql 数据库 - 不会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44101191/

相关文章:

mysql - SQL:选择在另一个表中不具有值的 ID/值组合

php - 使用 implode 和 stropos 将日期转换为数组

php数学题

mysql - 计算查询中的百分比

python - 安装python Mysql模块

使用库进行数据库迁移

html - 如何应对外语?

php - 模型可以链接到数据库中的 View 而不是 CakePHP 中的表吗?

php - Joomla 对禁用缓存的请求执行的数据库查询总量是多少?

php - 如何在 PHP 中调试 "array_key_exists() expects parameter 2 to be array, integer given"?