未选择数据库-php和phpmyadmin

fsi0uk1n  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(237)

我试图连接我的php代码到我的数据库,一旦我输入和提交的东西,如用户名,电子邮件地址,密码在我的html格式。它们将被发送到我的数据库,并在特定的列中显示,并将在我的页面上出现类似“新记录已成功创建”的文本。但现在,出现了一个错误。
下面是我的php代码

<?php
$visitorname = $emailaddress = $visitorpassword = $confirmpassword = "";

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb";

if($_SERVER["REQUEST_METHOD"] == "POST"){
$visitorname = input($_POST["visitorname"]);
$emailaddress = input($_POST["emailaddress"]);
$visitorpassword = input($_POST["visitorpassword"]);
$confirmpassword = input($_POST["confirmpassword"]);
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
}

function input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
if($visitorpassword == $confirmpassword){

// Create connection
$conn = new mysqli($servername, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$id = mysqli_insert_id($conn);
$sql = "INSERT INTO registereduser (id, username, emailaddress, 
hashedpassword) VALUES ($id, $visitorname, $emailaddress, 
$hashed_password)";

if ($conn->query($sql) === TRUE){

    echo "New record created successfully";
}else{
    echo "Errors " . $sql . "<br>" . $conn->error;
}
$conn->close();
}else{
    echo "Passwords do not match";
}
?>

以下是我的网页上显示的错误输出:
connected successfullyerrors插入registereduser(id、用户名、电子邮件地址、哈希密码)值(0、5577、,josh1998@hotmail.com,$2y$10$rmasedvoskmcxbmfchlebeuzla5l38jxfacqn7vmehr8a0mu/iic6)未选择数据库

qcbq4gxm

qcbq4gxm1#

你可以试着改变

// Create connection
$conn = new mysqli($servername, $dbname);

// Create connection
$conn =  new mysqli($servername, $username, $password);
mysqli_select_db($conn, $dbname);
stszievb

stszievb2#

请使用下面的代码连接

$conn = new mysqli($servername, $username, $password, $dbname);

相关问题