identicon identicon - 3 months ago 25
MySQL Question

Warning: mysqli_query(): Couldn't fetch mysqli in simple php page

I am new in PHP. I was creating the crud in angular js using PHP. Everything's are going good from angular side. But problem is getting from PHP Page. mysqli_query() is not inserting my values in database. Please check the link for error. http://prntscr.com/cdxnr8
I am using Xampp local server.

<?php

include 'config.php';
$data = json_decode(file_get_contents("php://input"));
/*$first_name= mysql_real_escape_string($data->fname);
$last_name= mysql_real_escape_string($data->lname);
$username= mysql_real_escape_string($data->username);
$email= mysql_real_escape_string($data->email);
$password= mysql_real_escape_string($data->password);*/

$first_name= 'new first name';
$last_name= 'new last name';
$username= 'new username';
$email= 'new email';
$password='new password';

$sql = "INSERT INTO 'account' (first_name,last_name,email,username,password) VALUES ('$first_name','$last_name','$email','$username','$password')";

/* mysqli_query($conn, $sql); */

if (!mysqli_query($conn,$sql))
{
echo("Error description: " .$sql ."<br />". mysqli_error($conn, $sql));
}


?>


config.php

<?php

$servername= 'localhost';
$username= 'root';
$password= '';
$datbase= 'angularDb';

$conn= mysqli_connect($servername,$username,$password);
mysqli_select_db($conn, $datbase);
if (!$conn) {
die('Could not connect:' . mysql_error());
}
echo 'Connected successfully';

$conn->close();

?>


I am new in PHP. I was creating the crud in angular js using PHP. Everything's are going good from angular side. But problem is getting from PHP Page. mysqli_query() is not inserting my values in database. Please check the link for error. http://prntscr.com/cdxnr8
I am using Xampp local server.

Answer

Remove the quotes around the table name. Replace with backticks or leave the name bare.

Change:

"INSERT INTO 'account' ...";

To:

"INSERT INTO `account` ...";

As @Nehal J Wani pointed out, you are also making the mistake of closing your connection within config.php, so no query can be run! Remove the line below:

$conn->close();