将表单中的数据插入sql时出现错误消息

时间:2015-03-23 15:36:28

标签: php mysql sql database

我这里有一个小问题......

我需要从表单中将数据插入数据库。这有效,但我有两个问题。

  1. 当我启动网站时,数据库中会添加一个空行。
  2. 当我启动网站和整个时间时,我得到了我想要插入数据库的每个对象的错误消息。我得到的错误代码是;
  3. 注意:未定义的变量:从第51行的C:\ wamp \ www \ index.php开始

    注意:未定义的变量:从第52行的C:\ wamp \ www \ index.php开始

    注意:未定义的变量:从第53行的C:\ wamp \ www \ index.php开始

    我该如何解决这个问题?

    这是我的代码:

    <html>
        <head>
            <title>ARbeidstimer</title>
            <link rel="stylesheet" type="text/css" href="style.css">
        </head>
    
        <body>
            <h2>Arbeidstimer</h2>
    
            <div id ="register">
                    <form action="index.php" method="post">
    
                        <p>
                            <label>Start: </label>
                            <input type="text" name="start" class="field">
                        </p>
                        <p>
                            <label>Slutt:</label>
                            <input type="text" name="slutt" class="field">
                        </p>
                        <p>
                            <label for="telefon">Timer:</label>
                            <input type="text" name="timer" class="field">
                        </p>
                        <p>
                            <input type="submit" name="submit" value="send">
                        </p>
                    </form>
                </div>
    
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "timer";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    if(isset($_POST['submit'])) {
    $start = $_POST['start'];
    $slutt = $_POST['slutt'];
    $timer = $_POST['timer'];
    }
    
    $sql = "INSERT INTO jobbing (id, start, slutt, timer)
    VALUES ('', '$start', '$slutt', '$timer')";
    
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    $conn->close();
    ?>
        </body>
    
    </html>
    

2 个答案:

答案 0 :(得分:3)

您使用if检查提交状态,但是如果括号(即插入和连接仍未进行),即使没有提交,也不要包装代码。

正确的代码:

    if(isset($_POST['submit'])) {
$start = $_POST['start'];
$slutt = $_POST['slutt'];
$timer = $_POST['timer'];


$sql = "INSERT INTO jobbing (id, start, slutt, timer)
VALUES ('', '$start', '$slutt', '$timer')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}

ID(如果它的自动增量)不需要在INSERT INTO查询中。

答案 1 :(得分:0)

您的if条件是检查提交是否已设置但执行查询,因为在查询之前关闭if的结束括号。所以只需在结尾处移动if块的右括号。

    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    if(isset($_POST['submit'])) {
    $start = $_POST['start'];
    $slutt = $_POST['slutt'];
    $timer = $_POST['timer'];

    $sql = "INSERT INTO jobbing (id, start, slutt, timer)
    VALUES ('', '$start', '$slutt', '$timer')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    }
    $conn->close();
    ?>