Jump to content

Code Error


jasonhardwick

Recommended Posts

Hi, I'm trying to build an add/edit area for an image that a user will update so i have and "edit_profile_image.php" file that contains the image form and a "update_profile_image.php" that contains the code below. I also am trying to change the name of the file to the users ID but it dosent work...

<?php

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$sql=( "UPDATE $tbl_name SET user_image = '$user_image' WHERE uname='$username'");
$rows = mysql_query($sql);

$id = $rows['id'];
$user_image = $rows['user_image'];

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

$ext = findexts ($_FILES['user_image']['name']) ;

$new_name = $rows['uname'].".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "user_images/";
//This combines the directory, the random file name, and the extension
$target = $target . $new_name.$ext;
echo $id;
if(move_uploaded_file($_FILES['user_image']['tmp_name'], $target)) 
{
echo "The file has been uploaded as ".$new_name.$ext;
} 
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/106154-code-error/
Share on other sites

your doing an update not a select thus

$rows['uname'] = null

 

EDIT:

try this (uses username instead)

<?php

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$user_image = $rows['user_image'];

$ext = findexts ($_FILES['user_image']['name']) ;

#$new_name = $rows['uname'].".";
$new_name = $username."."; //working replacement

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "user_images/";
//This combines the directory, the random file name, and the extension
$target = $target . $new_name.$ext;
if(move_uploaded_file($_FILES['user_image']['tmp_name'], $target)) 
{
//May need to add target to below (depends on other code)
$sql=( "UPDATE $tbl_name SET user_image = '".$new_name.$ext."' WHERE uname='$username'");
$rows = mysql_query($sql);
echo "The file has been uploaded as ".$new_name.$ext;
#$id = $rows['id'];//pointless
$id = mysql_insert_id();
echo $id;

} 
else
{
echo "Sorry, there was a problem uploading your file.";
}

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

?>

Link to comment
https://forums.phpfreaks.com/topic/106154-code-error/#findComment-544105
Share on other sites

Ok so i figgured out what my problem was... my session wasnt starting... now the file is uploaded to the right folder with the new name but it dosent change in the database

<?php
session_start();
//$_SESSION['username'] = "Joe Shmo";
if(!isset($_SESSION['username'])) {
header("location:index1.php");
exit;
}

?>

<?php



include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$sql=( "UPDATE $tbl_name SET user_image = '$new_target' WHERE uname='{$_SESSION['username']}'");
//$sql2= "SELECT *  from $tbl_name WHERE uname = '{$_SESSION['username']}' ";
$new_target=$_POST.$target;
//$rows = mysql_query($sql2);

//$id = $rows['id'];
$user_image = $new_name.$ext;

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

$ext = findexts ($_FILES['user_image']['name']) ;

$new_name = $username.".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target1 = "user_images/";
//This combines the directory, the random file name, and the extension
$target = $target1 . $new_name.$ext;

if(move_uploaded_file($_FILES['user_image']['tmp_name'], $target)) 
{
echo "The file has been uploaded as ".$new_name.$ext;
} 
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/106154-code-error/#findComment-544437
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.