0

common.php

function connection() {
    $servername = "db********.db.1and1.com";
    $username = "dbo********";
    $password = "********";
    $dbname = "db********";
    $connection = new mysqli($servername, $username, $password, $dbname);
    mysqli_set_charset($connection, "utf8");
    if ($connection->connect_error) {
        die($connection->connect_error);
    } 
}

category.php

include('../model/common.php');
connection();

$name = $_POST["catname"];

$sql = "INSERT INTO `category` (name) VALUES ('".$name."')" ;

if (mysqli_query($connection, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}


mysqli_close($connection);

I submitted form via jQuery ajax and it return this error:

Error: INSERT INTO `category` (name) VALUES ('some test value')

but if i put whole code of connection() function above my query without including common.php it works fine! I'm guessing there may be some global issue. I have also tried to global $connection or using singleton class but no success.

20
  • first check you common.php file is including or not!! Commented Mar 2, 2017 at 5:51
  • @prakashtank yes i checked, it included Commented Mar 2, 2017 at 5:51
  • 3
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Mar 2, 2017 at 6:03
  • 2
    As a note, it's a rough road if you want to stick with just core PHP and mysqli, which is perhaps the weakest database interface PHP has. PHP the Right Way provides a lot of advice on better ways to tackle this, and it's also worth exploring development frameworks if you're committed to learning PHP. Laravel is particularly beginner friendly, it's well documented, and has fantastic community support. You won't have to get tangled up in things like this with Eloquent. Commented Mar 2, 2017 at 6:08
  • 1
    Hell, even PDO is a much better API for working with database queries and it's built right in Commented Mar 2, 2017 at 6:14

2 Answers 2

2
  1. return $connection

    if ($connection->connect_error) {
      die($connection->connect_error);
    }
    return $connection
    
  2. Assign to new $connection variable

    $connection = connection();
    
Sign up to request clarification or add additional context in comments.

4 Comments

`problem solved, I forgot to assign function into a variable. thanks
It's likely related to the backticks on the return line, which are not supposed to be there.
@tadman ah, I missed those! Formatting code in lists isn't always obvious. I imagine OP was trying their best
@Phil I'm just speculating that's the problem, but it looks correct now. Nice fix!
1

The connection() function creates a context or scope. Variables will only exist for the duration of the function, so $connection wont be available below. See this thread for more information about variable scope. One solution would be to add global $connection at the top of the function to allow $connection to be set outside the connection() function. You could simply omit the function entirely, and simply include the file to set that information.

function connection() {
    global $connection; //Add this line
    $servername = "db********.db.1and1.com";
    $username = "dbo********";
    $password = "********";
    $dbname = "db********";
    $connection = new mysqli($servername, $username, $password, $dbname);
    mysqli_set_charset($connection, "utf8");
    if ($connection->connect_error) {
        die($connection->connect_error);
    } 
}

2 Comments

It would be much better if connection() simply returned $connection
@Phil Apples and oranges. He could simply omit the function entirely, and just include common.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.