在特定页面上显示图片

问题描述:

我想让用户上传图片,并仅在他们刚刚创建的活动页面上显示他们上传的图片。为此,我在PHP中创建了一个数据库,并创建了所有事件。每个事件都有一个25个字符的随机密钥,以使它们具有唯一性。在特定页面上显示图片

我的问题是,我试图通过使用随机密钥(称为«MomentEvent»)在正确的事件中上传图片,但它总是只在一个事件中更新,这是列表中的第一个事件。

那么,任何想法如何我可以通过使用MomentEvent键在正确的事件上传图片?

以下页面是将图片转换为缩略图并在表格中更新的位置,第二页是图片应显示的位置。

ajax_image.php

<?php 
include('db.php'); 
session_start(); 
$session_id=$_SESSION['id']; // Or Session ID 
$session_MomentEvent=$_SESSION['MomentEvent']; // Or Session ID 

$actual_image_names = time().substr($txt, 9).".".$ext; 
$t_width = 450; // Maximum thumbnail width 
$t_height = 150; // Maximum thumbnail height 
$new_name = "$actual_image_names".$session_MomentEvent.".jpg"; // Thumbnail image name 
$path = "images/"; 

if(isset($_GET['t']) and $_GET['t'] == "ajax"){ 
    extract($_GET); 
    $ratio = ($t_width/$w); 
    $nw = ceil($w * $ratio); 
    $nh = ceil($h * $ratio); 
    $nimg = imagecreatetruecolor($nw,$nh); 
    $im_src = imagecreatefromjpeg($path.$img); 
    imagecopyresampled($nimg,$im_src,0,0,$x1,$y1,$nw,$nh,$w,$h); 
    imagejpeg($nimg,$path.$new_name,90); 
    mysql_query("UPDATE users_event SET image_small='$new_name' WHERE MomentEvent='$session_MomentEvent'"); 
    echo $new_name."?".time(); 
    exit(); 
} 
?> 

show_picture.php

<?php 
    include('base.php'); 

    //We check if the users ID is defined 
    if(isset($_GET['MomentEvent'])) 
    { 
     $MomentEvent = $_GET['MomentEvent']; 
     //We check if the user exists 
     $sql = "select ID, TitreEvent, DescriptionEvent, LieuEvent, image_small from users_event where MomentEvent='$MomentEvent'"; 
     $dn = mysql_query($sql); 
     if(mysql_num_rows($dn)>0) 
     { 
      $dnn = mysql_fetch_array($dn); 
      //We display the user datas 
      { // <---- EDITOR: WHAT IS THAT ? You wanted to put a closing brace (}) ?? 
      echo "<div class='container';><img src=http://www.*******.com/images/".$dnn ['image_small'] ." HEIGHT='150px' WIDTH='450px'></div> <br>"; 

      } 
     } //<-- EDITOR: THIS closing braces were missing 
    } // <-- EDITOR: THIS one too 
?> 
+0

什么是$ txt和$ text ..? –

+0

- 总体看来逻辑,你应该var_dump()MomentEvent,我认为问题可能在那里。 - 您应该避免使用mysql_函数,它们已被弃用,并且对SQL注入不安全。看看mysqli_函数,或者更好的PDO。 - 您应该避免使用提取,因为您将来自己或某个人维护您的代码可能会感到困惑,因为您不知道变量来自哪里(这被认为是一些人的不良做法)。 - 你有最古怪的制表符我见过xDD很酷,但它令人困惑:P – aleation

+0

嘿@freddy,我只是编辑了你的代码以提高可读性,而且你有很多奇怪的大括号({)放置和未关闭如果是,也为mysql_query(在ajax_image.php)存储它的返回值,true或false,特别是如果您正在调试。 – aleation

更多信息

这个问题似乎是$session_MomentEvent=$_SESSION['MomentEvent']; // Or Session ID,这样用户似乎总是有相同的MomentEvent值,无论他正在处理什么事件。

因此,更新仅在表中的第一条记录上完成,因此是结果。您提供的代码不完整以解决谜题。但有理由表users_event有每个新输入的每个事件的唯一ID。为什么不是这个ID只是链接到该事件的ID,他的工作对:

mysql_query("UPDATE users_event SET image_small='$new_name' WHERE MomentEvent='$ID'"); 

凡$ ID是在任何时候知道的值,使得选择值:

$sql = "select ID, TitreEvent, DescriptionEvent, LieuEvent,image_small from users_event where MomentEvent='$ID'"; 

链接动态数据将会话变量与Ajax请求结合使用是棘手的业务。尝试省略$_SESSION['MomentEvent'],并为您的Ajax请求使用ID而不是$ _GET值。

是您正在试图用相同的MomentEvent更新数据库中多行的问题,或者是错误的数据库中的行正在更新?

查询mysql_query("UPDATE users_event SET image_small='$new_name' WHERE MomentEvent='$session_MomentEvent'");是否成功执行?

我的建议是要仔细检查$_SESSION['MomentEvent']设置正确 - 所以更新该行()你要更新,也许能使ajax_image.php返回一个JSON字符串详细介绍了SQL查询的结果,并检查您的AJAX脚本请确保上传已经走向了正确的事件

喜欢的东西:

<?php 
    header('Content-type: application/json'); 
    $result = array(); 

    include('db.php'); 
    session_start(); 

    $session_id = $_SESSION['id']; // Or Session ID 

    $session_MomentEvent = $_SESSION['MomentEvent']; // Or Session ID 
    $response['MomentEvent'] = $session_MomentEvent; 

    $actual_image_names = time().substr($txt, 9).".".$ext; 
    $t_width = 450; // Maximum thumbnail width 
    $t_height = 150; // Maximum thumbnail height 
    $new_name = "$actual_image_names".$session_MomentEvent.".jpg"; // Thumbnail image name 
    $path = "images/"; 

    if(isset($_GET['t']) and $_GET['t'] == "ajax") { 
     // Query and save image 
     extract($_GET); 
     $ratio = ($t_width/$w); 
     $nw = ceil($w * $ratio); 
     $nh = ceil($h * $ratio); 
     $nimg = imagecreatetruecolor($nw,$nh); 
     $im_src = imagecreatefromjpeg($path.$img); 
     imagecopyresampled($nimg,$im_src,0,0,$x1,$y1,$nw,$nh,$w,$h); 

     if($result = mysql_query("UPDATE users_event SET image_small='$new_name' WHERE MomentEvent='$session_MomentEvent'")) { 
      // Save image 
      $response['status'] = 'success'; 
      $response['updated'] = mysql_num_rows($result); 

      imagejpeg($nimg,$path.$new_name,90); 

      $response['result'] = $new_name."?".time(); 
     } 
    } 
    else { 
     $response['status'] = 'failed'; 
    } 

    echo json_encode($response); 
?> 

然后检查你的JSON响应,看看你做了什么?最好的办法,我能想到的调试这个问题,但我又不是100%清楚是什么问题

希望帮助队友,任何你能提供,我会尽力帮助调试这进一步

尝试var_dump MomentEvent并查看究竟返回的是什么。

你说它只更新列表中的第一个事件,这可能是因为你需要一个适当的引用,所以连接到MomentEvent(外键)的ID主键。

第二种选择是用foreach循环遍历每个事件,然后根据匹配的MomentEvent键和会话中用户的ID在每个事件中检查随机键,然后在第二页上显示用户映像。

只需重新修改你的逻辑,你就应该走上正轨。