Reputation: 59
I want to get user name from a database table from his id and put it in other data table
function upload_image($image_temp, $image_ext, $album_id, $image_n, $image_description) {
$album_id = (int)$album_id;
$image_n = mysql_real_escape_string(htmlentities($image_n));
$image_description = mysql_real_escape_string(htmlentities($image_description));
//$download_link = 'uploads/'. $album_id. '/'. $image['id']. '.'. $image_ext;
$mysql_date_now = date("Y-m-d (H:i:s)");
$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
mysql_query("INSERT INTO `images` VALUES ('', '".$_SESSION['user_id']."','$user_name', '$album_id', UNIX_TIMESTAMP(), '$image_ext', '$image_n', '$image_description', '','$mysql_date_now')");
$image_id = mysql_insert_id();
$download_link = 'uploads/'. $album_id. '/'. $image_id. '.'. $image_ext;
mysql_query("UPDATE `images` SET `download_link`='$download_link' WHERE `image_id`=$image_id ");
$selection = mysql_query("SELECT `user_two` FROM `follow` WHERE `user_one`='".$_SESSION['user_id']."'");
while ($row = mysql_fetch_array($selection)) {
mysql_query("INSERT INTO `notification` VALUES ('', '".$_SESSION['user_id']."', '".$row['user_two']."', '', UNIX_TIMESTAMP(), '$image_n', '$image_description', '$download_link')");
}
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);
Thumbnail('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/');
}
the problem is here
$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
mysql_query("INSERT INTO `images` VALUES ('', '".$_SESSION['user_id']."','$user_name', '$album_id', UNIX_TIMESTAMP(), '$image_ext', '$image_n', '$image_description', '','$mysql_date_now')");
i get this in database (Resource id #14)
Upvotes: 0
Views: 806
Reputation:
Your query doesn't return data: it returns a resource. You then have to work with the resource to retrieve your data, so in this line:
$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
$user_name doesn't contain the information you want.
Try:
$result = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
list($user_name) = mysql_fetch_array($result);
Note: mysql is deprecated - use mysqli or PDO. The principle is the same.
Upvotes: 2
Reputation: 1016
I think you should look here: MySQL syntax for Join Update
Or here: MySQL Insert & Joins
You could do this in just one query with some joining of the tables.
Upvotes: -1