无法用php将数据插入mysql

时间:2018-05-18 14:37:42

标签: php mysqli

所以我正在处理这个简单的项目,我不知道为什么,但我无法将数据插入数据库

这是我与数据库的连接

 <?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";
?>

在这部分代码中我试图插入数据:

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

然后我插入代码,我收到此错误:

  

错误:INSERT INTO用户(电子邮件,用户名,密码)VALUES(&#39; gerulisjonas@gmail.com',&#39; jonas2422&#39;,&#39;密码&#39;)

提前谢谢你:)

额外: 表格

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

2 个答案:

答案 0 :(得分:3)

创建连接时,您命名了包含数据库名称$database的变量,但是当您将其传递给mysqli_connect时,您使用的是$register

请改为尝试:

$database = "register";

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

答案 1 :(得分:0)

嘿伙计们抱歉我打扰了你,我只是通过我的代码运行,我发现我没有在register.php文件中包含connection.php文件