2015-10-17 85 views
1

我试图在PHP/MySQL的一个博客,并得到了2桌PHP MySQL错误未知ID

  • 职位(ID INT(6),CAT_ID INT(3),标题为varchar(255 ),内容文本)

  • 类别(cat_id int(11),name varchar(24))。

当我尝试运行编辑功能时,出现错误 - '未知列'id''where'子句''。

<?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; 
} 

我已经提到提供了此类错误的网站上的解决方案,但这对我的情况没有帮助。请帮忙。

+0

检查您发送到'edit_post什么($ ID,$标题,$内容,$类)'函数的'$ id'参数,如果它不是一个整数,那么将其转换为'(int)'可能会破坏任何值 – RiggsFolly

+0

您还使用'isset()'检查所有其他字段,但不检查$ _GET [ 'id']'以同样的方式存在 – RiggsFolly

+0

错误表明其中一个查询存在问题。请在你的问题中包含你的表格结构。 – cpburnz

回答

0

检查,如果ID在你的URL设置,试试这个方法

include_once('resources/init.php'); 

if(isset($_GET['id'],$_POST['title'],$_POST['contents'],$_POST['category'])) 
{ 
    $post=get_posts($_GET['id']); 
    $title=trim($_POST['title']); 
    $contents=trim($_POST['contents']); 

    edit_post($_GET['id'],$title,$contents,$_POST['category']); 

    $id = $post[0]['posts.id']; 
    echo $id; 
    header("Location:index.php?id=".$id); 
    die(); 
} 
0

你不应该直接运行你的第一个脚本,它应该与表单提交与ID输入里面运行。