简体   繁体   中英

Error messages when inserting data from form into sql

I have a small problem here...

I need to insert data into a database from a form. This works, but i have two problems.

  1. When i launch the website, a blank row is added into the database.
  2. when i launch the website and the whole time, i get a errormessage for each object i want to insert to the database. The errorcode i get is;

Notice: Undefined variable: start in C:\\wamp\\www\\index.php on line 51

Notice: Undefined variable: start in C:\\wamp\\www\\index.php on line 52

Notice: Undefined variable: start in C:\\wamp\\www\\index.php on line 53

How can i fix this problem?

here is my code:

<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>

You check your submit status with if, but you dont wrap the code around if brackets, which means, the insertion and connection is still made, even though there is no submission.

Correct code:

    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 (if its auto increment) doesn't need to be in your INSERT INTO query.

Your if condition is checking the submit is set or not but executing the query since closing braces of if is closed before the query. So just move the closing braces of if block at the end.

    $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();
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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