Solved

update query problem for image upload

Posted on 2011-03-08
6
403 Views
Last Modified: 2012-05-11
hey all. i found this image upload resizing script and am trying to add a bit to it to save the url to my images table in my db.

but its not updating the row in the table not sure why can u help please:


CREATE TABLE IF NOT EXISTS `images` (
  `PictureID` int(4) NOT NULL auto_increment,
  `MemberID` int(11) default NULL,
  `imgLarge` varchar(255) default NULL,
  `imgSmall` varchar(255) default NULL,
  PRIMARY KEY  (`PictureID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `images`
--

INSERT INTO `images` (`PictureID`, `MemberID`, `imgLarge`, `imgSmall`) VALUES
(3, 52, 'images/sml_71745-1920x1080-RED-SKULL.jpg', 'images/71745-1920x1080-RED-SKULL.jpg');


 

<form action="<?php echo $_server['php-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
        <p>Change Profile picture:</p>
        <p>Picture must be a JPEG</p>
        <p>
          <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
      </p>
        <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
</form>
<?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
                     global $message;
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "images/".$imagename;
              move_uploaded_file($source, $target);
 
              $imagepath = $imagename;
              $save = "images/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ;
 
              $modwidth = 286;
              $modheight = 269;
              $tn = imagecreatetruecolor($modwidth, $modheight) ;
              $image = imagecreatefromjpeg($file) ;
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
 
              imagejpeg($tn, $save, 100) ;
 
              $save = "images/sml_" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ;
 
              $modwidth = 80;
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff;
              $tn = imagecreatetruecolor($modwidth, $modheight) ;
              $image = imagecreatefromjpeg($file) ;
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
 
              imagejpeg($tn, $save, 100) ;
            echo "Large image: <img src='images/".$imagepath."'><br>";
            echo "Thumbnail: <img src='images/sml_".$imagepath."'>";
                  
                   include("conf.php");
    $connection = mysql_connect($databaseURL,$databaseUName,$databasePWord);
        // or die ("Error while connecting to localhost");
    $db = mysql_select_db($databaseName,$connection);
        //or die ("Error while connecting to database");
            
                  

            $query = "UPDATE `images` SET `imgLarge` = '".$file."', `imgSmall` = '".$save."' WHERE memberID = '".$MemberID."'";
            //$insert = mysql_query("INSERT INTO images (MemberID, imgLarge, imgSmall) VALUES ('$MemberID','$save', '$file')");
 mysql_close($connection);
          }
        }
?>
0
Comment
Question by:helpchrisplz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 1

Author Comment

by:helpchrisplz
ID: 35073572
i only want one user to have one profile picture at one time. please note the last query is commented out
0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 35073620
and i haven't shown a query for how i populate $MemberID but its from another bit of code above
0
 
LVL 13

Expert Comment

by:dsmile
ID: 35079594
If you're running on a *nix OS, then case sensitive may be the problem

Try change this

$query = "UPDATE `images` SET `imgLarge` = '".$file."', `imgSmall` = '".$save."' WHERE memberID = '".$MemberID."'";

to this

$query = "UPDATE `images` SET `imgLarge` = '".$file."', `imgSmall` = '".$save."' WHERE MemberID = '".$MemberID."'";

or

$query = "UPDATE `images` SET `imgLarge` = '".$file."', `imgSmall` = '".$save."' WHERE `MemberID` = '".$MemberID."'";
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 1

Author Comment

by:helpchrisplz
ID: 35083367
i have got this far please help:

i have changed the names in my table to lower case but still have a capital if there is a second word.

CREATE TABLE IF NOT EXISTS `images` (
  `pictureId` int(4) NOT NULL auto_increment,
  `memberId` int(11) default NULL,
  `imgLarge` varchar(255) default NULL,
  `imgSmall` varchar(255) default NULL,
  PRIMARY KEY  (`pictureId`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `images`
--

INSERT INTO `images` (`pictureId`, `memberId`, `imgLarge`, `imgSmall`) VALUES
(3, 52, 'images/sml_71745-1920x1080-RED-SKUL.jpg', 'images/71745-1920x1080-RED-SKULL.jpg');



and this is my code as i have it now.
 i have echoed out all the variables used in the update query to check they are actually populated and they all are containing what they should be so am just unsure why this update is not actually updating:

$queryOne = mysql_query ("SELECT pictureId FROM images WHERE memberId = $MemberID");
            

$row = mysql_fetch_array($queryOne);

$pictureId = $row['pictureId'];
echo $pictureId;
            

$UPDATE = "UPDATE `images` SET `imgLarge` = '".$file."', `imgSmall` = '".$save."', `memberId` = '".$MemberID."' WHERE `images`.`pictureId` = '".$pictureId."'";

            
 echo $file; // this does echo the url for the large file but update query does not save it to db
 echo $save; // this does echo the url for the small file but update query does not save it to db
0
 
LVL 1

Accepted Solution

by:
helpchrisplz earned 0 total points
ID: 35088427
fixed: i was silly and forgot the mysql_connect *bangs head*
0
 
LVL 1

Author Closing Comment

by:helpchrisplz
ID: 35126411
mysql_connect was missing
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question