Dynadot

Image Uploads but won't Update

Spaceship Spaceship
Watch
Hi All

I have made a script that uploads images fine, it also inserts the values of the image into a database i.e. image name etc. I'd like to update these records using another form that I have designed.

However, it uploads the image without a problem but will not update the the MySQL database.
Can anyone help with this below is the code:

I don't get any error messages when I run the form. It's just not updating the MySQL database.

The Form

PHP:
<?php    

    include ("../Connections/connect.php");

mysql_select_db($database_connect, $connect);

   

    if (isset($_GET['id'])){

       

         $this_page_id = $_GET['id'];

       

    $page_query = mysql_query ("SELECT * FROM co_pic_main_uploads WHERE id = $this_page_id") or die(mysql_error());

   

        $page_content = mysql_fetch_array($page_query);

       

        echo '<img src=' . $page_content['path'] . ' width="250" height="300" >

    <form action="pic_change.php" method="post" enctype="multipart/form-data">

  <p><br  />

     <label for="file">Select a file:</label>

     <input type="file" name="userfile" id="file">

     <br />

    

     <button name="upload">Upload File</button>

   <p>

</form>

    <a href="co_los_front.php">Back to Images</a>';

    

   

    } else {

           

   

    $query_news = mysql_query ("SELECT * FROM co_pic_main_uploads ORDER BY id")or die(mysql_error());

        echo '<ul>';



    while($row = mysql_fetch_array($query_news)) {

    echo '<li><a href=co_los_front.php?id=' . $row['id'] . '><img src=' . $row['path'] . ' width="72" height="72" ></a></li>';

    }

            echo '</ul>';



    }

   

    ?>

Upload and Update Script

PHP:
<?php

   

   

   



   

    // Configuration - Your Options /*

      $allowed_filetypes = array('.jpg','.gif','.bmp','.png','.jpeg'); // These will be the types of file that will pass the validation.

      $max_filesize = 15242880; // Maximum filesize in BYTES (currently 5.0MB).

      $upload_path = '../images/uploads/'; // The place the files will be uploaded to (currently a 'files' directory).



     $fileid = $_GET['userfile']['id'];

   $filename = $_GET['userfile']['name']; // Get the name of the file (including file extension).

   $tmpname = $_GET['userfile']['tmp_name']; // Give it a temporary name

    $filesize = $_GET['userfile']['size']; // Get the file size

    $filetype = $_GET['userfile']['type']; // Get the file type

    $filepath = $upload_path . $filename;

   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

   $result = move_uploaded_file($tmpname, $filepath);



    if (!$result) {

    echo "Error uploading file";

    exit;



   // Check if the filetype is allowed, if not DIE and inform the user.

   if(!in_array($ext,$allowed_filetypes))

      die('The file you attempted to upload is not allowed.');



   // Now check the filesize, if it is too large then DIE and inform the user.

   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)

      die('The file you attempted to upload is too large.');



   // Check if we can upload to the specified path, if not DIE and inform the user.

   if(!is_writable($upload_path))

      die('You cannot upload to the specified directory, please CHMOD it to 777.');



   // Upload the file to your specified path.

   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))

         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a> <br>

             <p>Once your done you can simply click the link below</p>

    <p><img src="http://www.namepros.com/images/PNG/onebit_34.png" width="48" height="48" /><br />

      <a href="co_admin.php">Return Home </a></p> '; // It worked.

      else

         echo 'There was an error during the file upload.  Please try again.'; // It failed :(.

                                                                                             

    }

    //Connect to my SQL                                                                                         

    include ("../Connections/connect.php");

   

     if(!get_magic_quotes_gpc())

{

$upload_path = addslashes($upload_path);

}







// Insert data obtained above into table

if (isset($_GET['id'])){

       

        $this_page_id = $_GET['id'];



$query = mysql_query("UPDATE co_pic_main_uploads

        SET name ='$filename', size='$filesize', type='$filetype', path='$filepath' WHERE id='$fileid'")or die('Error, query failed : ' . mysql_error());

    }





echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a> <br>

             <p>Once your done you can simply click the link below</p>

    <p><img src="http://www.namepros.com/images/PNG/onebit_34.png" width="48" height="48" /><br />

      <a href="co_admin.php">Return Home </a></p> '; // It worked.





   

   



?>
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
This block of code is definitely wrong:

Code:
if (isset($_GET['id'])){ 

         

        $this_page_id = $_GET['id']; 



$query = mysql_query("UPDATE co_pic_main_uploads  

        SET name ='$filename', size='$filesize', type='$filetype', path='$filepath' WHERE id='$fileid'")or die('Error, query failed : ' . mysql_error()); 

    }

If you are passing the 'id' parameter to the upload&update script, then you should use it in 'WHERE' clause like
Code:
... WHERE id='$this_page_id'

In other case, you might need to add a hidden field to your upload form, containting an id of the image and use it in your upload&update script correspondingly.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back