我搜索过类似的问题但无法找到答案,虽然我是初学者,所以可能错过了。
我正在尝试调用review_create()
函数,其中包含了包含此函数的reviews.php
。但是,我收到了错误
致命错误:在C:\ xampp \ htdocs \ cafe \ review.php中调用未定义的函数query()
用于user_pages.php的PHP:
<?php
require_once 'review.php';
require_once 'cafe.php';
require_once 'logged_in.php';
$RATING_MAP = array('5' => 'Excellent', '4' => 'Good', '3' => 'Ok', '2' => 'Bad', '1' => 'Awful', '0' => '---');
$error = array();//holds multiple errors
$id = $rating = $review = $action = '';
$self = $_SERVER['PHP_SELF'];
if (isset($_REQUEST['action'])) {
$action = $_REQUEST['action'];
}
if ($action == 'delete') {
if (isset($_REQUEST['id'])) {
review_delete($_REQUEST['id'], $error);
} else {
$error[count($error)] = "Cannot delete the review, missing ID!";
}
}
elseif ($action == 'create') {
if ((isset($_REQUEST['rating']))
&& (isset($_REQUEST['review'])
&& (isset($_REQUEST['review_cafe'])))) {
$rating = $_REQUEST['rating'];
$review = $_REQUEST['review'];
$review_cafe = $_REQUEST['review_cafe'];
if ($rating == '---' or $review == '' or $review_cafe == '') {
$error[count($error)] = "You must provide a cafe, rating and review!";
} else {
review_create($review_cafe, $rating, $review, $error);
}
}
else {
$error[count($error)] = "Unable to create review, missing parameters!";
}
}
?>
用于reviews.php的PHP:
<?php
require_once "sql.php";
function review_create($review_cafe, $rating, $review, &$error) {
$query = "INSERT INTO Reviews (cafe, rating, review) VALUES (". "'$review_cafe', $rating, '$review');";
$success = query($query, $error);
return $success;
}
?>
这是sql.php,其中定义了$ query:
<?php //sql.php
require_once "sql_constants.php";
class Sql {
private static $connection = false;
private static $error; // holds the last error.
static function getConnection() {
if (!Sql::$connection) {
Sql::setConnection(mysqli_connect(SQLHOST, SQLUSER, SQLPASSWORD, HOSTDB));
if (!Sql::$connection) {
Sql::setConnection(false);
Sql::setError("Could not connect...");
}
}
return Sql::$connection;
}
private static function setConnection($iConnection) {
Sql::$connection = $iConnection;
}
private static function setError($iError) {
Sql::$error = $iError;
}
static function getError() {
return Sql::$error;
}
static function query($query) {
$result = false;
Sql::setError(false); // reset the error
if ($link = Sql::getConnection()) {
$result = mysqli_query($link, $query);
if (!$result)
Sql::setError(mysqli_error($link));
}
return $result;
}
static function getInsertID() {
// Returns the last id automatically inserted into the
// database.
return mysqli_insert_id(Sql::getConnection());
}
}
?>
干杯 詹姆斯
答案 0 :(得分:1)
错误 - $success = query($query, $error);
应该是 - $success = mysql_query($query, $error);
除非你有一个执行查询的方法query()
答案 1 :(得分:1)
避免使用mysql
,而是使用 mysqli_并更新代码以使用mysqli_query
代替query
(这是错误!! )
简单地做:
return (mysqli_query( $conn, $query )); //$conn is connection object
或者,如果您对结果有疑问,请使用mysqli_error()
$success = mysqli_query($con, $query);
if ( $success === false)
{
printf("Error is : ", mysqli_error($con));
}
else
{
return ($success);
}
答案 2 :(得分:1)
我更喜欢使用PDO或MYSQLI而不是它,这很容易注入。 (http://en.wikipedia.org/wiki/SQL_injection)请参阅http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers