Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php Mysql错误未知id_Php_Mysql_Error Handling - Fatal编程技术网

php Mysql错误未知id

php Mysql错误未知id,php,mysql,error-handling,Php,Mysql,Error Handling,我试图用PHP/MySQL创建一个博客,有两个表 帖子(id int(6)、cat_id int(3)、title varchar(255)、contents text) 类别(类别id int(11),名称varchar(24)) 当我尝试运行编辑函数时,在where子句“”中出现错误-“未知列”id <?php include_once('resources/init.php'); $post=get_posts($_GET['id']); if(isset($_POST['t

我试图用PHP/MySQL创建一个博客,有两个表

  • 帖子(id int(6)、cat_id int(3)、title varchar(255)、contents text)

  • 类别(类别id int(11),名称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();
}
?> 
您可能需要参考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输入的表单提交来运行。

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

检查您的url中是否设置了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();
}

检查您的url中是否设置了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();
}

检查在
$id
参数上的
edit\u post($id,$title,$contents,$category)
函数中发送的内容,如果它不是整数,则将其强制转换为
(int)
可能会完全破坏任何值。您还可以使用
isset()
检查所有其他字段是否存在,但不检查
$\u是否获得['id']
以同样的方式存在错误表明您的一个查询存在问题。请在问题中包括您的表结构。检查您发送到
$id
参数上的
edit\u post($id,$title,$contents,$category)
函数中的内容,如果它不是整数,则将其强制转换为
(int)
可能会完全破坏任何值。您还可以使用
isset()检查所有其他字段是否存在
但是您没有检查
$\u GET['id']
是否以同样的方式存在。错误表明您的一个查询存在问题。请在问题中包括您的表格结构。