John John - 1 year ago 101
MySQL Question

PHP Upload script is not posting

I have a PHP image upload script. The uploading part of the script is working, but when I try to upload the $newname of the image to the database it is not working. I dont get any error or something. It is just dont doing anything.

Here is my script


//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100");

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;

//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.

//checks if the form has been submitted

//reads the name of the file the user submitted for uploading

//if it is not empty
if ($image)

//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);

//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);

//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))

//print error message
echo '<h1>Unknown extension!</h1>';

//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
echo '<h1>You have exceeded the size limit!</h1>';

//we will give an unique name, for example the time in unix time format

//the new name will be containing the full path where will be stored (images folder)

//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
echo '<h1>Copy unsuccessfull!</h1>';

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
echo "<h1>File Uploaded Successfully!</h1>";


/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "db");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());

$sql = "UPDATE users SET logo='" . $newname . "' WHERE user_id='" . $_SESSION['USER_ID'] ."'";

// Close connection


Can someone help me to solve this problem?

Answer Source

Firstly, for $sql = "UPDATE users SET...

You never queried.


$sql = mysqli_query($link, "UPDATE users SET ...");

and check for errors on PHP and MySQL and that the session array does have a value and that the column is long enough to hold the data.

If (and for example) WHERE user_id=1"; works, then your session array is empty and was never set.

You may also be outputting before header, so place session_start(); at the top of your script. Which is what's going on here. Your code puts on the brakes as soon as it hits starting the session and failed on you silently.

Consult these following links and and apply that to your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download