Reputation: 157
I want to let users upload a picture, and show the picture they uploaded only on the event page that they just created. For that, I have a database in PHP with all the events created. Each event have a random key of 25 characters, to make them unique.
My problem, is that I try to upload the picture in the correct event, by using that random key (called «MomentEvent»), but it always update only in one event, which is the first one in the list.
So, any idea how I can upload the picture in the correct event, by using the MomentEvent key?
The following pages is where the picture is transformed to a thumbnail and updated in the table, and the second page is where the picture is supposed to appear.
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
?>
Upvotes: 3
Views: 357
Reputation: 141
Try to var_dump MomentEvent and see what exactly is being returned.
You say that it only updates the first event in your list which could possibly be that you need a proper reference so a primary key of ID linked to MomentEvent(foreign key).
Second option is to loop through each event with a foreach and have it actually check for the random key per event then based on the matched MomentEvent key and ID of user in the session do the appearance of the users image on second page.
Just rework your logic a bit and you should be on track.
Upvotes: 0
Reputation: 2355
The problem seems to be the $session_MomentEvent=$_SESSION['MomentEvent']; // Or Session ID
in this way that the user seems to have always the same MomentEvent value, whatever event he's working on.
As thus, the update is only done on the first record in the table, hence the outcome. The code you provided is incomplete to solve the riddle. But it stands to reason that the table users_event
has an unique id for every event with every NEW input. Why not simply link this id to the ID of the event he's working on:
mysql_query("UPDATE users_event SET image_small='$new_name' WHERE MomentEvent='$ID'");
Where the $ID is at any time a know value, making the select value:
$sql = "select ID, TitreEvent, DescriptionEvent, LieuEvent,image_small from users_event where MomentEvent='$ID'";
Linking dynamic data to session variables in combination with Ajax-requests is tricky business. Try to omit $_SESSION['MomentEvent']
and use the ID instead with $_GET values for your Ajax-requests.
Upvotes: 1
Reputation: 514
Is the problem that you are trying to update multiple rows in the database with the same MomentEvent or is the wrong row in the database being updated?
Does the query mysql_query("UPDATE users_event SET image_small='$new_name' WHERE MomentEvent='$session_MomentEvent'");
execute successfully?
My suggestion is to double check that $_SESSION['MomentEvent']
is set correctly - and so updating the row(s) you want to update, perhaps get ajax_image.php
to return a JSON string detailing the SQL query results and check that in your AJAX script to make sure the upload has gone to the correct event
Something like:
<?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);
?>
Then check your JSON response and see what you are getting? Best way I can think of debugging this issue but again I'm not 100% clear what the issue is
Hope that helps mate, any more information you can provide and I'll try and help debug this further
Upvotes: 0