No database selected - php and phpmyadmin - php

I am trying to connect my php code to my database and once I input and submit things like username, email address, and password in my html form. They will be sent to my database and shown in specific columns and there will be text like "New record has been created successfully" on my page. But for now, an error occurs instead.
The following is my php code
<?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";
}
?>
The following is the error output shown on my web page:
Connected successfullyErrors INSERT INTO registereduser (id, username, emailaddress, hashedpassword) VALUES (0, Josh5577, josh1998#hotmail.com, $2y$10$RMasEdVOskmcXbmfchLeBeUzLa5l38jXFaCQN7vMEhR8A0mU/iiC6)
No database selected

Please use below code for connection
$conn = new mysqli($servername, $username, $password, $dbname);

You can try to change
// Create connection
$conn = new mysqli($servername, $dbname);
to
// Create connection
$conn = new mysqli($servername, $username, $password);
mysqli_select_db($conn, $dbname);

Related

Inserting data into mySQL database through PHP is not working

I've been trying to figure out how to insert data into mySQL database for a long time. When I try to insert data, it returns "no database selected". I'm not too sure what's wrong with the code, could someone check it out?
<?php
$servername = "localhost";
$database= "learnsc2_ts";
$username = "learnsc2_admin";
$password = "Ts#123";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
echo "Connection successful";
}
$query = "INSERT INTO users(fname, lname) VALUES ('Owen',
'Feng')";
mysqli_query($conn, $query);
if (mysqli_query($conn, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);?>
Make sure your database name is correct.
i tested it in my local, It's work Just fine.
$servername = "localhost";
$database= "test";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "INSERT INTO users(fname, lname) VALUES ('Owen',
'Feng')";
$query = mysqli_query($conn, $query);
if ($query) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
You forgot to add database name
$conn = new mysqli($servername, $username, $password, $database);
I figured it out. Something was wrong with the old username I was using. After changing to a new username and database, it worked out!

php sql insert blank values

Whenever I submit my php form, the data shows up blank in the database. What is not working? I've tried to setup the code different, but no matter what I do, the values ends up being blank.
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST["email"]))
{
$login = $_POST["email"];
}
else
{
$login = null;
}
if (isset($_POST["psw"]))
{
$psw = $_POST["psw"];
}
else
{
$psw = null;
}
$login2 = mysqli_real_escape_string($conn, $login);
$psw2 = mysqli_real_escape_string($conn, $psw);
$verify = "INSERT INTO test (email, password) VALUES ('$login2', '$psw2')";
$verify2 = mysqli_query($conn, $verify);
$conn->close();
?>
Try this:
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['login']) and isset($_POST['email'])){
$insert = mysqli_query("INSERT INTO test VALUES('".$_POST['login']."', '".$_POST['email']."')");
if($insert){
echo "Dados inseridos.";
}
}
$conn->close();

Trying to check if an email with the same value already exists

Im trying to make so it checks if the email already exists in the database before inserting the data in it.
Here is my code:
<?php
$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";
$Mail = $_POST['email'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Inputed in E-Mail Field
$Mail = $_POST['email'];
$SearchEmail = $conn->query("SELECT (Mail) FROM betakey WHERE Mail = '$Mail'");
if ($SearchEmail->num_rows > 0) {
print "That Email is already registered for the closed alpha";
}
else {
$conn->query("INSERT INTO betakey VALUES ('$Mail')");
}
$conn->close();
?>
It doesn't give me any error when i access the page but it also doesn't echo that it exists or inserts the data when it doesn't.
The problem is in your insert query. An insert query is always structured like shown below.
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
See: http://www.w3schools.com/php/php_mysql_insert.asp
Applying this to your code, results in the following.
<?php
$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";
$Mail = $_POST['email'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Inputed in E-Mail Field
$Mail = $_POST['email'];
$SearchEmail = $conn->query("SELECT count(Mail) FROM betakey WHERE Mail = '$Mail'");
if ($SearchEmail->num_rows > 0) {
print "That Email is already registered for the closed alpha";
}
else {
$conn->query("INSERT INTO betakey (Mail) VALUES ('$Mail')");
}
$conn->close();
?>
And I suggest you take away one of the two '$Mail = $_POST['email'];', because it is useless to declare a variable twice in the same way.

mysql will not insert past fourth row

I'm not exactly sure what happened but this database and the php effecting it were working just fine until it hit the fourth row and now it won't insert new records at all.
if($_POST)
{
$servername = ******;
$username = ******;
$password = ******;
$db = ******;
$conn = mysqli_connect($servername, $username, $password, $db);
mysqli_select_db($conn,$db);
$uuid = $_POST['uuid'];
$sql = "INSERT INTO uuid VALUES ('$uuid');";
mysqli_query($conn,$sql);
mysqli_close($conn);
}
I'm not sure what happened but this is the relevant code for the mysqli query.
try this
<?php
if(isset($_POST['uuid']))
{
$servername = yourServerName;
$username = username;
$password = password;
$dbname = databaseName;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$uuid = $_POST['uuid'];
$sql = "INSERT INTO tableName (columnName) VALUES ('$uuid')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
Also, I recommend using prepared statements.

Php to MySQL database

I have problem with MySQL database, I can't insert the information into the table. My php code seems to work, but when I run it nothing happens.
<?php
$servername = "localhost";
$fname = "fname";
$lname = "lname";
$klas = "klas";
$nomer = "nomer";
$file = "dom";
$dbname = "homeworks";
$conn = new mysqli($servername, $fname, $lname,$klas,$file,$dbname);
$sql = "INSERT INTO student (fname, lname,klas,file)
VALUES ($servername, $fname, $lname,$klas,$file,)";
?>
You have three main problems in your code:
You're still not connected to the database
Only constructing and not executing
Having not matched parameters in the insert values
Solution :
1. Make a connection first
$conn = new mysqli($servername, $username, $password, $dbname);
The Parameter $servername, $username, $password, $dbname is obviously your hostname, Database Username, Password and the Database name
You should not have your table name or column names in the connection parameters
2. Construct the parameters which matches the coloumn name and variables correctly
$sql = "INSERT INTO student (fname, lname,klas,file)
VALUES ($fname, $lname,$klas,$file)";
3. Execute Your Query :
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Note :
Also it's good practice to close your connection once you are done
$conn->close();
So, you should be having something like this
<?php
$servername = "localhost";
$username = "YourDBUsername";
$password = "YourDBPassword";
$fname = "fname";
$lname = "lname";
$klas = "klas";
$nomer = "nomer";
$file = "dom";
$dbname = "homeworks"; //Hope you will have your db name here
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "
INSERT INTO student (fname, lname,klas,file) VALUES
('$fname'
,'$lname'
,'$klas'
,'$file');
";
if ($conn->query($sql) === TRUE) {
echo "New record inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
Advice :
Always use prepared statements else clean your inputs before you insert.
Your connection should look something like this. link
<?php
//change the data into your connection data
$conn = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
You made your query but didn't execute it.
if (mysqli_query($conn, $sql)) {
echo 'records created successfully<br>';
} else {
echo $sql . '"<br>"' . mysqli_error($conn);
}

Categories