-2

I don't know how PHP functions work. I have this function:

function runQueryMeta($tabelM, $metaName, $id_post, $metaValue, $check_ok_M){
$result = $conn -> query("SELECT * FROM ".$tabelM." WHERE post_id=".$id_post." AND meta_key='".$metaName."'");
if ($result->num_rows > 0){
    $conn -> query("UPDATE ".$tabelM." SET meta_value='".$metaValue."' WHERE post_id='".$id_post."' AND meta_key='".$metaName."'");
}else {
    $conn -> query("INSERT INTO ".$tabelM." (post_id, meta_key, meta_value) VALUES ('".$id_post."', '".$metaName."', '".$metaValue."')");
}
$check_ok_M = 1;
$result -> free_result();
return $check_ok_M;

}

I have this call for the function:

$nume_ok = runQueryMeta('mnsa_usermeta', 'nume_organizatie_companie', $item_id, $_POST['f_nume']);

And i have this function that connects to the database:

function OpenCon(){
$dbhost = "host";
$dbuser = "user";
$dbpass = "pass";
$db = "db";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
                     
return $conn;

}

with this call

$conn = OpenCon();

But i have this error: Failed to load resource: the server responded with a status of 500 () when i run the first fuction.

Kratos
  • 33
  • 4
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Aug 16 '20 at 21:13
  • Your code is a bit buggy. The variable $conn should be passed as a parameter to runQueryMeta() and you should read the advise above for SQL injection. And finally, you mysql server is down or something. – Sébastien Bémelmans Aug 16 '20 at 21:36

1 Answers1

-2

You must call the connect db function inside runQueryMeta function. Is the openConn function has same class with runQueryMeta? if it so, you just call it

function runQueryMeta($tabelM, $metaName, $id_post, $metaValue, $check_ok_M) {
    $conn = $this->OpenCon();
Dharman
  • 30,962
  • 25
  • 85
  • 135
Garaulo
  • 11
  • 3