我对php和MySQL有点新鲜。我正在阅读教程并在单击编辑主题按钮时收到以下错误消息。我将包含我正在使用的所有适用代码。我很确定问题在于数据库连接,因为显示的错误是从connection.php页面打印出来的。
错误:
数据库连接失败:您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第1行的“1”附近使用正确的语法
数据库:
I have 1 database(widget_corp) with 3 tables
Tables:
subjects(id, menu_name, position, visible),
pages(id, subject_id, menu_name, position, visible, content),
users(id, username, hashed_password) //this one is not used yet
源代码:
<?PHP require_once("includes/connection.php"); ?>
<?PHP require_once("includes/functions.php"); ?>
<?PHP
if(isset($_POST['submit'])) {
$errors = array();
$required_fields = array('menu_name', 'position', 'visible');
foreach($required_fields as $fieldname) {
if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) {
$errors[] = $fieldname;
}
}
$fields_with_lengths = array('menu_name' => 30);
foreach($fields_with_lengths as $fieldname => $maxlength) {
if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
$errors[] = $fieldname; }
}
if (empty($errors)) {
// Perform Update
$id = mysql_prep($_GET['subj']);
$menu_name = mysql_prep($_POST['menu_name']);
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = {$visible} WHERE id = {$id}";
$result = mysql_query($query, $connection);
if(mysql_affected_rows() == 1) {
// Sucess
} else {
// Failed
}
} else {
} // end: if (isset($_POST['submit']))
}
?>
<?PHP find_selected_page(); ?>
<?PHP include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<?PHP echo navigation($sel_subject, $sel_page); ?>
</td>
<td id="page">
<h2>Edit Subject: <?PHP echo $sel_subject['menu_name']; ?></h2>
<form action="edit_subject.php?subj="<?PHP echo urlencode($sel_subject['id']); ?>" method="post">
<p>Subject name:
<input type="text" name="menu_name" value="<?PHP echo $sel_subject['menu_name']; ?>" id="menu_name" />
</p>
<p>Position:
<select name="position">
<?PHP
$subject_set = get_all_subjects();
$subject_count = mysql_num_rows($subject_set);
// $subject_count +1 b/c we are adding a subject
for($count=1; $count <= $subject_count+1; $count++) {
echo "<option value=\"{$count}\"";
if ($sel_subject['position'] == $count) {
echo " selected";
}
echo ">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0"<?PHP
if($sel_subject['visible'] == 0) { echo " checked"; }
?>/> No
<input type="radio" name="visible" value="1" <?PHP
if($sel_subject['visible'] ==1) {echo " checked"; }
?> /> Yes
</p>
<input type="submit" name="submit" value="Edit Subject" />
</form>
<br />
<a href="content.php">Cancel</a>
</td>
</tr>
</table>
<?PHP require("includes/footer.php"); ?>
来自functions.php的适用函数
function redirect_to($location = NULL) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
function navigation($sel_subject, $sel_page) {
$output = "<ul class=\"subjects\" >";
$subject_set = get_all_subjects();
while ($subject = mysql_fetch_array($subject_set)) {
$output .= "<li";
if ($subject["id"] == $sel_subject["id"]) {$output .= " class=\"selected\"";}
$output .= "><a href=\"edit_subject.php?subj=" . urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"]);
$output .= "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
$output .= "<li";
if ($page['id']==$sel_page['id']) {$output .= " class=\"selected\"";}
$output .= "><a href=\"content.php?page=" . urlencode($page["id"]) . "\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}
function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
if ($new_enough_php) { //PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if($magic_quotes_active) { $value = stripslashes($value); }
$value = mysql_real_escape_string($value);
} else { //before PHP v4.3.0
// if magic quotes aren't already on then add slahses manually
if( !$magic_quotes_active ) { $value = addslashes($value); }
// if magic quotes are active, then the slashes already exist
}
return $value;
}
function get_subject_by_id($subject_id){
global $connection;
$query = 'SELECT * FROM `subjects` WHERE `id`= ' . (int)$subject_id . 'LIMIT 1';
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
//if no rowes are returned, fetch array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
function get_pages_for_subject($subject_id) {
global $connection;
$query = "Select *
FROM pages
WHERE subject_id={$subject_id}
ORDER BY position ASC";
$pages_set = mysql_query($query, $connection);
confirm_query($pages_set);
return $pages_set;
}
function get_page_by_id($page_id){
global $connection;
$query = 'SELECT * FROM `pages` WHERE `id` = ' . (int)$page_id . ' LIMIT 1';
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
//if no rowes are returned, fetch array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
function get_all_subjects() {
global $connection;
$query = "SELECT * FROM subjects ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
return $subject_set;
}
function find_selected_page() {
global $sel_subject;
global $sel_page;
if (isset($_GET['subj'])) {
$sel_subject = get_subject_by_id($_GET['subj']);
$sel_page = NULL;
} else if (isset($_GET['page'])) {
$sel_subject = NULL;
$sel_page = get_page_by_id($_GET['page']);
} else{
$sel_subject = NULL;
$sel_page = NULL;
}
}
function confirm_query($result_set) {
if (!$result_set) {
die("Database connection failed: " . mysql_error());
}
}
包含的页面
header.php
<html>
<head>
<title>Widget Corp</title>
<link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<h1>Widget Corp</h1>
</div>
<div id="main"></div></body></html>
footer.php
</div>
<div id="footer">Copyright 2007, Widget Corp</div>
</body>
</html>
<?PHP
if (isset($connection)){
mysql_close($connection);
}
?>
connection.php
<?PHP
require("constants.php");
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db(DB_NAME, $connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
?>
constants.php
<?PHP
// Database Constants
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "********");
define("DB_NAME", "widget_corp");
?>
答案 0 :(得分:5)
看起来在functions.php中,你有一行
$query = 'SELECT * FROM `subjects` WHERE `id`= ' . (int)$subject_id . 'LIMIT 1';
并且LIMIT
之前没有空格。尝试将'LIMIT 1'
更改为' LIMIT 1'
,看看会发生什么。
要更快地发现这些错误,您需要添加一些错误检查:
$result_set = mysql_query($query, $connection);
if (!$result_set) {
echo "mysql_query() error: ", mysql_error($connection), "\nquery was: $query\n";
die();
}
然后,您将看到生成的SQL,它允许快速发现错误。