Reputation: 23
I'm getting the above error in regards to my statement below, tried multiple fixes from other answers in stack but continue to get the same error :(
this is part of a php code in a user registration form, would like their info to insert in the table
if($conn->query( "INSERT INTO users (FIRST_NAME, LAST_NAME, USERNAME, PASSWORD)
VALUES ('$fname', '$lname', '$uname', '$pword')")==TRUE){
echo 'Inserted';
}else{
echo 'Not Inserted';
}
i have this other code in a separate file for the $conn
function dbConnect(){
$servername = "x";
$database = "activity2";
$username = "root";
$password = "root";
// Create connection
$conn = new
mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);
} //echo "Connection successful"; //make variable global to access in other
functions global $conn; return $conn;}
Upvotes: 1
Views: 52
Reputation:
First thing to do is to create the class that will return a connection:
<?php
//filename: dbConnect.php
class dbConnect
{
public function connect() {
global $conn;
$servername = "localhost";
$username = "root";
$password = "";
$database = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} //echo "Connection successful"; //make variable global to access in other
return $conn;
}
}
?>
Now you can execute your sql command in another file:
<?php
require 'dbConnect.php';
$db = new dbConnect();
$conn = $db->connect();
$nome = "Anailton";
$email = "[email protected]";
if($conn->query( "INSERT INTO clientes (NOME, EMAIL) VALUES ('$nome', '$email')")==TRUE){
echo 'Inserted';
}else{
echo 'Not Inserted';
}
?>
Upvotes: 1