简体   繁体   English

php Mysql错误未知ID

[英]php Mysql Error unknown id

I'm trying to make a blog in PHP/MySQL and have got 2 tables 我正在尝试在PHP / MySQL中创建一个博客,并且有2个表

  • posts (id int(6),cat_id int(3),title varchar(255),contents text) 帖子(id int(6),cat_id int(3),标题varchar(255),内容文本)

  • categories (cat_id int(11),name varchar(24)). 类别(cat_id int(11),名称varchar(24))。

I am getting an error - 'Unknown column 'id' in 'where clause'' when I try to run edit function. 尝试运行编辑功能时,出现错误-“ 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();
}
?> 

Here is the edit function - 这是编辑功能-

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}");
}

You might need to refer get_posts function- 您可能需要引用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;
}

I have referred the solutions provided for this type of error on the site but it wasn't helpful for my case. 我已经在网站上介绍了针对此类错误提供的解决方案,但对我的情况没有帮助。 Please help. 请帮忙。

您不应该直接运行第一个脚本,而应使用内部带有ID输入的表单提交来运行它。

check if Id is set in your url, try this approach 检查是否在您的网址中设置了ID,请尝试这种方法

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM