Jump to content

change insert script to modify/update script


Lisa23

Recommended Posts

Hi i have this upload script which works fine it uploads image to a specified folder and sends the the details to the database.

but now i am trying to instead make a modify script which is Update set so i tried to change insert to update but didnt work can someone help me out please

this my insert image script which works fine but want to change to modify instead

<?php
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("upload") or die(mysql_error()) ;

// my file the name of the input area on the form type is the extension of the file
//echo $_FILES["myfile"]["type"];

//myfile is the name of the input area on the form 
$name = $_FILES["image"] ["name"]; // name of the file
$type = $_FILES["image"]["type"]; //type of the file
$size = $_FILES["image"]["size"]; //the size of the file
$temp = $_FILES["image"]["tmp_name"];//temporary file location when click upload it temporary stores on the computer and gives it a temporary name
$error =array(); // this an empty array where you can then call on all of the error messages
$allowed_exts = array('jpg', 'jpeg', 'png', 'gif'); // array with the following extension name values
$image_type = array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'); // array with the following image type values
$location = 'images/'; //location of the file or directory where the file will be stored
$appendic_name = "news".$name;//this append the word [news] before the name so the image would be news[nameofimage].gif

// substr counts the number of carachters and then you the specify how how many you letters you want to cut off from the beginning of the word example drivers.jpg it would cut off dri, and would display vers.jpg
//echo $extension = substr($name, 3);

//using both substr and strpos, strpos it will delete anything before the dot in this case it finds the dot on the $name file deletes and + 1 says read after the last letter you delete because you want to display the letters after the dot. if remove the +1 it will display .gif which what we want is just gif
$extension = strtolower(substr($name, strpos ($name, '.') +1));//strlower turn the extension non capital in case extension is capital example JPG will strtolower will make jpg
// another way of doing is with explode
// $image_ext strtolower(end(explode('.',$name))); will explode from where you want in this case from the dot adn end will display from the end after the explode

$myfile = $_POST["myfile"];

     if (isset($image)) // if you choose a file name do the if bellow
       {
       
       // if extension is not equal to any of the variables in the array $allowed_exts error appears
        if(in_array($extension, $allowed_exts) === false )
       {
         $error[] = 'Extension not allowed! gif, jpg, jpeg, png only<br />'; // if no errror read next if line
       }
        // if file type is not equal to any of the variables in array $image_type error appears
        if(in_array($type, $image_type) === false)
       {
          $error[] = 'Type of file not allowed! only images allowed<br />';     
       }
       
        // if file bigger than the number bellow error message
        if($size > 2097152)
       {
          $error[] = 'File size must be under 2MB!';     
       }
       
       // check if  folder exist in the server
        if(!file_exists ($location))
       {
          $error[] = 'No directory ' . $location. ' on the server Please create a folder ' .$location;     
       }
            
     }
     // if no error found do the move upload function
       if (empty($error)){
           if (move_uploaded_file($temp, $location .$appendic_name))
           {
             // insert data into database first are the field name teh values are the variables you want to insert into those fields appendic is the new name of the image
mysql_query("INSERT INTO image (myfile ,image)
VALUES ('$myfile', '$appendic_name')") ;
           exit();
           }
         }
       else
          {
        foreach ($error as $error)
           {
               echo $error;
           }
        }
      
     
  
//echo $type;
?>

 

 

Link to comment
Share on other sites

For an update you're going to want to pass the image id to the processing script so the script knows which record is being updated.  You would end up with something like this.

mysql_query("UPDATE image SET myfile='$myfile', image='$appendic_name' WHERE id=$imageid");

Link to comment
Share on other sites

Thanks its working just one problem it only update the detail if i upload an a new image if i dnt update an image it doesnt update

the form at moment just has two fields ID, MYFILE, IMAGE

if i dnt update a new image the it doesnt upload is there a way i can set if no image upload just update myfile field??

Link to comment
Share on other sites

Ok i think i'm almost there the problem now is if no image is uploaded it updates the database to null because no image has been entered is there a way that i can do if no image enter keep details of old image this what i've done for the rest of the update to work

mysql_query("UPDATE image SET myfile='$myfile', image='$appendic_name' WHERE id=$id");
     if (isset($image)) // if you choose a file name do the if bellow
       {

Link to comment
Share on other sites

Ok i managed to get it the update script to work just one thing i am a bit concern about i had to add two update set line on the script so not sure if that is correct thing to do, the reason i had to do that is because i have a upload restriction which include if file empty appear error message and dont run the script for security reason. but because user might just modify the text fields the script wont run cz the image field will be empty. so i had to include an update for the fields also outside those image restriction. my question is is this valid way of doing or could be a more effiecient way.

here teh whoel script first update line is is just before the (  if (isset($image))) and then after that and all the image check is the second update is that reliable way of doing it???

<?php
/* this is a normal update without image as a start example 
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("upload") or die(mysql_error()) ;


$id=$_POST['id'];
$myfile=$_POST['myfile'];
$name = $_FILES['name'];


$query="UPDATE image SET myfile='$myfile', name='$name' WHERE id='$id'";
mysql_query($query);
echo "Record Updated";
mysql_close();
*/
?>

<?php
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("upload") or die(mysql_error()) ;

// my file the name of the input area on the form type is the extension of the file
//echo $_FILES["myfile"]["type"];

//myfile is the name of the input area on the form 
$name = $_FILES["image"]["name"]; // name of the file
$type = $_FILES["image"]["type"]; //type of the file
$size = $_FILES["image"]["size"]; //the size of the file
$temp = $_FILES["image"]["tmp_name"];//temporary file location when click upload it temporary stores on the computer and gives it a temporary name
$error =array(); // this an empty array where you can then call on all of the error messages
$allowed_exts = array('jpg', 'jpeg', 'png', 'gif'); // array with the following extension name values
$image_type = array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'); // array with the following image type values
$location = 'images/'; //location of the file or directory where the file will be stored
$appendic_name = "news".$name;//this append the word [news] before the name so the image would be news[nameofimage].gif

// substr counts the number of carachters and then you the specify how how many you letters you want to cut off from the beginning of the word example drivers.jpg it would cut off dri, and would display vers.jpg
//echo $extension = substr($name, 3);

//using both substr and strpos, strpos it will delete anything before the dot in this case it finds the dot on the $name file deletes and + 1 says read after the last letter you delete because you want to display the letters after the dot. if remove the +1 it will display .gif which what we want is just gif
$extension = strtolower(substr($name, strpos ($name, '.') +1));//strlower turn the extension non capital in case extension is capital example JPG will strtolower will make jpg
// another way of doing is with explode
// $image_ext strtolower(end(explode('.',$name))); will explode from where you want in this case from the dot adn end will display from the end after the explode

$id = $_POST["id"];
$myfile = $_POST["myfile"];
$image = $_FILES['image'];	
mysql_query("UPDATE image SET myfile='$myfile' WHERE id=$id"); // update here otherwise if no image wont update the rest of the fields
     if (isset($image)) // if you choose a file name do the if bellow
       {
   	   	    
	// if extension is not equal to any of the variables in the array $allowed_exts error appears
        if(in_array($extension, $allowed_exts) === false )
       {
         $error[] = 'Extension not allowed! gif, jpg, jpeg, png only<br />'; // if no errror read next if line
       }
        // if file type is not equal to any of the variables in array $image_type error appears
        if(in_array($type, $image_type) === false)
       {
          $error[] = 'Type of file not allowed! only images allowed<br />';     
       }
       
        // if file bigger than the number bellow error message
        if($size > 2097152)
       {
          $error[] = 'File size must be under 2MB!';     
       }
       
       // check if  folder exist in the server
        if(!file_exists ($location))
       {
          $error[] = 'No directory ' . $location. ' on the server Please create a folder ' .$location;     
       }
            
     }
     // if no error found do the move upload function
       if (empty($error)){
           if (move_uploaded_file($temp, $location .$appendic_name))
           {
             // update data into database first are the field name teh values are the variables you want to insert into those fields appendic is the new name of the image
		 $sql = "SELECT image
              FROM image
              WHERE id = $id";

      $result = mysql_query($sql) 
                or die('Error, get details info failed. ' . 
                        mysql_error());
      $row = mysql_fetch_assoc($result);
      unlink($location . $row['image']);

   } else {
      // don't change the image
  $appendic_name = "image";
   }

   $query = "UPDATE image 
               SET myfile  = '$myfile', 
                 image = '$appendic_name'
             WHERE id = $id";

   mysql_query($query) 
  	 or die('Error, modify details failed : ' . mysql_error());
           }
       
       else
          {
        foreach ($error as $error)
           {
               echo $error;
           }
        }
  
     
  
//echo $type;
?>

Link to comment
Share on other sites

Hi i've fixed the problem so just posting here in case anyone ever needed and in case someone spot something not right on the script but so far working 100% thanks for all the helps

<?php
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("your_database_name") or die(mysql_error()) ;

// my file the name of the input area on the form type is the extension of the file
//echo $_FILES["myfile"]["type"];

//myfile is the name of the input area on the form 

$name = $_FILES["image"]["name"]; // name of the file
$type = $_FILES["image"]["type"]; //type of the file
$size = $_FILES["image"]["size"]; //the size of the file
$temp = $_FILES["image"]["tmp_name"];//temporary file location when click upload it temporary stores on the computer and gives it a temporary name
$error =array(); // this an empty array where you can then call on all of the error messages
$allowed_exts = array('jpg', 'jpeg', 'png', 'gif'); // array with the following extension name values
$image_type = array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'); // array with the following image type values
$location = 'images/'; //location of the file or directory where the file will be stored
$appendic_name = "news".$name;//this append the word [news] before the name so the image would be news[nameofimage].gif

// substr counts the number of carachters and then you the specify how how many you letters you want to cut off from the beginning of the word example drivers.jpg it would cut off dri, and would display vers.jpg
//echo $extension = substr($name, 3);

//using both substr and strpos, strpos it will delete anything before the dot in this case it finds the dot on the $name file deletes and + 1 says read after the last letter you delete because you want to display the letters after the dot. if remove the +1 it will display .gif which what we want is just gif
$extension = strtolower(substr($name, strpos ($name, '.') +1));//strlower turn the extension non capital in case extension is capital example JPG will strtolower will make jpg
// another way of doing is with explode
// $image_ext strtolower(end(explode('.',$name))); will explode from where you want in this case from the dot adn end will display from the end after the explode

$id = $_POST["id"];
$myfile = $_POST["myfile"];
$image = $_FILES['image'];

if($name=="") // if name is empty just update these fields on the database
{	
     $query = "UPDATE upload 
               SET myfile  = '$myfile'
             WHERE id = '$id'";

   mysql_query($query) 
  	 or die('Error, modify details failed : ' . mysql_error());	
}
else // if image has name execute these lines
{
   	   	    
	// if extension is not equal to any of the variables in the array $allowed_exts error appears
        if(in_array($extension, $allowed_exts) === false )
       {
         $error[] = 'Extension not allowed! gif, jpg, jpeg, png only<br />'; // if no errror read next if line
       }
        // if file type is not equal to any of the variables in array $image_type error appears
        if(in_array($type, $image_type) === false)
       {
          $error[] = 'Type of file not allowed! only images allowed<br />';     
       }
       
        // if file bigger than the number bellow error message
        if($size > 2097152)
       {
          $error[] = 'File size must be under 2MB!';     
       }
       
       // check if  folder exist in the server
        if(!file_exists ($location))
       {
          $error[] = 'No directory ' . $location. ' on the server Please create a folder ' .$location;     
       }            
     // if no error found do the move upload function
       if (empty($error)){
           move_uploaded_file($temp, $location .$appendic_name);
           
             // update data into database first are the field name teh values are the variables you want to insert into those fields appendic is the new name of the image
		 $sql = "SELECT image
              FROM upload
              WHERE id = '$id'";

      $result = mysql_query($sql) 
                or die('Error, get details info failed. ' . 
                        mysql_error());
      $row = mysql_fetch_assoc($result);
      unlink($location . $row['image']);//remove the image from the folder remmebr $location is 
     $query = "UPDATE upload 
               SET myfile  = '$myfile', 
                 image = '$appendic_name'
             WHERE id = '$id'";

   mysql_query($query) 
  	 or die('Error, modify details failed : ' . mysql_error());
           }
   
       else
          {
        foreach ($error as $error)
           {
               echo $error;
           }
	  }
}

//echo $type;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.