好的,所以我相信我有范围问题;但是,我已经尝试了一些我在堆栈上看到的解决方案,这些解决方案与我正在尝试做的事情松散地相关而没有成功。
我还尝试将所有三个文件合并为一个,看看是否可行 - 但也没有成功。
当我尝试提交表单数据时,我会收到一些错误,例如:
错误:
警告:mysqli_real_escape_string()期望参数1为mysqli,null
警告:mysqli_query()要求参数1为mysqli,给定
非常感谢任何建议
设定:
settings.php(包含各种事物的定义常量, 包括主机,用户和数据库名称。)
config.php(包含连接函数)。
process.php(处理表单数据)。
所有三个文件都位于包含文件夹中。
的settings.php:
define ('HOST', 'localhost'); //default HOST (change to live server information)
define ('DB', 'db_name'); //default database (change to live server information)
//WRITE ACCESS USER
define ('USER_W', 'uwrite'); //default user name (change to live server information)
define ('PASS_W', '1234'); //default password (change to live server information)
//READ ACCESS USER
define ('USER_R', 'uread'); //default user name (change to live server information)
define ('PASS_R', '1234'); //default password (change to live server information)
的config.php:
include ('settings.php');
function connect($userType) {
if ($userType == 'write') {
$link = mysqli_connect(HOST, USER_W, PASS_W, DB);
}
elseif ($userType == 'read') {
$link = mysqli_connect(HOST, USER_R, PASS_R, DB);
}
if ($link -> connect_error) {
die('OOPS SOMETHING WENT WRONG!: (' . $link -> connect_errno . ')' . $link -> connect_error);
}
}
process.php:
include ('config.php');
$con = connect('write');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo "HELLO WORLD!";
} //remove after testing
// escape variables for security
$item = mysqli_real_escape_string($con, $_POST['item']);
$description = mysqli_real_escape_string($con, $_POST['description']);
$price = mysqli_real_escape_string($con, $_POST['price']);
$sql="INSERT INTO `menus` (`item`, `description`, `price`)
VALUES ('$item', '$description', '$price')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error());
}
echo "1 record added";
形式:
<form id="insertForm" action="./includes/process.php" method="post">
<fieldset>
<legend>Add New Item</legend>
<p class="red form_P_position">* Denotes required field...</p>
<p><label for="item" class="mainLabels"><span class="red">* </span>Item:</label>
<input name="item" id="item" class="mainInputs" type="text" /></p>
<p><label for="description" class="mainLabels">Description:</label>
<textarea name="description" id="description" class="mainInputs">
</textarea></p>
<p><label for="price" class="mainLabels"><span class="red">* </span>Price:</label>
<input name="price" id="price" class="mainInputs" type="text" /></p>
<p><button name="submit" id="submit" class="button" type="submit">
Add Item
</button></p>
</fieldset>
</form>
答案 0 :(得分:0)
您在connect函数中没有返回$ link,因此$ con不包含mysqli对象。你必须添加return $ link;在你的connect()函数的底部,它应该工作得很好。
function connect($userType) {
if ($userType == 'write') {
$link = mysqli_connect(HOST, USER_W, PASS_W, DB);
}
elseif ($userType == 'read') {
$link = mysqli_connect(HOST, USER_R, PASS_R, DB);
}
if ($link -> connect_error) {
die('OOPS SOMETHING WENT WRONG!: (' . $link -> connect_errno . ')' . $link -> connect_error);
}
return $link;
}