Jump to content

mysqli to mysql image upload error


fypstudent

Recommended Posts

Hi we are students doing a staff web portal that allows the staff to edit their profile and upload their photo. Initially we did all the php codes in mysqli, because of the server problem, we have to change them all to mysql. However, we faced some problem during the transitions.

 

This is shown from the original mysqli code which is the correct one. After the user upload the photo, an uploaded photo would be shown on the screen. under "your current picture is"

http://www.flickr.com/photos/75437207@N00/2861731732/in/photostream/

 

But after we change the codes to mysql.

http://www.flickr.com/photos/75437207@N00/2861731814/in/photostream/

we got a problem here, when we upload the photo, it doesn't display any uploaded photo, and the layout stays the same. How should i fix it?

<?php 
session_start();
echo $name;
ob_start();
?>

<html>
<head>
<title>Staff Signage</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">

<style type="text/css">
<!--
a:link {
color: #000000;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #000000;
}
a:hover {
text-decoration: underline;
color: #333333;
}
a:active {
text-decoration: none;
}
-->
</style></head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!-- ImageReady Slices (main1.psd) -->
<table width="800" height="600" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
<tr>
	<td width="800" height="136"><?php include("include/header.php") ?></td>
  </tr>
<tr>
  <td width="800" height="438" valign="top" bgcolor="#FFFFFF">
        <table width="796" height="426" border="0">
          <tr>
            <td width="5" height="2"></td>
            <td width="741"> </td>
            <td width="15"></td>
            <td width="17"></td>
          </tr>
          <tr>
            <td height="409"></td>
            <td><p>
                <?php
				$submitted = isset($_POST['formSubmitted']);
				$success = FALSE;	

$staffid=$_SESSION['staffid'];
/////////////////////////////////////SQLI///////////////////////////////////
				// Connect to database
				/*$mysqli = new mysqli("localhost", "iminds_isc", "commitment", "imindss_temp");
				// Prepare statement
				$stmt = $mysqli->prepare("SELECT photo FROM staff WHERE staffid=?");

				// Bind parameters
				$stmt->bind_param ("s", $staffid);

				$stmt->execute();
				$stmt->store_result();
				$stmt->bind_result($photo);
				$stmt->fetch();

				$stmt->close();
				$mysqli->close();*/

				require_once('staffSql.php');

				mysql_select_db($dbname);

				$_SESSION['staffid'] = $staffid;

				//Query the database to see if the given username/password combination is valid.
				$query = "SELECT photo FROM staff WHERE staffid='$staffid'";

				$result = mysql_query($query);	 	



				//define a maxim size for the uploaded images in Kb 
	define ("MAX_SIZE","81920"); 
//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. 
	 $errors=0;
	 //checks if the form has been submitted
	  if(isset($_POST['Submit']))  { 
	  		
	  //reads the name of the file the user submitted for uploading 	
		  $image=$_FILES['image']['name']; 	
		  
		 if (empty($image))  {

			 echo '<span class= "contentText"><font color="red"><center><b>You forgot to browse a image.</b></center></font><br><span>';

			 $errors=1;
		 }



		  //if it is not empty 	
		  if ($image)  	{ 	
		  		$errors=0;
			  //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,  	
			  //otherwise we will do more tests 
				if (($extension != "jpg") && ($extension != "jpeg")){	
				$errors=1;	
			  //print error message 			
					echo '<span class= "contentText"><font color="red"><center><b>Unknown file type!</b></center></font><br><span>'; 			

				}//if(ext)

				else{
				  //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 
					$size=filesize($_FILES['image']['tmp_name']);
				  //compare the size with the maxim size we defined and print error if bigger

					if ($size > MAX_SIZE*1024){	
						echo '<span class = "headerText">You have exceeded the size limit!</span>';	
						$errors=1;
					}//if ($size > MAX_SIZE*1024)


					  //we will give an unique name, for example the time in unix time format
								 $image_name=time().'.'.$extension;;
					  //the new name will be containing the full path where will be stored (images folder)
								 $newname="images/uploaded/".$image_name;
					  //we verify if the image has been uploaded, and print error instead
						 $copied = copy($_FILES['image']['tmp_name'], $newname);

						 if (!$copied) {	
								echo '<span class = "headerText">Upload unsuccessful!</span>';	
								$errors=1;
						}//close of if copied


					}//close of else

			} //if ($image) 

		}//close of if



	 if(isset($_POST['Submit']) && !$errors)  { 	


		  list($width, $height) = getimagesize($newname) ; 
                                                         
              $modwidth = 180; 
                                                         
              $diff = $width / $modwidth;
                                                        
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($newname);
		  
		   
		  //$image1 = imagecreatefrompng($newname);
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                                                        
              imagejpeg($tn, $newname, 100) ; 	

				/////////////////////////////////////SQLI///////////////////////////////////
				$image = mysql_real_escape_string($_POST["newname"]);
				require_once('staffSql.php');

				mysql_select_db($dbname);

				$_SESSION['staffid'] = $staffid;

				//Query the database to see if the given username/password combination is valid.
				$query = "UPDATE staff SET photo='$newname' WHERE staffid='$staffid'";

				$result = mysql_query($query);	 	


				$success = TRUE;
				echo "<center><span class = 'headerText'>File Uploaded Successfully!</span><BR>
					<span class = 'text'>Click <a href=editImage.php>here</a> to view the photo you had updated.</span></center>"; 

			} 



?>
                <?php 

			if (isset($_SESSION['staffid'])==TRUE) {


				if($success==FALSE) { 

							if($photo==NULL) {

				?>
                <center>
                 <span class="headerText"> Upload your own photo and this will be displayed on the touch screen application.</span>
                    <form name="newad" method="post" enctype="multipart/form-data"  action="">
        <table>
          <tr>
            <td width="275"><div align="center">
              <input type="file" name="image">
              <br>
                  <span class="contentText">  ( <b>ONLY</b> JPEG files below 10mb will be accepted)</span> </div></td>
          </tr>
          <tr>
            <td><div align="center">
              <input name="Submit" type="submit" value="Upload image">
              <input type="hidden" name="formSubmitted" value='TRUE' />
            </div></td>
          </tr>
        </table>
                  </form>
                </center>
              <?php } else {  ?>
                <center>
                  <span class="headerText"> Your current picture is: </span>
                  <P> <?php echo '<img src ="'.$photo.'"/>  ';  ?>
                  <P>
                  <span class="headerText">Upload a new picture:</span>
                  <form name="newad" method="post" enctype="multipart/form-data"  action="">
                    <table>
                      <tr>
                        <td width="265"><div align="center">
                          <input type="file" name="image">
                          <br>
                          <span class="contentText">( <b>ONLY</b> JPEG files below 10mb will be accepted)</span></div></td>
                      </tr>
                      <tr>
                        <td><div align="center">
                          <input name="Submit" type="submit" value="Upload image">
                          <input type="hidden" name="formSubmitted" value='TRUE' />
                        </div></td>
                      </tr>
                    </table>
                  </form>
                </center>
              <?php   } } } else {

					echo '<center><BR>You must be logged in to upload photo!<P></center>';
					echo '<P> <P><P> <P><P> <P><P> <P>';


				}?>
            </td>
            <td valign="top" bgcolor="#FFFFFF"><!--HERE--></td>
            <td></td>
          </tr>
        </table>
      </td>
</tr>
<tr>
	<td width="800" height="26">	<?php include("include/footer.php") ?>		</td>
</tr>
</table>
<!-- End ImageReady Slices -->



</body>
</html>
<?php ob_flush(); ?>

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/124415-mysqli-to-mysql-image-upload-error/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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