我正在尝试创建一个函数来使用 mysqli(对我来说是全新的)检查 mysql 中的重复条目,但我无法解决这个错误。我有 3 个 php 文件:db.php - db connection functions.php 和我的函数(需要 db.php)和 submit.php - 接受输入的文件。
db.php:
<?php
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "user"); // The database username.
define("PASSWORD", "pass"); // The database password.
define("DATABASE", "db_list"); // The database name.
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
?>
函数.php:
require 'db.php';
function check($str, $mysqli) {
$checkdupe = $mysqli->prepare("SELECT name FROM list WHERE str = ?"); //line giving me error
$checkdupe->bind_param("s", $str);
$checkdupe->execute();
$checkdupe->store_result();
if ($checkdupe->num_rows > 0) {
//dupe found
return true;
} else {
//no dupe
return false;
}
}
提交.php
require 'functions.php';
if (isset($_POST['name'])) {
if (check($_POST['name'], $mysqli) == true) { //added $mysqli parameter
echo "success!";
} else {
echo "fail;
}
} else {
echo 'invalid post';
}
?>