在PHP中执行SQL查询时,没有显示错误,但数据库中看不到数据。我应该检查什么?

问题描述 投票:0回答:2

我正在尝试使用 PHP 将数据添加到 MySql 数据库中。

代码:

<?php
    session_start();
    include('config.php');


    if(isset($_POST['Submit'])) {
        $city = $_POST['city'];
    
        $from_station = $_POST['from_station'];
        $from_date = $_POST['from_date'];

        // Prepare and execute the SQL query
        $sql = "INSERT INTO journeys (city, from_station, from_date) VALUES ('$city', '$from_station', '$from_date')";
        $result = $conn->query($sql);

        if (mysqli_query($conn, $sql)) {
            echo "New record created successfully";
        } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }

    }
?>



<!DOCTYPE html>
<html lang="pt_PT">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>IR PLANNER</title>
        <link rel="stylesheet" href="Styles\general_styles.css">
        <link rel="stylesheet" href="Styles\citymanager_styles.css">
    </head>


    <body>        
        <form class="modal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
            <label for="city">City:</label>
            <input type="text" name="city" id="city">
            <label for="from_station">From Station:</label>
            <input type="text" name="from_station" id="from_station">
            <label for="from_date">From Date:</label>
            <input type="date" name="from_date" id="from_date">
            <input type="submit" value="Submit" id="Submit">
        </form>
       
    </body>

</html>

按下提交按钮时,不会显示错误。然而,当检查数据库表时,什么也没有出现。

我已阅读文章,并且 sql 连接(config.php)工作正常(在其他页面和文件中工作)并且 sql 查询也很好。

有人遇到这个问题吗?

php mysql mysqli sql-insert
2个回答
1
投票

您需要在提交按钮中传递 name 属性,直到您无法从中获取任何值。在您的条件下,它总是返回 false。所以不会更新任何内容,也不会生成错误。只需更新这里是更新的代码。

<?php
        session_start();
        include('config.php');
    
    
        if(isset($_POST['Submit'])) {
            $city = $_POST['city'];
        
            $from_station = $_POST['from_station'];
            $from_date = $_POST['from_date'];
    
            // Prepare and execute the SQL query
            $sql = "INSERT INTO journeys (city, from_station, from_date) VALUES ('$city', '$from_station', '$from_date')";
            $result = $conn->query($sql);
    
            if (mysqli_query($conn, $sql)) {
                echo "New record created successfully";
            } else {
                    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }
    
        }
    ?>
    
    
    
    <!DOCTYPE html>
    <html lang="pt_PT">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>IR PLANNER</title>
            <link rel="stylesheet" href="Styles\general_styles.css">
            <link rel="stylesheet" href="Styles\citymanager_styles.css">
        </head>
    
    
        <body>        
            <form class="modal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <label for="city">City:</label>
                <input type="text" name="city" id="city">
                <label for="from_station">From Station:</label>
                <input type="text" name="from_station" id="from_station">
                <label for="from_date">From Date:</label>
                <input type="date" name="from_date" id="from_date">
                <input type="submit" name="Submit" value="Submit" id="Submit">
            </form>
           
        </body>
    
    </html>

0
投票

以下是您必须采取的基本调试步骤:

  1. 执行过程中出现错误。请参阅此答案中的详细说明。
  2. 由于程序逻辑不正确,SQL根本没有运行。添加临时调试输出以确保代码到达查询执行的位置(这是提交输入时出现的特定问题,但这不是重点)。
  3. SQL执行成功,但在错误的数据库中查看结果。仔细检查一下
  4. 输入的数据与数据库不匹配。以下是一些关于如何检查的建议。
© www.soinside.com 2019 - 2024. All rights reserved.