简体   繁体   中英

Can't insert data with php into mysql

so i am working on the simple project and i dont know why, but i can't insert data into the database

Here is my connection to database

 <?php
$servername = "localhost";
$username = "root";
$password = "root";
$database = "register";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $register);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

And in this part of code i am trying to insert data:

<?php
  if (isset($_POST['submitreg'])){
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    $sql = "INSERT INTO users (email, username, password) VALUES ('$email', '$username', '$password')";
    if (mysqli_query($conn, $sql)) {
        echo "New record created successfully";
        header("Location: signin.php");
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}
mysqli_close($conn);
 ?>

And then i am inserting the code, i am getting this error:

Error: INSERT INTO users (email, username, password) VALUES ('gerulisjonas@gmail.com', 'jonas2422', 'password')

Thank you in advance :)

extra: Form

<form id="register" class="signinform" action="includes/registerinc.php" method="post">
            <div class="formcenter">
            <input type="text" name="username" value="" placeholder="user name"><br>
            <input type="email" name="email" value="" placeholder="email"><br>
            <input type="password" id="passwordid" name="password" value="" placeholder="password"><br>
            <input type="password" name="passwordtwo" value="" placeholder="repeat password"><br>
            <input type="submit" name="submitreg" class="btn btn-success" value="Register"></input>
            </div>
          </form>

When creating your connection you named the variable that holds the database name $database , but when you pass it along to mysqli_connect you are using $register .

Try this instead:

$database = "register";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

嘿,对不起,我打扰了你,我只是运行我的代码,发现我的register.php文件中没有包含connection.php文件。

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