【发布时间】:2015-10-17 12:24:03
【问题描述】:
我正在尝试在 PHP/MySQL 中创建一个博客,并且有 2 个表
posts (id int(6),cat_id int(3),title varchar(255),contents text)
类别(cat_id int(11),name varchar(24))。
当我尝试运行编辑功能时,出现错误 - 'where 子句'中的未知列 'id'。
<?php
include_once('resources/init.php');
$post=get_posts($_GET['id']);
if(isset($_POST['title'],$_POST['contents'],$_POST['category']))
{
$title=trim($_POST['title']);
$contents=trim($_POST['contents']);
edit_post($_GET['id'],$title,$contents,$_POST['category']);
header("Location:index.php?id=$post[0]['posts.id']");
die();
}
?>
这里是编辑功能-
function edit_post($id,$title,$contents,$category)
{
$id=(int)$id;
$title=mysql_real_escape_string($title);
$category=(int)$category;
$contents=mysql_real_escape_string($contents);
mysql_query("UPDATE posts SET cat_id= {$category},
title='{$title}',
contents='{$contents}'
WHERE id={$id}");
}
你可能需要参考get_posts函数-
function get_posts($id=null,$cat_id=null){
$posts=array();
$query=("SELECT posts.id AS post_id, categories.cat_id AS category_id,
title, contents,
categories.name AS name
FROM posts
INNER JOIN categories ON categories.cat_id = posts.cat_id");
if (isset($id)) {
$id=(int)$id;
$query .= " WHERE posts.id={$id}";
}
if(isset($cat_id)) {
$cat_id=(int)$cat_id;
$query .=" WHERE categories.cat_id={$cat_id}";
}
$query .= " ORDER BY posts.id DESC";
$query = mysql_query($query);
while($row=mysql_fetch_assoc($query)) {
$posts[]=$row;
}
return $posts;
}
我已经在网站上提到了针对此类错误提供的解决方案,但这对我的情况没有帮助。请帮忙。
【问题讨论】:
-
检查您在
$id参数上发送到edit_post($id,$title,$contents,$category)函数的内容,如果它不是整数,则将其转换为(int)可能会破坏任何值 -
您还使用
isset()检查所有其他字段是否存在,但您没有检查$_GET['id']是否以相同的方式存在 -
该错误表明您的其中一个查询存在问题。请在您的问题中包含您的表结构。
标签: php mysql error-handling