Solved

update query problem for image upload

Posted on 2011-03-08
6
396 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
  • 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now