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.
common.phpfile is including or not!!mysqliyou should be using parameterized queries andbind_paramto 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,$_GETor any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake.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.