Having trouble with procedural use of mysqli
Here's the function:
db.php
<?php
define("DB_HOST", "host");
define("DB_LOGIN", "login");
define("DB_PASSWORD", "password");
define("DB_NAME", "myDb");
$link = mysqli_connect(DB_HOST, DB_LOGIN, DB_PASSWORD, DB_NAME);
if(!$link) {
echo 'ERROR: ' . mysqli_connect_errno() . ': ' . mysqli_connect_error();
}
?>
function.php
<?php
function addItemToCatalog($var1, $var2, $var3, $var4) {
$sql = 'INSERT INTO catalog (var1, var2, var3, var4)
VALUES (?, ?, ?, ?)';
if (!$stmt = mysqli_prepare($link, $sql)){
return false;
}
mysqli_stmt_bind_param($stmt, "ssii", $var1, $var2, $var3, $var4);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
return true;
}
?>
page.php
<?php
require_once ("db.php");
require_once ("function.php");
$var1 = $_POST['var1']; //showing without filtering methods
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
$var4 = $_POST['var4'];
if(!addItemToCatalog($var1, $var2, $var3, $var4)){
echo 'some error text';
}
else {
header("Location: success.php");
exit;
}
?>
After using this there's
WARNING: mysqli_prepare() expects parameter 1 to be mysqli, null given in function.php on line 5.
Anyone?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…