【问题标题】:php mysql.. can not assign a variable to the field name in the databasephp mysql ..无法为数据库中的字段名称分配变量
【发布时间】:2013-08-22 15:51:33
【问题描述】:

我在查询 2 表专辑和图片时需要一些帮助

我需要从数据库中选择一些字段,它们之间有一个user_id

查询:

  $album_query = mysql_query("SELECT `albums`.`album_id`, `albums`.`timestamp`, `albums`.`name`, LEFT(`albums`.`description`, 50) as `description`, COUNT(`images`.`image_id`) as `image_count`
     FROM `albums`
     LEFT JOIN `images`
     ON `albums`.`album_id` = `images`.`album_id`
     WHERE `albums`.`user_id` = '**here it must take the session created before**'
     GROUP BY `albums`.`album_id`") or die(mysql_error());

这是代码块:

<?php 

ob_start();
if(!isset($_SESSION))
{
session_start();
}

require_once('include/connect.php'); 



if(isset($_GET['user_id']))
{
    $id=$_GET['user_id'];


}
elseif(isset($_SESSION['user_id']))
{
    $id= $_SESSION['user_id'];
}

else
{
     require_once('index.php');

    exit();

}

 //function to ge the data for the album
 function album_data($album_id)
 {

 }
 //check if the album  belong to the particular user
 function album_check($album_id)
 {
 }

 //get thelist of albums 
 function get_albums()
 {

     $albums = array();
     $album_query = mysql_query("SELECT `albums`.`album_id`, `albums`.`timestamp`, `albums`.`name`, LEFT(`albums`.`description`, 50) as `description`, COUNT(`images`.`image_id`) as `image_count`
     FROM `albums`
     LEFT JOIN `images`
     ON `albums`.`album_id` = `images`.`album_id`
     WHERE `albums`.`user_id` = ''
     GROUP BY `albums`.`album_id`") or die(mysql_error());

     while($albums_row = mysql_fetch_array($album_query))
     {
         $albums[] = array(
                'id' =>$albums_row['album_id'], 
                'timestamp'=>$albums_row['timestamp'],
                'name'=>$albums_row['name'],
                'description'=>$albums_row['description'],
                'count'=>$albums_row['image_count']
         );

     }
     return $albums;
 }


       function create_album($album_name, $album_description)
 {
     $album_name= mysql_real_escape_string(htmlentities($album_name));
     $album_description= mysql_real_escape_string(htmlentities($album_description));

     mysql_query("INSERT INTO albums VALUES('', '" .$_SESSION['user_id']. "', UNIX_TIMESTAMP(), '$album_name','$album_description')");

     mkdir('uploads/'.mysql_insert_id(), 0744);
     mkdir('uploads/thumbs/'.mysql_insert_id(), 0744);


 }

?>


*elseif(isset($_SESSION['user_id']))
    {
        $id= $_SESSION['user_id'];
    }*

这是错误,因为系统不允许我分配专辑。user_id = $id 并且数据库字段存储一个 0

我不知道我是否以易于理解的方式解释了我的问题

谁能帮帮我???

【问题讨论】:

  • 在他编辑之前的第二个代码中有一个插入查询。
  • 好的,我将再次编辑问题并添加插入查询

标签: php mysql session variables


【解决方案1】:

你应该改变这一行

 WHERE `albums`.`user_id` = ''

像这样:

WHERE `albums`.`user_id` = '".$id."'

【讨论】:

  • 我添加了 '".$id."' 系统给出了这个错误 Undefined variable: id 但在代码中我确实定义了 $id
  • 如果$_GET['user_id']$_SESSION['user_id']都没有设置,你调用index.php:我想知道你是否应该使用header('Location: /index.php')。我假设在 index.php 中用户可以登录,然后设置会话变量。
  • 所以我需要删除标题??但是标题中的意思是,如果用户尝试访问此页面而不首先渴望它,它将不允许他并将他重定向到主页
  • require_once('index.php'); 更改为header('Location: /index.php'); 会将浏览器重定向到登录页面。
猜你喜欢
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多