Jump to content

Recommended Posts

I have a form to input some data in the database, it works when I do not upload an image, when I do it won't work and I just can't find what's wrong.

 

this is the setter of my class

 

case "image":
	$this->m_simage = $p_sValue;
break;

 

this is the getter of my class

 

case "image":
	$vResult = $this->m_simage;
break;

 

this is the code in my php page

 

//Image is optional so check if the user submitted an image
	if(isset($_FILES['tutorial_thumb']['name']) && !empty($_FILES['tutorial_thumb']['name']) && isset($_FILES['tutorial_thumb']['tmp_name']) && ! empty($_FILES['tutorial_thumb']['tmp_name'])):
		//Check if an image was uploaded, otherwise use the category thumbnail
		$filename = $_FILES['tutorial_thumb']['name'];
		$filetmp = $_FILES['tutorial_thumb']['tmp_name'];
		$filetype = $_FILES['tutorial_thumb']['type'];
		$filesize = $_FILES['tutorial_thumb']['size'];
		//Check the image size, it should be exactly 72*72
		//if(!Validator::ImageSizeBetween($filetmp, 73, 71, 73, 71)): throw new exception ("Your image must be exactly 72*72 !"); endif;

		//if the size is ok save the path to the image
		$rand = rand(1111,9999);
		$path = 'images/tutorials/'.$rand.$filename;

		//send the path the the image setter
		$tutorial->image = $path;
	else:
		$filename = "nofile";
		$filetmp = "nofile";
		$filetype = "nofile";
		$filesize = "nofile";
		$path = 'images/category/'.mysqli_real_escape_string($link, $_POST['category_tutorial']).".png";
		$tutorial->image = $path;
	endif;

 

and this is the function in my class

 

/*###############################################*/
public function tutorialSubmit($userid, $filetype, $filetmp, $path)
/*###############################################*/
{
global $link;

//check if a file was uploade
if($filetype != "nofile"):
	//Check the filetype first, if it was succesfyll upload the image
	if(($filetype == "image/gif") || ($filetype == "image/jpeg") || ($filetype == "image/pjpeg") || ($filetype == "image/png")):
			if(move_uploaded_file($filetmp, $path)):
				return(true);
			else:
				throw new exception (_imagenotuploaded_);
			endif;
	else:
			throw new exception (_allowedextentions_);
	endif;
endif;

//Set up the insert query
$sqlquery ="INSERT INTO tutorials (title, url, description, tutorial_thumb, tags, category_id, user_id) 
			VALUES 
			('".$this->m_stitle."', '".$this->m_surl."', '".$this->m_sdescription."',
			 '".$this->m_simage."', '".$this->m_stags."', '".$this->m_scategory."', '".$userid."')
			 ";
echo $this->m_simage;
//Check if the query was executed succesfully
if(mysqli_query($link,$sqlquery)):
	return (true);
else:
	throw new exception(_tutsubmissionunsucces_);
endif;
}// end of tutorialSubmit

 

I echo $this->m_simage;, when there I don't upload an image I see the ling to the category image but when I upload I see nothing...

if you find the problem let me know :D

Link to comment
https://forums.phpfreaks.com/topic/185617-query-wont-work-when-uploading-image/
Share on other sites

when you say "it doesn't work", what exactly do you mean?  does the script stop?  does the db query still go through, just doesn't upload the image?

 

do you have error reporting on? add:

 

trigger_error (mysql_error());

 

to your queries.

 

can you post your form, please.

your arguments seems to be ass backwards for your query:

 

mysqli_query($link,$sqlquery)

 

as per the manual, query comes before link_identifier .. weird.

 

He is using mysqli_query note the i :) In mysqli the connection is needed before the query. (Just an FYI).

your arguments seems to be ass backwards for your query:

 

mysqli_query($link,$sqlquery)

 

as per the manual, query comes before link_identifier .. weird.

 

He is using mysqli_query note the i :) In mysqli the connection is needed before the query. (Just an FYI).

 

man, i did see the i, yet i still looked up the mysql_query manual .. i don't currently use mysqli in my scripts, that's why the identifier coming before the query string looked out of place :P cheers premiso.

when you say "it doesn't work", what exactly do you mean?  does the script stop?  does the db query still go through, just doesn't upload the image?

 

do you have error reporting on? add:

 

trigger_error (mysql_error());

 

to your queries.

 

can you post your form, please.

 

it will upload the image but not process the query, i use a if-statement to see if the query was done succesfully or not, if not an exception will be thrown. but I get the success message so I don't get it...

 

this is the form:

 

<table class="add_tutorial">
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    	<tr>
        	<td><label><?php echo _submittitle_; ?></label></td>
            <td><input class="forminput" type="text" name="title" id="title" size="40" value="<?php if(isset($_POST['title'])){echo $_POST['title'];} ?>" /></td>
        </tr>
        <tr>
        	<td><label><?php echo _tutorialavatar_; ?></label></td>
            <td><input  class="forminput" type="file" name="tutorial_thumb" id="tutorial_thumb" size="40" />
                <br />
                <small>
                <ul>
                    <li>Only jpeg, png and gif are allowed! Your image should be exactly 72*72!</li>
                </ul>
                </small>
            </td>
        </tr>
        <tr>
            <td><label><?php echo _tutorialdescription_; ?></label></td>
            <td><textarea class="forminput" name="description" id="description" cols="45" rows="5"><?php if(isset($_POST['description'])){echo $_POST['description'];} ?></textarea></td>
        </tr>
        <tr>
           <td><label><?php echo _tutoriallink_; ?></label></td>
           <td><input class="forminput" type="text" name="tutorial" id="tutorial" size="60" value="<?php if(isset($_POST['tutorial'])){echo $_POST['tutorial'];} ?>"/></td>
        </tr>
        <tr>
           <td><label><?php echo _tutorialcategory_; ?></label></td>
           <td>
               <select name="category_tutorial" id="category_tutorial">
                   <?php
			   $category = mysqli_query($link, "SELECT category_id, category_name FROM categories");
                   while($categories = mysqli_fetch_object($category))
                   {
                       ?>
                       <option value="<?php echo $categories->category_id; ?>"><?php echo $categories->category_name; ?></option>
                       <?php
                   }
                   ?>
               </select>
           </td>
        </tr>
        <tr>
            <td><label for="tags"><?php echo _tags_; ?></label></td>
            <td>
                <input id="tags" class="forminput" name="tags" type="text" size="30" value="<?php if(isset($_POST['tags'])){echo $_POST['tags'];} ?>"/>
                <br/><small>Seperate each tag with a space</small>
            </td>
         </tr>
         <tr>
            <td><label for="captcha_code"><?php echo _tutorialantispam_; ?></label></td>
            <td>
                <img src="<?php echo $siteurl; ?>includes/captcha.php" alt="<?php echo _tutorialcaptcha_; ?>" title="<?php echo _tutorialcaptcha_; ?>" /><br />
                <small><?php echo _tutorialspamtext_; ?></small><br />
                <input class="forminput" name="captcha_code" type="text" size="30" />
            </td>
         </tr>
         <tr>
         	<td><input class="button" type="submit" name="submit_tutorial" value="<?php echo _tutorialsubmit_; ?>" /></td>
         </tr>
    </form>
    </table>

I finally found it, the if-statement that checked if the image was uploaded allready returned true, so for some reason it didn't work because of that.

 

if(($filetype == "image/gif") || ($filetype == "image/jpeg") || ($filetype == "image/pjpeg") || ($filetype == "image/png")):
            if(move_uploaded_file($filetmp, $path)):
               return(true);
            else:
               throw new exception (_imagenotuploaded_);
            endif;
      else:
            throw new exception (_allowedextentions_);
      endif;

 

I changed it to

 

if(($filetype == "image/gif") || ($filetype == "image/jpeg") || ($filetype == "image/pjpeg") || ($filetype == "image/png")):
            if(!move_uploaded_file($filetmp, $path)):
               throw new exception (_imagenotuploaded_);
            endif;
      else:
            throw new exception (_allowedextentions_);
      endif;

 

and now it will wright the url of the image into the database.

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.